public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport
Defines new Groovy methods which appear on standard Java 8 classes within the Groovy environment.
Type Params | Return Type | Name and description |
---|---|---|
|
public static boolean |
asBoolean(Optional<?> optional) Coerce an Optional instance to a boolean value. |
<S, T> |
public static Future<T> |
collect(Future<S> self, Closure<T> transform) Returns a Future asynchronously returning a transformed result. |
<S, T> |
public static Optional<T> |
collect(Optional<S> self, Closure<T> transform) If a value is present in the Optional , returns transformed value
obtained using the transform closure or no value as an optional. |
|
public static DoubleStream |
doubleStream(double[] self) |
<T> |
public static Optional<T> |
filter(Optional<?> self, Class<T> type) Tests given value against specified type and changes generics of result. |
|
public static OptionalInt |
filter(OptionalInt self, IntPredicate test) If a value is present in the OptionalInt , tests the value using
the given predicate and returns the optional if the test returns true or
else empty.
|
|
public static OptionalLong |
filter(OptionalLong self, LongPredicate test) If a value is present in the OptionalLong , tests the value using
the given predicate and returns the optional if the test returns true or
else empty.
|
|
public static OptionalDouble |
filter(OptionalDouble self, DoublePredicate test) If a value is present in the OptionalDouble , tests the value using
the given predicate and returns the optional if the test returns true or
empty otherwise.
|
|
public static int |
get(OptionalInt self) If a value is present in the OptionalInt , returns the value. |
|
public static long |
get(OptionalLong self) If a value is present in the OptionalLong , returns the value. |
|
public static double |
get(OptionalDouble self) If a value is present in the OptionalDouble , returns the value. |
<T> |
public static T |
getAt(Optional<T> self, int index) If a value is present in the Optional , returns the value or null. |
|
public static String |
getPid(Runtime self) Gets the pid of the current Java process. |
|
public static IntStream |
intStream(int[] self) |
|
public static StringBuilder |
leftShift(StringBuilder self, Object value) Overloads the left shift operator to provide an easy way to append multiple objects as string representations to a StringBuilder. |
|
public static LongStream |
longStream(long[] self) |
<T> |
public static OptionalDouble |
mapToDouble(Optional<T> self, ToDoubleFunction<? super T> mapper) If a value is present in the Optional , returns an OptionalDouble
consisting of the result of applying the given function to the value or else empty.
|
<T> |
public static OptionalInt |
mapToInt(Optional<T> self, ToIntFunction<? super T> mapper) If a value is present in the Optional , returns an OptionalInt
consisting of the result of applying the given function to the value or else empty.
|
<T> |
public static OptionalLong |
mapToLong(Optional<T> self, ToLongFunction<? super T> mapper) If a value is present in the Optional , returns an OptionalLong
consisting of the result of applying the given function to the value or else empty.
|
<T> |
public static Optional<T> |
mapToObj(OptionalInt self, IntFunction<? extends T> mapper) If a value is present in the OptionalInt , returns an Optional
consisting of the result of applying the given function to the value or else empty.
|
<T> |
public static Optional<T> |
mapToObj(OptionalLong self, LongFunction<? extends T> mapper) If a value is present in the OptionalLong , returns an Optional
consisting of the result of applying the given function to the value or else empty.
|
<T> |
public static Optional<T> |
mapToObj(OptionalDouble self, DoubleFunction<? extends T> mapper) If a value is present in the OptionalDouble , returns an Optional
consisting of the result of applying the given function to the value or else empty.
|
|
public static Object |
next(Enum self) Overloads the ++ operator for enums. |
<T> |
public static Optional<T> |
orOptional(Optional<T> self, Supplier<Optional<? extends T>> supplier) Provides similar functionality to JDK9 or on JDK8. |
|
public static OptionalInt |
peek(OptionalInt self, IntConsumer action) If a value is present in the OptionalInt , executes the specified
action with the value as input and then returns self . |
|
public static OptionalLong |
peek(OptionalLong self, LongConsumer action) If a value is present in the OptionalLong , executes the specified
action with the value as input and then returns self . |
|
public static OptionalDouble |
peek(OptionalDouble self, DoubleConsumer action) If a value is present in the OptionalDouble , executes the specified
action with the value as input and then returns self . |
<T> |
public static Optional<T> |
peek(Optional<T> self, Consumer<? super T> action) If a value is present in the Optional , executes the specified
action with the value as input and then returns self . |
|
public static String |
plus(StringBuilder self, String value) Appends a String to this StringBuilder. |
|
public static Object |
previous(Enum self) Overloads the -- operator for enums. |
|
public static void |
putAt(StringBuilder self, EmptyRange range, Object value) Supports the range subscript operator for StringBuilder. |
|
public static void |
putAt(StringBuilder self, IntRange range, Object value) Supports the range subscript operator for StringBuilder. |
|
public static int |
size(StringBuilder self) Provides the standard Groovy size() method for StringBuilder. |
<T> |
public static Stream<T> |
stream(T self) |
<T> |
public static Stream<T> |
stream(T[] self) |
|
public static Stream<Integer> |
stream(int[] self) |
|
public static Stream<Long> |
stream(long[] self) |
|
public static Stream<Double> |
stream(double[] self) |
|
public static Stream<Character> |
stream(char[] self) |
|
public static Stream<Byte> |
stream(byte[] self) |
|
public static Stream<Short> |
stream(short[] self) |
|
public static Stream<Boolean> |
stream(boolean[] self) |
|
public static Stream<Float> |
stream(float[] self) |
<T> |
public static Stream<T> |
stream(Enumeration<T> self) |
<T> |
public static Stream<T> |
stream(Iterable<T> self) |
<T> |
public static Stream<T> |
stream(Iterator<T> self) |
<T> |
public static Stream<T> |
stream(Spliterator<T> self) |
<T> |
public static Stream<T> |
stream(NullObject self) |
<T> |
public static Stream<T> |
stream(Optional<T> self) |
|
public static IntStream |
stream(OptionalInt self) |
|
public static LongStream |
stream(OptionalLong self) |
|
public static DoubleStream |
stream(OptionalDouble self) |
<T> |
public static T[] |
toArray(Stream<? extends T> self, Class<T> type) |
<T> |
public static List<T> |
toList(Stream<T> self) |
<T> |
public static List<T> |
toList(BaseStream<T, ? extends BaseStream> self) |
<T> |
public static Set<T> |
toSet(Stream<T> self) |
<T> |
public static Set<T> |
toSet(BaseStream<T, ? extends BaseStream> self) |
Coerce an Optional
instance to a boolean
value.
assert !Optional.empty().asBoolean() assert Optional.of(1234).asBoolean()
true
if a value is present, false
otherwise Returns a Future
asynchronously returning a transformed result.
import java.util.concurrent.* def executor = Executors.newSingleThreadExecutor() Futurefoobar = executor.submit{ "foobar" } Future foobarSize = foobar.collect{ it.size() } assert foobarSize.get() == 6 executor.shutdown()
self
- a Futuretransform
- the closure used to transform the Future value If a value is present in the Optional
, returns transformed value
obtained using the transform
closure or no value as an optional.
assert Optional.of("foobar").collect{ it.size() }.get() == 6 assert !Optional.empty().collect{ it.size() }.isPresent()
transform
- the closure used to transform the optional value if present Tests given value against specified type and changes generics of result.
This is equivalent to: self.filter(it -> it instanceof Type).map(it -> (Type) it)
assert !Optional.empty().filter(Number).isPresent() assert !Optional.of('x').filter(Number).isPresent() assert Optional.of(1234).filter(Number).isPresent() assert Optional.of(1234).filter(Number).get().equals(1234)
If a value is present in the OptionalInt
, tests the value using
the given predicate and returns the optional if the test returns true or
else empty.
assert !OptionalInt.empty().filter(i -> true).isPresent() assert OptionalInt.of(1234).filter(i -> true).isPresent() assert !OptionalInt.of(1234).filter(i -> false).isPresent() assert OptionalInt.of(1234).filter(i -> true).getAsInt() == 1234
If a value is present in the OptionalLong
, tests the value using
the given predicate and returns the optional if the test returns true or
else empty.
assert !OptionalLong.empty().filter(n -> true).isPresent() assert OptionalLong.of(123L).filter(n -> true).isPresent() assert !OptionalLong.of(123L).filter(n -> false).isPresent() assert OptionalLong.of(123L).filter(n -> true).getAsLong() == 123L
If a value is present in the OptionalDouble
, tests the value using
the given predicate and returns the optional if the test returns true or
empty otherwise.
assert !OptionalDouble.empty().filter(n -> true).isPresent() assert OptionalDouble.of(Math.PI).filter(n -> true).isPresent() assert !OptionalDouble.of(Math.PI).filter(n -> false).isPresent() assert OptionalDouble.of(Math.PI).filter(n -> true).getAsDouble() == Math.PI
If a value is present in the OptionalInt
, returns the value.
assert OptionalInt.of(1234).get() == 1234
If a value is present in the OptionalLong
, returns the value.
assert OptionalLong.of(1234L).get() == 1234L
If a value is present in the OptionalDouble
, returns the value.
assert OptionalDouble.of(Math.PI).get() == Math.PI
If a value is present in the Optional
, returns the value or null.
def opt = Optional.empty() assert opt[-1] == null assert opt[0] == null opt = Optional.of('') assert opt[-1] == '' assert opt[0] == '' groovy.test.GroovyAssert.shouldFail(IndexOutOfBoundsException) { opt[1] } // use via destructuring opt = Optional.empty() def (String s) = opt assert s == null opt = Optional.of('') (s) = opt assert s == ''
Overloads the left shift operator to provide an easy way to append multiple objects as string representations to a StringBuilder.
self
- a StringBuildervalue
- a value to append If a value is present in the Optional
, returns an OptionalDouble
consisting of the result of applying the given function to the value or else empty.
assert !Optional.empty().mapToDouble(x -> Math.PI).isPresent() assert Optional.of('x').mapToDouble(x -> Math.PI).getAsDouble() == Math.PI
If a value is present in the Optional
, returns an OptionalInt
consisting of the result of applying the given function to the value or else empty.
assert !Optional.empty().mapToInt(x -> 42).isPresent() assert Optional.of('x').mapToInt(x -> 42).getAsInt() == 42
If a value is present in the Optional
, returns an OptionalLong
consisting of the result of applying the given function to the value or else empty.
assert !Optional.empty().mapToLong(x -> 42L).isPresent() assert Optional.of('x').mapToLong(x -> 42L).getAsLong() == 42L
If a value is present in the OptionalInt
, returns an Optional
consisting of the result of applying the given function to the value or else empty.
assert !OptionalInt.empty().mapToObj(x -> new Object()).isPresent() assert OptionalInt.of(1234).mapToObj(x -> new Object()).isPresent() assert !OptionalInt.of(1234).mapToObj(x -> null).isPresent() assert OptionalInt.of(1234).mapToObj(Integer::toString).get() == '1234'
If a value is present in the OptionalLong
, returns an Optional
consisting of the result of applying the given function to the value or else empty.
assert !OptionalLong.empty().mapToObj(x -> new Object()).isPresent() assert OptionalLong.of(123L).mapToObj(x -> new Object()).isPresent() assert !OptionalLong.of(123L).mapToObj(x -> null).isPresent() assert OptionalLong.of(1234L).mapToObj(Long::toString).get() == '1234'
If a value is present in the OptionalDouble
, returns an Optional
consisting of the result of applying the given function to the value or else empty.
assert !OptionalDouble.empty().mapToObj(x -> new Object()).isPresent() assert OptionalDouble.of(Math.PI).mapToObj(x -> new Object()).isPresent() assert !OptionalDouble.of(Math.PI).mapToObj(x -> null).isPresent() assert OptionalDouble.of(Math.PI).mapToObj(Double::toString).get().startsWith('3.14')
Overloads the ++
operator for enums. It will invoke Groovy's
default next behaviour for enums that do not have their own next method.
self
- an Enum Provides similar functionality to JDK9 or
on JDK8.
def x = Optional.empty() def y = Optional.of('y') assert y.orOptional(() -> Optional.of('z')).get() == 'y' assert x.orOptional(() -> Optional.of('z')).get() == 'z'
If a value is present in the OptionalInt
, executes the specified
action
with the value as input and then returns self
.
boolean called = false def opt = OptionalInt.empty() def out = opt.peek{ called = true } assert out === opt assert !called opt = OptionalInt.of(42) out = opt.peek{ assert it == 42; called = true } assert out === opt assert called
If a value is present in the OptionalLong
, executes the specified
action
with the value as input and then returns self
.
boolean called = false def opt = OptionalLong.empty() def out = opt.peek{ called = true } assert out === opt assert !called opt = OptionalLong.of(42L) out = opt.peek{ assert it == 42L; called = true } assert out === opt assert called
If a value is present in the OptionalDouble
, executes the specified
action
with the value as input and then returns self
.
boolean called = false def opt = OptionalDouble.empty() def out = opt.peek{ called = true } assert out === opt assert !called opt = OptionalDouble.of(Math.PI) out = opt.peek{ assert it == Math.PI; called = true } assert out === opt assert called
If a value is present in the Optional
, executes the specified
action
with the value as input and then returns self
.
boolean called = false def opt = Optional.empty() def out = opt.peek{ called = true } assert out === opt assert !called opt = Optional.of('x') out = opt.peek{ assert it == 'x'; called = true } assert out === opt assert called
Appends a String to this StringBuilder.
self
- a StringBuildervalue
- a String Overloads the --
operator for enums. It will invoke Groovy's
default previous behaviour for enums that do not have their own previous method.
self
- an EnumSupports the range subscript operator for StringBuilder.
self
- a StringBuilderrange
- a Rangevalue
- the object that's toString() will be insertedSupports the range subscript operator for StringBuilder. Index values are treated as characters within the builder.
self
- a StringBuilderrange
- a Rangevalue
- the object that's toString() will be inserted Provides the standard Groovy size()
method for StringBuilder.
self
- a StringBuilder