@@ -91,6 +91,15 @@ abstract class OptionalPresenceCheckMethod extends Method {
9191abstract class OptionalGetValueMethod extends Method {
9292}
9393
94+ /**
95+ * Instance method which uses an alternative value in case the optional is empty.
96+ *
97+ * Does not include methods which throw an exception if the optional is empty
98+ * (e.g. `orElseThrow`).
99+ */
100+ abstract class OptionalOrMethod extends Method {
101+ }
102+
94103/**
95104 * JDK class representing an optional value. This includes the `Object` reference
96105 * storing `java.util.Optional` as well as the primitive value variants, such as
@@ -143,9 +152,20 @@ class JavaObjectOptionalOfNullableMethod extends NewNullableOptionalCallable, Me
143152class JavaObjectOptionalGetMethod extends OptionalGetValueMethod {
144153 JavaObjectOptionalGetMethod ( ) {
145154 getDeclaringSourceOrASupertype ( this ) instanceof JavaObjectOptional
146- and hasStringSignature ( [
147- "get()" ,
148- "orElseThrow()" // Equivalent to get()
155+ and (
156+ hasStringSignature ( "get()" )
157+ or hasName ( "orElseThrow" ) // Equivalent to get()
158+ )
159+ }
160+ }
161+
162+ class JavaObjectOptionalOrMethod extends OptionalOrMethod {
163+ JavaObjectOptionalOrMethod ( ) {
164+ getDeclaringSourceOrASupertype ( this ) instanceof JavaObjectOptional
165+ and hasName ( [
166+ "or" , // TODO: Maybe exclude this because it takes a `Supplier<Optional>` and returns an `Optional`
167+ "orElse" ,
168+ "orElseGet" ,
149169 ] )
150170 }
151171}
@@ -196,11 +216,23 @@ class JavaOptionalPresenceCheckMethod extends OptionalPresenceCheckMethod {
196216class JavaPrimitiveOptionalGetMethod extends OptionalGetValueMethod {
197217 JavaPrimitiveOptionalGetMethod ( ) {
198218 getDeclaringType ( ) instanceof JavaPrimitiveOptional
199- and hasStringSignature ( [
200- "orElseThrow()" , // Exists for all Optional types and is equivalent to their specific methods
201- "getAsDouble()" , // OptionalDouble
202- "getAsInt()" , // OptionalInt
203- "getAsLong()" // OptionalLong
219+ and (
220+ hasStringSignature ( [
221+ "getAsDouble()" , // OptionalDouble
222+ "getAsInt()" , // OptionalInt
223+ "getAsLong()" , // OptionalLong
224+ ] )
225+ or hasName ( "orElseThrow" ) // Exists for all Optional types and is equivalent to their specific `get...` methods
226+ )
227+ }
228+ }
229+
230+ class JavaPrimitiveOptionalOrMethod extends OptionalOrMethod {
231+ JavaPrimitiveOptionalOrMethod ( ) {
232+ getDeclaringType ( ) instanceof JavaPrimitiveOptional
233+ and hasName ( [
234+ "orElse" ,
235+ "orElseGet" ,
204236 ] )
205237 }
206238}
@@ -244,6 +276,23 @@ class GuavaOptionalIsPresentMethod extends OptionalPresenceCheckMethod {
244276 boolean polarity ( ) { result = true }
245277}
246278
279+ class GuavaOptionalGetMethod extends OptionalGetValueMethod {
280+ GuavaOptionalGetMethod ( ) {
281+ getDeclaringSourceOrASupertype ( this ) instanceof GuavaOptional
282+ and hasStringSignature ( "get()" )
283+ }
284+ }
285+
286+ class GuavaOptionalOrMethod extends OptionalOrMethod {
287+ GuavaOptionalOrMethod ( ) {
288+ getDeclaringSourceOrASupertype ( this ) instanceof GuavaOptional
289+ and hasName ( [
290+ "or" , // TODO: Maybe exclude overload `or(Optional)`, which returns an `Optional`?
291+ "orNull" ,
292+ ] )
293+ }
294+ }
295+
247296class GuavaOptionalOrNullCall extends OptionalObjectOrNullCall {
248297 GuavaOptionalOrNullCall ( ) {
249298 exists ( Method m | m = getMethod ( ) |
0 commit comments