Skip to content

Commit

Permalink
Trivial refactoring: Simplify Optional method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellansun committed Dec 22, 2024
1 parent e233689 commit d532d52
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void newGroovyLambdaWrapperAndLoad(final ClassNode lambdaClass, final La

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public static <T> Stream<T> stream(final NullObject self) {
* @since 3.0.0
*/
public static <T> Stream<T> stream(final Optional<T> self) {
return self.map(Stream::of).orElseGet(Stream::empty);
return self.stream();
}

//
Expand All @@ -497,7 +497,7 @@ public static <T> Stream<T> stream(final Optional<T> self) {
* @since 3.0.0
*/
public static IntStream stream(final OptionalInt self) {
if (!self.isPresent()) {
if (self.isEmpty()) {
return IntStream.empty();
}
return IntStream.of(self.getAsInt());
Expand All @@ -510,7 +510,7 @@ public static IntStream stream(final OptionalInt self) {
* @since 3.0.0
*/
public static LongStream stream(final OptionalLong self) {
if (!self.isPresent()) {
if (self.isEmpty()) {
return LongStream.empty();
}
return LongStream.of(self.getAsLong());
Expand All @@ -523,7 +523,7 @@ public static LongStream stream(final OptionalLong self) {
* @since 3.0.0
*/
public static DoubleStream stream(final OptionalDouble self) {
if (!self.isPresent()) {
if (self.isEmpty()) {
return DoubleStream.empty();
}
return DoubleStream.of(self.getAsDouble());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public static <T> Optional<T> filter(final Optional<?> self, final Class<T> type
* @since 3.0.0
*/
public static OptionalInt filter(final OptionalInt self, final IntPredicate test) {
if (!self.isPresent() || !test.test(self.getAsInt())) {
if (self.isEmpty() || !test.test(self.getAsInt())) {
return OptionalInt.empty();
}
return self;
Expand All @@ -449,7 +449,7 @@ public static OptionalInt filter(final OptionalInt self, final IntPredicate test
* @since 3.0.0
*/
public static OptionalLong filter(final OptionalLong self, final LongPredicate test) {
if (!self.isPresent() || !test.test(self.getAsLong())) {
if (self.isEmpty() || !test.test(self.getAsLong())) {
return OptionalLong.empty();
}
return self;
Expand All @@ -469,7 +469,7 @@ public static OptionalLong filter(final OptionalLong self, final LongPredicate t
* @since 3.0.0
*/
public static OptionalDouble filter(final OptionalDouble self, final DoublePredicate test) {
if (!self.isPresent() || !test.test(self.getAsDouble())) {
if (self.isEmpty() || !test.test(self.getAsDouble())) {
return OptionalDouble.empty();
}
return self;
Expand All @@ -488,7 +488,7 @@ public static OptionalDouble filter(final OptionalDouble self, final DoublePredi
* @since 3.0.0
*/
public static <T> Optional<T> mapToObj(final OptionalInt self, final IntFunction<? extends T> mapper) {
if (!self.isPresent()) {
if (self.isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(mapper.apply(self.getAsInt()));
Expand All @@ -507,7 +507,7 @@ public static <T> Optional<T> mapToObj(final OptionalInt self, final IntFunction
* @since 3.0.0
*/
public static <T> Optional<T> mapToObj(final OptionalLong self, final LongFunction<? extends T> mapper) {
if (!self.isPresent()) {
if (self.isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(mapper.apply(self.getAsLong()));
Expand All @@ -526,7 +526,7 @@ public static <T> Optional<T> mapToObj(final OptionalLong self, final LongFuncti
* @since 3.0.0
*/
public static <T> Optional<T> mapToObj(final OptionalDouble self, final DoubleFunction<? extends T> mapper) {
if (!self.isPresent()) {
if (self.isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(mapper.apply(self.getAsDouble()));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public MetaMethod transformMetaMethod(final MetaClass metaClass, final MetaMetho
CachedMethod cachedMethod = (CachedMethod) metaMethod;
CachedClass declaringClass = metaMethod.getDeclaringClass();
Optional<CachedMethod> transformedMethod = Optional.ofNullable(cachedMethod.getTransformedMethod());
if (!transformedMethod.isPresent()
if (transformedMethod.isEmpty()
// if caller can access the method legally, there is no need to transform the cached method
&& !checkAccessible(caller, declaringClass.getTheClass(), metaMethod.getModifiers(), false)) {
Class<?> theClass = metaClass.getTheClass();
Expand Down

0 comments on commit d532d52

Please sign in to comment.