Class FactoryNewInstance<DATATYPE>

java.lang.Object
com.helger.base.lang.clazz.FactoryNewInstance<DATATYPE>
Type Parameters:
DATATYPE - The return type of the factory
All Implemented Interfaces:
Supplier<DATATYPE>

@Immutable public class FactoryNewInstance<DATATYPE> extends Object implements Supplier<DATATYPE>
Implementation of Supplier that always creates a new instance via reflection
Author:
Philip Helger
  • Constructor Details

    • FactoryNewInstance

      public FactoryNewInstance(@Nullable Class<? extends DATATYPE> aClass, boolean bCheckInstancable)
      Constructor.
      Parameters:
      aClass - The class to instantiate. May be null.
      bCheckInstancable - true to check that the class can be instantiated, false to skip the check.
  • Method Details

    • getFactoryClass

      public @Nullable Class<? extends DATATYPE> getFactoryClass()
      Returns:
      The class to be instantiated. May be null.
    • get

      public @Nullable DATATYPE get()
      Create a new instance of the factory class via reflection.
      Specified by:
      get in interface Supplier<DATATYPE>
      Returns:
      The newly created instance or null if instantiation failed.
    • 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
    • create

      public static <DATATYPE> @NonNull FactoryNewInstance<DATATYPE> create(@Nullable Class<DATATYPE> aClass)
      Create a new factory without an instancability check.
      Type Parameters:
      DATATYPE - The data type to be created
      Parameters:
      aClass - The class to instantiate. May be null.
      Returns:
      The new factory instance. Never null.
    • create

      public static <DATATYPE> @NonNull FactoryNewInstance<DATATYPE> create(@Nullable Class<DATATYPE> aClass, boolean bCheckInstancable)
      Create a new factory.
      Type Parameters:
      DATATYPE - The data type to be created
      Parameters:
      aClass - The class to instantiate. May be null.
      bCheckInstancable - true to check that the class can be instantiated, false to skip the check.
      Returns:
      The new factory instance. Never null.