FloatFunction.java

  1. package swingtree.api;

  2. import com.google.errorprone.annotations.Immutable;

  3. /**
  4.  *  Used by {@link NoiseFunction#andThen(FloatFunction)}
  5.  *  to apply a scalar function to the result of a noise function
  6.  *  or to the input of a noise function using {@link NoiseFunction#compose(FloatFunction)}.
  7.  */
  8. @Immutable
  9. @FunctionalInterface
  10. public interface FloatFunction
  11. {
  12.     /**
  13.      *  Applies this function to the given float value
  14.      *  and returns the result.
  15.      *
  16.      * @param in The input float value.
  17.      * @return The output float value.
  18.      */
  19.     float from( float in );

  20.     /**
  21.      *  Allows you to compose this function with another function
  22.      *  so that the other function is applied after this function.
  23.      *
  24.      * @param after The function to apply after this function.
  25.      * @return A new function that applies this function and then the given function.
  26.      */
  27.     default FloatFunction andThen( FloatFunction after ) {
  28.         return (x) -> after.from(from(x));
  29.     }

  30.     /**
  31.      *  Allows you to compose this function with another function
  32.      *  so that the other function is applied before this function.
  33.      *
  34.      * @param before The function to apply before this function.
  35.      * @return A new function that applies the given function and then this function.
  36.      */
  37.     default FloatFunction compose( FloatFunction before ) {
  38.         return (x) -> from(before.from(x));
  39.     }
  40. }