@@ -91,6 +91,15 @@ abstract class OptionalPresenceCheckMethod extends Method {
91
91
abstract class OptionalGetValueMethod extends Method {
92
92
}
93
93
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
+
94
103
/**
95
104
* JDK class representing an optional value. This includes the `Object` reference
96
105
* storing `java.util.Optional` as well as the primitive value variants, such as
@@ -143,9 +152,20 @@ class JavaObjectOptionalOfNullableMethod extends NewNullableOptionalCallable, Me
143
152
class JavaObjectOptionalGetMethod extends OptionalGetValueMethod {
144
153
JavaObjectOptionalGetMethod ( ) {
145
154
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" ,
149
169
] )
150
170
}
151
171
}
@@ -196,11 +216,23 @@ class JavaOptionalPresenceCheckMethod extends OptionalPresenceCheckMethod {
196
216
class JavaPrimitiveOptionalGetMethod extends OptionalGetValueMethod {
197
217
JavaPrimitiveOptionalGetMethod ( ) {
198
218
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" ,
204
236
] )
205
237
}
206
238
}
@@ -244,6 +276,23 @@ class GuavaOptionalIsPresentMethod extends OptionalPresenceCheckMethod {
244
276
boolean polarity ( ) { result = true }
245
277
}
246
278
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
+
247
296
class GuavaOptionalOrNullCall extends OptionalObjectOrNullCall {
248
297
GuavaOptionalOrNullCall ( ) {
249
298
exists ( Method m | m = getMethod ( ) |
0 commit comments