Skip to content

Commit

Permalink
Add Java 9 Optional APIs (gwtproject#9838)
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 authored Jul 3, 2023
1 parent 26764ef commit 9b9e888
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 12 deletions.
34 changes: 30 additions & 4 deletions user/super/com/google/gwt/emul/java/util/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;
import static javaemul.internal.InternalPreconditions.checkCriticalNotNull;
import static javaemul.internal.InternalPreconditions.checkNotNull;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;
import static javaemul.internal.InternalPreconditions.checkCriticalNotNull;
import static javaemul.internal.InternalPreconditions.checkNotNull;
import java.util.stream.Stream;

/**
* See <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html">
Expand Down Expand Up @@ -72,6 +73,14 @@ public void ifPresent(Consumer<? super T> consumer) {
}
}

public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
if (isPresent()) {
action.accept(ref);
} else {
emptyAction.run();
}
}

public Optional<T> filter(Predicate<? super T> predicate) {
checkNotNull(predicate);
if (!isPresent() || predicate.test(ref)) {
Expand All @@ -96,6 +105,23 @@ public <U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
return empty();
}

public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) {
checkNotNull(supplier);
if (isPresent()) {
return this;
} else {
return (Optional) checkNotNull(supplier.get());
}
}

public Stream<T> stream() {
if (isPresent()) {
return Stream.of(ref);
} else {
return Stream.empty();
}
}

public T orElse(T other) {
return isPresent() ? ref : other;
}
Expand Down
21 changes: 19 additions & 2 deletions user/super/com/google/gwt/emul/java/util/OptionalDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;

import java.util.function.DoubleConsumer;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;
import java.util.stream.DoubleStream;

/**
* See <a href="https://docs.oracle.com/javase/8/docs/api/java/util/OptionalDouble.html">
Expand Down Expand Up @@ -65,6 +66,22 @@ public void ifPresent(DoubleConsumer consumer) {
}
}

public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
if (isPresent()) {
action.accept(ref);
} else {
emptyAction.run();
}
}

public DoubleStream stream() {
if (isPresent()) {
return DoubleStream.of(ref);
} else {
return DoubleStream.empty();
}
}

public double orElse(double other) {
return present ? ref : other;
}
Expand Down
21 changes: 19 additions & 2 deletions user/super/com/google/gwt/emul/java/util/OptionalInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;

import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;
import java.util.stream.IntStream;

/**
* See <a href="https://docs.oracle.com/javase/8/docs/api/java/util/OptionalInt.html">
Expand Down Expand Up @@ -65,6 +66,22 @@ public void ifPresent(IntConsumer consumer) {
}
}

public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) {
if (present) {
action.accept(ref);
} else {
emptyAction.run();
}
}

public IntStream stream() {
if (present) {
return IntStream.of(ref);
} else {
return IntStream.empty();
}
}

public int orElse(int other) {
return present ? ref : other;
}
Expand Down
21 changes: 19 additions & 2 deletions user/super/com/google/gwt/emul/java/util/OptionalLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package java.util;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;

import java.util.function.LongConsumer;
import java.util.function.LongSupplier;
import java.util.function.Supplier;

import static javaemul.internal.InternalPreconditions.checkCriticalElement;
import java.util.stream.LongStream;

/**
* See <a href="https://docs.oracle.com/javase/8/docs/api/java/util/OptionalLong.html">
Expand Down Expand Up @@ -65,6 +66,22 @@ public void ifPresent(LongConsumer consumer) {
}
}

public void ifPresentOrElse(LongConsumer action, Runnable emptyAction) {
if (present) {
action.accept(ref);
} else {
emptyAction.run();
}
}

public LongStream stream() {
if (present) {
return LongStream.of(ref);
} else {
return LongStream.empty();
}
}

public long orElse(long other) {
return present ? ref : other;
}
Expand Down
12 changes: 10 additions & 2 deletions user/test/com/google/gwt/emultest/EmulJava9Suite.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import com.google.gwt.emultest.java9.util.ListTest;
import com.google.gwt.emultest.java9.util.MapTest;
import com.google.gwt.emultest.java9.util.OptionalDoubleTest;
import com.google.gwt.emultest.java9.util.OptionalIntTest;
import com.google.gwt.emultest.java9.util.OptionalLongTest;
import com.google.gwt.emultest.java9.util.OptionalTest;
import com.google.gwt.emultest.java9.util.SetTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand All @@ -26,8 +30,12 @@
@RunWith(Suite.class)
@SuiteClasses({
ListTest.class,
SetTest.class,
MapTest.class
MapTest.class,
OptionalDoubleTest.class,
OptionalIntTest.class,
OptionalLongTest.class,
OptionalTest.class,
SetTest.class
})
public class EmulJava9Suite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.emultest.java9.util;

import com.google.gwt.emultest.java.util.EmulTestBase;

import java.util.OptionalDouble;
import java.util.stream.Stream;

/**
* Tests for java.util.OptionalDouble Java 9 API emulation.
*/
public class OptionalDoubleTest extends EmulTestBase {
public void testIfPresentOrElse() {
int[] called = {0};
OptionalDouble.of(10.0).ifPresentOrElse(value -> {
assertEquals(10.0, value);
called[0]++;
}, () -> {
fail("should not call empty action");
});
assertEquals(1, called[0]);
called[0] = 0;
OptionalDouble.empty().ifPresentOrElse(ignore -> {
fail("Should not call present action");
}, () -> called[0]++);
assertEquals(1, called[0]);
}

public void testStream() {
assertEquals(0, OptionalDouble.empty().stream().count());
assertEquals(1, OptionalDouble.of(10.0).stream().count());

assertEquals(
new double[] {10.0, 100.0, 1000.0},
Stream.of(
OptionalDouble.of(10.0),
OptionalDouble.empty(),
OptionalDouble.of(100.0),
OptionalDouble.of(1000.0)
).flatMapToDouble(OptionalDouble::stream).toArray()
);
}
}
57 changes: 57 additions & 0 deletions user/test/com/google/gwt/emultest/java9/util/OptionalIntTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.emultest.java9.util;

import com.google.gwt.emultest.java.util.EmulTestBase;

import java.util.OptionalInt;
import java.util.stream.Stream;

/**
* Tests for java.util.OptionalInt Java 9 API emulation.
*/
public class OptionalIntTest extends EmulTestBase {
public void testIfPresentOrElse() {
int[] called = {0};
OptionalInt.of(10).ifPresentOrElse(value -> {
assertEquals(10, value);
called[0]++;
}, () -> {
fail("should not call empty action");
});
assertEquals(1, called[0]);
called[0] = 0;
OptionalInt.empty().ifPresentOrElse(ignore -> {
fail("Should not call present action");
}, () -> called[0]++);
assertEquals(1, called[0]);
}

public void testStream() {
assertEquals(0, OptionalInt.empty().stream().count());
assertEquals(1, OptionalInt.of(10).stream().count());

assertEquals(
new int[] {10, 100, 1000},
Stream.of(
OptionalInt.of(10),
OptionalInt.empty(),
OptionalInt.of(100),
OptionalInt.of(1000)
).flatMapToInt(OptionalInt::stream).toArray()
);
}
}
57 changes: 57 additions & 0 deletions user/test/com/google/gwt/emultest/java9/util/OptionalLongTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.emultest.java9.util;

import com.google.gwt.emultest.java.util.EmulTestBase;

import java.util.OptionalLong;
import java.util.stream.Stream;

/**
* Tests for java.util.OptionalLong Java 9 API emulation.
*/
public class OptionalLongTest extends EmulTestBase {
public void testIfPresentOrElse() {
int[] called = {0};
OptionalLong.of(10).ifPresentOrElse(value -> {
assertEquals(10, value);
called[0]++;
}, () -> {
fail("should not call empty action");
});
assertEquals(1, called[0]);
called[0] = 0;
OptionalLong.empty().ifPresentOrElse(ignore -> {
fail("Should not call present action");
}, () -> called[0]++);
assertEquals(1, called[0]);
}

public void testStream() {
assertEquals(0, OptionalLong.empty().stream().count());
assertEquals(1, OptionalLong.of(10).stream().count());

assertEquals(
new long[] {10, 100, 1000},
Stream.of(
OptionalLong.of(10),
OptionalLong.empty(),
OptionalLong.of(100),
OptionalLong.of(1000)
).flatMapToLong(OptionalLong::stream).toArray()
);
}
}
Loading

0 comments on commit 9b9e888

Please sign in to comment.