Class PropertyScope

java.lang.Object
org.moeaframework.util.PropertyScope
All Implemented Interfaces:
AutoCloseable

public class PropertyScope extends Object implements AutoCloseable
Creates a scope for a TypedProperties, allowing the properties to be temporarily overridden within the scope. These should typically be used within try-with-resources so the scope is automatically closed when exiting the block.
   try (PropertyScope scope = properties.createScope()) {
       // temporarily override properties within scope
       properties.setInt("populationSize", 500);
   }
 
*

Scopes are not thread-safe or thread-local. This means two threads referencing the same underlying TypedProperties object will both see modifications made within the scope. If this is a concern, create a copy of the properties before use.

  • Constructor Details

    • PropertyScope

      public PropertyScope(TypedProperties properties)
      Creates a new scope for the properties.
      Parameters:
      properties - the properties
  • Method Details

    • with

      public PropertyScope with(String key, double value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public PropertyScope with(String key, float value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public PropertyScope with(String key, long value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public PropertyScope with(String key, int value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public PropertyScope with(String key, short value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public PropertyScope with(String key, byte value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public PropertyScope with(String key, boolean value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public <T extends Enum<?>> PropertyScope with(String key, T value)
      Convenience method for setting a property within the scope.
      Type Parameters:
      T - the enumeration type
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • with

      public PropertyScope with(String key, String value)
      Convenience method for setting a property within the scope.
      Parameters:
      key - the property key
      value - the property value
      Returns:
      a reference to this scope for chaining multiple calls together
    • without

      public PropertyScope without(String key)
      Convenience method for removing a property within the scope.
      Parameters:
      key - the property key
      Returns:
      a reference to this scope for chaining multiple calls together
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable