Class Wrapper<DATATYPE>

java.lang.Object
com.helger.base.wrapper.Wrapper<DATATYPE>
Type Parameters:
DATATYPE - The type of object to be wrapped.
All Implemented Interfaces:
ICloneable<Wrapper<DATATYPE>>, IMutableWrapper<DATATYPE>, IWrapper<DATATYPE>, Supplier<DATATYPE>

@NotThreadSafe public class Wrapper<DATATYPE> extends Object implements IMutableWrapper<DATATYPE>, ICloneable<Wrapper<DATATYPE>>
This is a stupid wrapper around any object. Its original purpose was the encapsulation of return values from within an anonymous class.
Author:
Philip Helger
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Wrapper(@Nullable DATATYPE aObj)
    Constructor with an existing object.
    Wrapper(@NonNull IWrapper<DATATYPE> aRhs)
    Copy constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> @NonNull Wrapper<T>
    Create a new Wrapper object that contains a null value.
    boolean
     
    @Nullable DATATYPE
    get()
    boolean
    get(@NonNull IToBooleanFunction<DATATYPE> aProvider, boolean bDefault)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use the version with Predicate instead
    <T> @Nullable T
    get(@NonNull Function<DATATYPE,T> aProvider)
    Apply the provided function on the value contained in this wrapper, but only if the value is not null.
    boolean
    get(@NonNull Predicate<? super DATATYPE> aProvider, boolean bDefault)
    Apply the provided function on the value contained in this wrapper, but only if the value is not null.
    double
    get(@NonNull ToDoubleFunction<DATATYPE> aProvider, double dDefault)
    Apply the provided function on the value contained in this wrapper, but only if the value is not null.
    int
    get(@NonNull ToIntFunction<DATATYPE> aProvider, int nDefault)
    Apply the provided function on the value contained in this wrapper, but only if the value is not null.
    long
    get(@NonNull ToLongFunction<DATATYPE> aProvider, long nDefault)
    Apply the provided function on the value contained in this wrapper, but only if the value is not null.
    @NonNull Wrapper<DATATYPE>
     
    int
     
    static <T> @NonNull Wrapper<T>
    of(@Nullable T aValue)
    Create a new Wrapper object that contains the provided value.
    static <T> @NonNull Wrapper<T>
    of(@NonNull IWrapper<T> aValue)
    Create a new Wrapper object that contains the value of the provided Wrapper.
    @NonNull EChange
    set(@Nullable DATATYPE aObj)
    Change the wrapped object.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface com.helger.base.wrapper.IWrapper

    isNotSet, isSet
  • Constructor Details

    • Wrapper

      public Wrapper()
      Default constructor. Wraps a null values.
    • Wrapper

      public Wrapper(@Nullable DATATYPE aObj)
      Constructor with an existing object.
      Parameters:
      aObj - The existing object. May be null.
    • Wrapper

      public Wrapper(@NonNull IWrapper<DATATYPE> aRhs)
      Copy constructor. Only takes wrappers of the same type.
      Parameters:
      aRhs - The other wrapper to use. May not be null.
  • Method Details

    • get

      public @Nullable DATATYPE get()
      Specified by:
      get in interface IWrapper<DATATYPE>
      Specified by:
      get in interface Supplier<DATATYPE>
      Returns:
      The currently wrapped object. May be null.
    • get

      public <T> @Nullable T get(@NonNull Function<DATATYPE,T> aProvider)
      Apply the provided function on the value contained in this wrapper, but only if the value is not null.
      Type Parameters:
      T - The data type to be returned.
      Parameters:
      aProvider - The function to be applied on the wrapped value.
      Returns:
      null if the wrapped value is null or if the function returned null.
      Since:
      12.1.2
    • get

      public int get(@NonNull ToIntFunction<DATATYPE> aProvider, int nDefault)
      Apply the provided function on the value contained in this wrapper, but only if the value is not null.
      Parameters:
      aProvider - The function to be applied on the wrapped value.
      nDefault - the value to be returned if the wrapped value is null.
      Returns:
      The extracted value.
      Since:
      12.1.2
    • get

      public long get(@NonNull ToLongFunction<DATATYPE> aProvider, long nDefault)
      Apply the provided function on the value contained in this wrapper, but only if the value is not null.
      Parameters:
      aProvider - The function to be applied on the wrapped value.
      nDefault - the value to be returned if the wrapped value is null.
      Returns:
      The extracted value.
      Since:
      12.1.2
    • get

      public double get(@NonNull ToDoubleFunction<DATATYPE> aProvider, double dDefault)
      Apply the provided function on the value contained in this wrapper, but only if the value is not null.
      Parameters:
      aProvider - The function to be applied on the wrapped value.
      dDefault - the value to be returned if the wrapped value is null.
      Returns:
      The extracted value.
      Since:
      12.1.2
    • get

      @Deprecated(forRemoval=true, since="12.1.5") public boolean get(@NonNull IToBooleanFunction<DATATYPE> aProvider, boolean bDefault)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use the version with Predicate instead
      Apply the provided function on the value contained in this wrapper, but only if the value is not null.
      Parameters:
      aProvider - The function to be applied on the wrapped value.
      bDefault - the value to be returned if the wrapped value is null.
      Returns:
      The extracted value.
      Since:
      12.1.2
    • get

      public boolean get(@NonNull Predicate<? super DATATYPE> aProvider, boolean bDefault)
      Apply the provided function on the value contained in this wrapper, but only if the value is not null.
      Parameters:
      aProvider - The function to be applied on the wrapped value.
      bDefault - the value to be returned if the wrapped value is null.
      Returns:
      The extracted value.
      Since:
      12.1.5
    • set

      public @NonNull EChange set(@Nullable DATATYPE aObj)
      Change the wrapped object.
      Specified by:
      set in interface IMutableWrapper<DATATYPE>
      Parameters:
      aObj - The new object to be wrapped. May be null.
      Returns:
      EChange
    • getClone

      @ReturnsMutableCopy public @NonNull Wrapper<DATATYPE> getClone()
      Specified by:
      getClone in interface ICloneable<DATATYPE>
      Returns:
      A clone of this wrapper containing the same wrapped object. Never null.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • empty

      public static <T> @NonNull Wrapper<T> empty()
      Create a new Wrapper object that contains a null value.
      Type Parameters:
      T - The type to be wrapped.
      Returns:
      A non-null Wrapper instance.
      Since:
      12.1.2
    • of

      public static <T> @NonNull Wrapper<T> of(@Nullable T aValue)
      Create a new Wrapper object that contains the provided value.
      Type Parameters:
      T - The type to be wrapped.
      Parameters:
      aValue - The value to be wrapped. May be null.
      Returns:
      A non-null Wrapper instance.
      Since:
      12.1.2
    • of

      public static <T> @NonNull Wrapper<T> of(@NonNull IWrapper<T> aValue)
      Create a new Wrapper object that contains the value of the provided Wrapper.
      Type Parameters:
      T - The type to be wrapped.
      Parameters:
      aValue - The wrapped value to be wrapped again. May not be null.
      Returns:
      A non-null Wrapper instance.
      Since:
      12.1.2