Services.Environment

This example demonstrates how to create an environment using the Services.environment utility method.

  static void map () {

    final HashMap< Name, Object > map =
      new HashMap<> ();

    final Name string =
      Services.name (
        "string"
      );

    final Name integer =
      Services.name (
        "integer"
      );

    map.put (
      string,
      "string"
    );

    map.put (
      integer,
      42
    );

    final Environment env =
      Services.environment (
        map::get
      );

    assert
      env.getObject (
        string
      ).filter (
        "string"::equals
      ).isPresent ();

    assert
      !env.getBoolean (
        string
      ).filter (
        Boolean.TRUE::equals
      ).isPresent ();

    assert
      42 ==
        env.getInteger (
          integer,
          0
        );

    assert
      !env.getString (
        integer
      ).filter (
        "42"::equals
      ).isPresent ();

  }

This example demonstrates how to nest and chain together multiple environments via the environment(Environment) method. In the code below you can see the overriding of the name property value via the nested environment.

  static void chaining () {

    final Name name =
      Services.name (
        "name"
      );

    final Environment env =
      Services.environment (
        name,
        "parent"
      );

    assert
      "parent".equals (
        env.getString (
          name,
          null
        )
      );

    assert
      "child".equals (
        env.environment (
          name,
          "child"
        ).getString (
          name, null
        )
      );

    assert
      "parent".equals (
        env.environment (
          n ->
            Optional.empty ()
        ).getString (
          name,
          null
        )
      );


  }

This example demonstrates how to create an environment from various property name to value chained calls. Note the code can be shortened with static import of the Services.name and Services.environment methods.

  static void constructing () {

    final Environment env =
      Services.environment (
        Services.name (
          "one"
        ),
        1
      ).environment (
        Services.name (
          "two"
        ),
        2
      );

    assert
      1 ==
        env.getInteger (
          Services.name (
            "one"
          ),
          0
        );

    assert
      2 ==
        env.getInteger (
          Services.name (
            "two"
          ),
          0
        );

  }

This example demonstrates how an environment can be mapped to a property storage in transforming a Name to a String.

static void transforming () {

    final Properties props =
      new Properties ();

    props.setProperty (
      "name.one",
      "1"
    );

    final Environment env =
      Services.environment (
        n ->
          props.getProperty (
            n.toString ()
          )
      );

    assert
      1 ==
        env.getInteger (
          Services.name (
            "name"
          ).name (
            "one"
          ),
          0
        );

  }