Class Settings

java.lang.Object
org.instancio.settings.Settings

public final class Settings extends Object
This class provides an API for updating settings programmatically. An instance of this class can be created using one of the following static methods:
  • create() - returns a new instance of blank settings
  • defaults() - returns a new instance containing default settings

Out of the box, Instancio uses default settings as returned by defaults(). Defaults can be overridden either globally using a configuration file, or per-object using the API, for example:


     // Create a blank instance of settings and set the overrides
     Settings settings = Settings.create()
         .set(Keys.COLLECTION_MIN_SIZE, 50)
         .set(Keys.COLLECTION_MAX_SIZE, 100);

     // Pass the overrides when creating an object
     Person person = Instancio.of(Person.class)
         .withSettings(settings)
         .create();
 

For information on how to override settings globally using a configuration file, please refer to the user guide.

Since:
1.0.1
See Also:
  • Method Details

    • create

      public static Settings create()
      Create a new instance of empty settings.
      Returns:
      empty settings
    • defaults

      public static Settings defaults()
      Create default settings.
      Returns:
      settings containing defaults
    • from

      public static Settings from(Map<Object,Object> map)
      Create settings from the given map.
      Parameters:
      map - to create settings from
      Returns:
      settings
    • from

      public static Settings from(Settings other)
      Create settings from the given settings.
      Parameters:
      other - settings to create settings from
      Returns:
      a new instance of settings
    • merge

      public Settings merge(@Nullable Settings other)
      Creates a new instance of settings by merging given settings with these settings.
      Parameters:
      other - settings to merge
      Returns:
      new instance of merged settings
    • get

      public <T> T get(SettingKey key)
      Get setting value for given key.
      Type Parameters:
      T - setting value type
      Parameters:
      key - setting key
      Returns:
      value for given key, or null if none.
    • set

      public Settings set(SettingKey key, @Nullable Object value)
      Set the setting with the given key to the specified value.

      Note: when updating range settings (such as Keys.COLLECTION_MIN_SIZE and Keys.COLLECTION_MAX_SIZE), range bounds are auto-adjusted by Constants.RANGE_ADJUSTMENT_PERCENTAGE if the new minimum is higher than the current maximum, and vice versa.

      Parameters:
      key - to set
      value - to set
      Returns:
      updated settings
    • mapType

      public Settings mapType(Class<?> from, Class<?> to)
      Map 'from' supertype to 'to' subtype.
      Parameters:
      from - supertype class
      to - subtype class
      Returns:
      updated settings
    • getSubtypeMap

      public Map<Class<?>,Class<?>> getSubtypeMap()
      Returns a read-only view of the subtype map.
      Returns:
      subtype map
    • lock

      public Settings lock()
      Locks these settings for further modifications, making this instance immutable.
      Returns:
      read-only settings
    • toString

      public String toString()
      Overrides:
      toString in class Object