Class AbstractGenerator

java.lang.Object
dev.orne.test.rnd.AbstractGenerator
All Implemented Interfaces:
Generator
Direct Known Subclasses:
AbstractParameterizableGenerator, AbstractTypedGenerator, ArrayGenerator, EnumGenerator

@API(status=STABLE, since="0.1") public abstract class AbstractGenerator extends Object implements Generator
Abstract basic implementation of Generator. Provides basic implementations for common methods.
Since:
0.1
Version:
1.0, 2021-03
Author:
(w) Iker Hernaez
  • Field Details

    • DEFAULT_NULL_PROBABILITY

      public static final float DEFAULT_NULL_PROBABILITY
      The default probability of null values.
      See Also:
  • Constructor Details

    • AbstractGenerator

      protected AbstractGenerator()
      Creates a new instance.
  • Method Details

    • assertSupported

      protected void assertSupported(@NotNull @NotNull Class<?> type)
      Verifies that the specified type is supported by this instance. If test fails throws an IllegalArgumentException exception as documented in Generator.defaultValue(Class), nullableDefaultValue(Class), Generator.randomValue(Class) and nullableRandomValue(Class).
      Parameters:
      type - The requested value type.
      Throws:
      UnsupportedValueTypeException - If the specified type is not supported.
    • nullableDefaultValue

      public <T> T nullableDefaultValue(@NotNull @NotNull Class<T> type)
      Returns the default value for the specified type allowing null values.

      This method should return null except for native types.

      Specified by:
      nullableDefaultValue in interface Generator
      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      Returns:
      The nullable default value for the specified type.
    • nullableRandomValue

      public <T> T nullableRandomValue(@NotNull @NotNull Class<T> type)
      Returns a random value of the specified type allowing null values.

      The returned value has a probability of be null except for native types. If not null behaves as randomValue().

      Specified by:
      nullableRandomValue in interface Generator
      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      Returns:
      A random nullable value for the specified type.
      See Also:
    • getNullProbability

      @Min(0L) @Max(1L) public @javax.validation.constraints.Min(0L),@javax.validation.constraints.Max(1L) float getNullProbability()
      Returns the probability of null value in nullableRandomValue(Class).
      Returns:
      The probability of null values.
    • setNullProbability

      public void setNullProbability(@Min(0L) @Max(1L) @javax.validation.constraints.Min(0L),@javax.validation.constraints.Max(1L) float prob)
      Sets the probability of null value in nullableRandomValue(Class).
      Parameters:
      prob - The probability of null values.
    • randomNull

      public boolean randomNull(@NotNull @NotNull Class<?> type)
      Determines if the value must be null based on the probability of null values.

      Overriding classes can use the type parameter to detect non nullable types (like primitives).

      Parameters:
      type - The requested value type
      Returns:
      If the value must be null
      See Also:
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object