Skip to content

Commit d532d52

Browse files
committed
Trivial refactoring: Simplify Optional method calls
1 parent e233689 commit d532d52

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

Diff for: src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesLambdaWriter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private void newGroovyLambdaWrapperAndLoad(final ClassNode lambdaClass, final La
186186

187187
Optional<ConstructorNode> generatedConstructor = lambdaClass.getDeclaredConstructors().stream()
188188
.filter(ctor -> Boolean.TRUE.equals(ctor.getNodeMetaData(IS_GENERATED_CONSTRUCTOR))).findFirst();
189-
if (!generatedConstructor.isPresent()) {
189+
if (generatedConstructor.isEmpty()) {
190190
throw new GroovyBugError("Failed to find the generated constructor");
191191
}
192192

Diff for: src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public static <T> Stream<T> stream(final NullObject self) {
485485
* @since 3.0.0
486486
*/
487487
public static <T> Stream<T> stream(final Optional<T> self) {
488-
return self.map(Stream::of).orElseGet(Stream::empty);
488+
return self.stream();
489489
}
490490

491491
//
@@ -497,7 +497,7 @@ public static <T> Stream<T> stream(final Optional<T> self) {
497497
* @since 3.0.0
498498
*/
499499
public static IntStream stream(final OptionalInt self) {
500-
if (!self.isPresent()) {
500+
if (self.isEmpty()) {
501501
return IntStream.empty();
502502
}
503503
return IntStream.of(self.getAsInt());
@@ -510,7 +510,7 @@ public static IntStream stream(final OptionalInt self) {
510510
* @since 3.0.0
511511
*/
512512
public static LongStream stream(final OptionalLong self) {
513-
if (!self.isPresent()) {
513+
if (self.isEmpty()) {
514514
return LongStream.empty();
515515
}
516516
return LongStream.of(self.getAsLong());
@@ -523,7 +523,7 @@ public static LongStream stream(final OptionalLong self) {
523523
* @since 3.0.0
524524
*/
525525
public static DoubleStream stream(final OptionalDouble self) {
526-
if (!self.isPresent()) {
526+
if (self.isEmpty()) {
527527
return DoubleStream.empty();
528528
}
529529
return DoubleStream.of(self.getAsDouble());

Diff for: src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ public static <T> Optional<T> filter(final Optional<?> self, final Class<T> type
429429
* @since 3.0.0
430430
*/
431431
public static OptionalInt filter(final OptionalInt self, final IntPredicate test) {
432-
if (!self.isPresent() || !test.test(self.getAsInt())) {
432+
if (self.isEmpty() || !test.test(self.getAsInt())) {
433433
return OptionalInt.empty();
434434
}
435435
return self;
@@ -449,7 +449,7 @@ public static OptionalInt filter(final OptionalInt self, final IntPredicate test
449449
* @since 3.0.0
450450
*/
451451
public static OptionalLong filter(final OptionalLong self, final LongPredicate test) {
452-
if (!self.isPresent() || !test.test(self.getAsLong())) {
452+
if (self.isEmpty() || !test.test(self.getAsLong())) {
453453
return OptionalLong.empty();
454454
}
455455
return self;
@@ -469,7 +469,7 @@ public static OptionalLong filter(final OptionalLong self, final LongPredicate t
469469
* @since 3.0.0
470470
*/
471471
public static OptionalDouble filter(final OptionalDouble self, final DoublePredicate test) {
472-
if (!self.isPresent() || !test.test(self.getAsDouble())) {
472+
if (self.isEmpty() || !test.test(self.getAsDouble())) {
473473
return OptionalDouble.empty();
474474
}
475475
return self;
@@ -488,7 +488,7 @@ public static OptionalDouble filter(final OptionalDouble self, final DoublePredi
488488
* @since 3.0.0
489489
*/
490490
public static <T> Optional<T> mapToObj(final OptionalInt self, final IntFunction<? extends T> mapper) {
491-
if (!self.isPresent()) {
491+
if (self.isEmpty()) {
492492
return Optional.empty();
493493
}
494494
return Optional.ofNullable(mapper.apply(self.getAsInt()));
@@ -507,7 +507,7 @@ public static <T> Optional<T> mapToObj(final OptionalInt self, final IntFunction
507507
* @since 3.0.0
508508
*/
509509
public static <T> Optional<T> mapToObj(final OptionalLong self, final LongFunction<? extends T> mapper) {
510-
if (!self.isPresent()) {
510+
if (self.isEmpty()) {
511511
return Optional.empty();
512512
}
513513
return Optional.ofNullable(mapper.apply(self.getAsLong()));
@@ -526,7 +526,7 @@ public static <T> Optional<T> mapToObj(final OptionalLong self, final LongFuncti
526526
* @since 3.0.0
527527
*/
528528
public static <T> Optional<T> mapToObj(final OptionalDouble self, final DoubleFunction<? extends T> mapper) {
529-
if (!self.isPresent()) {
529+
if (self.isEmpty()) {
530530
return Optional.empty();
531531
}
532532
return Optional.ofNullable(mapper.apply(self.getAsDouble()));

Diff for: src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public MetaMethod transformMetaMethod(final MetaClass metaClass, final MetaMetho
197197
CachedMethod cachedMethod = (CachedMethod) metaMethod;
198198
CachedClass declaringClass = metaMethod.getDeclaringClass();
199199
Optional<CachedMethod> transformedMethod = Optional.ofNullable(cachedMethod.getTransformedMethod());
200-
if (!transformedMethod.isPresent()
200+
if (transformedMethod.isEmpty()
201201
// if caller can access the method legally, there is no need to transform the cached method
202202
&& !checkAccessible(caller, declaringClass.getTheClass(), metaMethod.getModifiers(), false)) {
203203
Class<?> theClass = metaClass.getTheClass();

0 commit comments

Comments
 (0)