Class ElementCreationService

java.lang.Object
ru.objectsfill.service.ElementCreationService

public class ElementCreationService extends Object
The ElementCreationService class is responsible for generating single values or instances of classes based on the provided field type and Fill object. It maintains separate containers for BoxTypeFill and ObjectTypeFill implementations, which are used to generate values for primitive types and object types, respectively.
  • Field Details

    • DEFAULT_LOCAL_CLASS_CREATION_PATH

      public static final String DEFAULT_LOCAL_CLASS_CREATION_PATH
      DEFAULT_LOCAL_CLASS_CREATION_PATH
      See Also:
  • Constructor Details

    • ElementCreationService

      public ElementCreationService()
      Constructs a new ElementCreationService and initializes the containers by searching for local container implementations and adding default mappings.
  • Method Details

    • generateSingleValue

      public Object generateSingleValue(Class<?> fieldType, Fill fill)
      Generates a single value based on the provided field type and Fill object. First, it attempts to find a BoxTypeFill implementation for the field type in the containerBoxType. If found, it delegates the generation to that implementation. If not found, it attempts to find an ObjectTypeFill implementation for the field type in the containerObjectType. If found, it delegates the generation to that implementation. If no suitable implementation is found, it creates a new instance of the field type using createInstance() method.
      Parameters:
      fieldType - The type of the field for which the value is being generated.
      fill - The Fill object containing the necessary information for generation.
      Returns:
      The generated single value.
    • fillCollectionStream

      public <T> Stream<T> fillCollectionStream(Field field, Fill fill)
      Fills a collection stream based on the provided field and Fill object. Extracts the generic collection type from the field's ParameterizedType and attempts to generate a collection stream based on that type using generateCollectionByClassType() method.
      Type Parameters:
      T - The type of elements in the collection.
      Parameters:
      field - The field for which the collection stream is being filled.
      fill - The Fill object containing the necessary information for generation.
      Returns:
      A stream of collection elements.
    • findClassInContainer

      public static <T> Optional<T> findClassInContainer(Class<?> fieldType, Map<Class<?>,T> container)
      Finds the value associated with the provided field type in the given container map. It first attempts to find an exact match for the field type in the map. If found, it returns the corresponding value. If not found, it searches for a key in the map that is assignable from the field type. If found, it returns the value associated with that key.
      Type Parameters:
      T - The type of value to retrieve.
      Parameters:
      fieldType - The type of the field for which a value is being searched.
      container - The container map in which to search for the value.
      Returns:
      An optional containing the value if found, or empty if not found.
    • createInstance

      public static <T> T createInstance(Class<T> vClass, Fill fill)
      Creates a new instance of the provided class type using reflection. The created instance is assigned a Fill object with decreased depth, and the fill() method is called to populate the fields of the instance.
      Type Parameters:
      T - The type of the instance being created.
      Parameters:
      vClass - The class type of the instance being created.
      fill - The Fill object containing the necessary information for generation.
      Returns:
      The created instance, or null if an error occurred during instantiation.