Class PluginDefaultGroovyMethods

java.lang.Object
org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
org.codehaus.groovy.vmplugin.v8.PluginDefaultGroovyMethods

public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport
Defines new Groovy methods which appear on standard Java 8 classes within the Groovy environment.
Since:
2.5.0
  • Method Details

    • next

      public static Object next(Enum self)
      Overloads the ++ operator for enums. It will invoke Groovy's default next behaviour for enums that do not have their own next method.
      Parameters:
      self - an Enum
      Returns:
      the next defined enum from the enum class
      Since:
      1.5.2
    • previous

      public static Object previous(Enum self)
      Overloads the -- operator for enums. It will invoke Groovy's default previous behaviour for enums that do not have their own previous method.
      Parameters:
      self - an Enum
      Returns:
      the previous defined enum from the enum class
      Since:
      1.5.2
    • collect

      public static <S, T> Future<T> collect(Future<S> self, @ClosureParams(FirstGenericType.class) Closure<T> transform)
      Returns a Future asynchronously returning a transformed result.
       import java.util.concurrent.*
       def executor = Executors.newSingleThreadExecutor()
       Future foobar = executor.submit{ "foobar" }
       Future foobarSize = foobar.collect{ it.size() }
       assert foobarSize.get() == 6
       executor.shutdown()
       
      Parameters:
      self - a Future
      transform - the closure used to transform the Future value
      Returns:
      a Future allowing the transformed value to be obtained asynchronously
      Since:
      3.0.0
    • asBoolean

      public static boolean asBoolean(Optional<?> optional)
      Coerce an Optional instance to a boolean value.
       assert !Optional.empty().asBoolean()
       assert Optional.of(1234).asBoolean()
       
      Returns:
      true if a value is present, false otherwise
      Since:
      2.5.0
    • get

      public static int get(OptionalInt self)
      If a value is present in the OptionalInt, returns the value.
       assert OptionalInt.of(1234).get() == 1234
       
      Throws:
      NoSuchElementException - if no value is present
      Since:
      3.0.0
    • get

      public static long get(OptionalLong self)
      If a value is present in the OptionalLong, returns the value.
       assert OptionalLong.of(1234L).get() == 1234L
       
      Throws:
      NoSuchElementException - if no value is present
      Since:
      3.0.0
    • get

      public static double get(OptionalDouble self)
      If a value is present in the OptionalDouble, returns the value.
       assert OptionalDouble.of(Math.PI).get() == Math.PI
       
      Throws:
      NoSuchElementException - if no value is present
      Since:
      3.0.0
    • getAt

      public static <T> T getAt(Optional<T> self, int index)
      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 == ''
       
      Throws:
      IndexOutOfBoundsException - if index is not 0 or -1
      Since:
      5.0.0
    • peek

      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.
       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
       
      Since:
      5.0.0
    • peek

      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.
       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
       
      Since:
      5.0.0
    • peek

      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.
       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
       
      Since:
      5.0.0
    • peek

      public static <T> 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.
       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
       
      Since:
      5.0.0
    • collect

      public static <S, T> Optional<T> collect(Optional<S> self, @ClosureParams(FirstGenericType.class) 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.
       assert Optional.of("foobar").collect{ it.size() }.get() == 6
       assert !Optional.empty().collect{ it.size() }.isPresent()
       
      Parameters:
      transform - the closure used to transform the optional value if present
      Returns:
      an Optional containing the transformed value or empty if the input is empty or the transform returns null
      Since:
      3.0.0
    • filter

      public static <T> Optional<T> filter(Optional<?> self, Class<T> type)
      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)
       
      Since:
      3.0.0
    • filter

      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.
       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
       
      Since:
      3.0.0
    • filter

      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.
       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
       
      Since:
      3.0.0
    • filter

      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.
       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
       
      Since:
      3.0.0
    • mapToObj

      public static <T> 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.
       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'
       
      Since:
      3.0.0
    • mapToObj

      public static <T> 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.
       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'
       
      Since:
      3.0.0
    • mapToObj

      public static <T> 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.
       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')
       
      Since:
      3.0.0
    • mapToInt

      public static <T> 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.
       assert !Optional.empty().mapToInt(x -> 42).isPresent()
       assert  Optional.of('x').mapToInt(x -> 42).getAsInt() == 42
       
      Since:
      3.0.0
    • mapToLong

      public static <T> 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.
       assert !Optional.empty().mapToLong(x -> 42L).isPresent()
       assert  Optional.of('x').mapToLong(x -> 42L).getAsLong() == 42L
       
      Since:
      3.0.0
    • mapToDouble

      public static <T> 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.
       assert !Optional.empty().mapToDouble(x -> Math.PI).isPresent()
       assert  Optional.of('x').mapToDouble(x -> Math.PI).getAsDouble() == Math.PI
       
      Since:
      3.0.0
    • orOptional

      public static <T> Optional<T> orOptional(Optional<T> self, Supplier<Optional<? extends T>> supplier)
      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'
       
      Since:
      3.0.6
    • getPid

      public static String getPid(Runtime self)
      Gets the pid of the current Java process.
      Since:
      4.0.0
    • leftShift

      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.
      Parameters:
      self - a StringBuilder
      value - a value to append
      Returns:
      the StringBuilder on which this operation was invoked
      Since:
      1.5.2
    • plus

      public static String plus(StringBuilder self, String value)
      Appends a String to this StringBuilder.
      Parameters:
      self - a StringBuilder
      value - a String
      Returns:
      a String
      Since:
      1.5.2
    • putAt

      public static void putAt(StringBuilder self, EmptyRange range, Object value)
      Supports the range subscript operator for StringBuilder.
      Parameters:
      self - a StringBuilder
      range - a Range
      value - the object that's toString() will be inserted
      Since:
      1.5.2
    • putAt

      public static void putAt(StringBuilder self, IntRange range, Object value)
      Supports the range subscript operator for StringBuilder. Index values are treated as characters within the builder.
      Parameters:
      self - a StringBuilder
      range - a Range
      value - the object that's toString() will be inserted
      Since:
      1.5.2
    • size

      @Deprecated public static int size(StringBuilder self)
      Deprecated.
      Provides the standard Groovy size() method for StringBuilder.
      Parameters:
      self - a StringBuilder
      Returns:
      the length of the StringBuilder
      Since:
      1.5.2
      See Also:
    • stream

      @Deprecated public static <T> Stream<T> stream(T self)
      Deprecated.
    • stream

      @Deprecated public static <T> Stream<T> stream(T[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Integer> stream(int[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Long> stream(long[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Double> stream(double[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Character> stream(char[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Byte> stream(byte[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Short> stream(short[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Boolean> stream(boolean[] self)
      Deprecated.
    • stream

      @Deprecated public static Stream<Float> stream(float[] self)
      Deprecated.
    • stream

      @Deprecated public static <T> Stream<T> stream(Enumeration<T> self)
      Deprecated.
    • stream

      @Deprecated public static <T> Stream<T> stream(Iterable<T> self)
      Deprecated.
    • stream

      @Deprecated public static <T> Stream<T> stream(Iterator<T> self)
      Deprecated.
    • stream

      @Deprecated public static <T> Stream<T> stream(Spliterator<T> self)
      Deprecated.
    • stream

      @Deprecated public static <T> Stream<T> stream(NullObject self)
      Deprecated.
    • stream

      @Deprecated public static <T> Stream<T> stream(Optional<T> self)
      Deprecated.
    • stream

      @Deprecated public static IntStream stream(OptionalInt self)
      Deprecated.
    • stream

      @Deprecated public static LongStream stream(OptionalLong self)
      Deprecated.
    • stream

      @Deprecated public static DoubleStream stream(OptionalDouble self)
      Deprecated.
    • intStream

      @Deprecated public static IntStream intStream(int[] self)
      Deprecated.
    • longStream

      @Deprecated public static LongStream longStream(long[] self)
      Deprecated.
    • doubleStream

      @Deprecated public static DoubleStream doubleStream(double[] self)
      Deprecated.
    • toArray

      @Deprecated public static <T> T[] toArray(Stream<? extends T> self, Class<T> type)
      Deprecated.
    • toList

      @Deprecated public static <T> List<T> toList(Stream<T> self)
      Deprecated.
    • toList

      @Deprecated public static <T> List<T> toList(BaseStream<T,? extends BaseStream> self)
      Deprecated.
    • toSet

      @Deprecated public static <T> Set<T> toSet(Stream<T> self)
      Deprecated.
    • toSet

      @Deprecated public static <T> Set<T> toSet(BaseStream<T,? extends BaseStream> self)
      Deprecated.