Friday 20 May 2016

Java: Owner: Parameterized properties


You can provide parameterized properties on methods.
package owner_api_tutorial;

import org.aeonbits.owner.Config;

public interface ProjectConfig extends Config {

  @Key("first_name")
  @DefaultValue("Hello Mr. %s!")
  String firstName(String name);

  @Key("full_name")
  @DefaultValue("Hello Mr. %s %s!")
  String fullName(String firstName, String lastName);

}

package owner_api_tutorial;

import org.aeonbits.owner.ConfigFactory;
import java.util.*;

public class PropertyUtil {
  public static void main(String args[]) throws Exception {
    Map<String, String> properties1 = new HashMap<>();

    properties1.put("first_name", "Hari Krishna");
    properties1.put("full_name", "Hari Krishna");

    ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class,
        properties1);

    System.out.println(cfg.firstName("Sandesh"));
    System.out.println(cfg.fullName("Sandesh", "Nair"));
  }
}


Output
Hari Krishna
Hari Krishna


Now comment following lines in PropertyUtil.java and re run.
properties1.put("first_name", "Hari Krishna");
properties1.put("full_name", "Hari Krishna");

package owner_api_tutorial;

import org.aeonbits.owner.ConfigFactory;
import java.util.*;

public class PropertyUtil {
  public static void main(String args[]) throws Exception {
    Map<String, String> properties1 = new HashMap<>();

    // properties1.put("first_name", "Hari Krishna");
    // properties1.put("full_name", "Hari Krishna");

    ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class,
        properties1);

    System.out.println(cfg.firstName("Sandesh"));
    System.out.println(cfg.fullName("Sandesh", "Nair"));
  }
}


Output
Hello Mr. Sandesh!
Hello Mr. Sandesh Nair!


Owner API use the class java.util.Formatter, to format string.

How to disable parameters formatting?

You can disable parameter formatting, by using DisableFeature annotation.
@DisableFeature(PARAMETER_FORMATTING)
package owner_api_tutorial;

import org.aeonbits.owner.Config;

public interface ProjectConfig extends Config {

  @Key("first_name")
  @DefaultValue("Hello Mr. %s!")
  String firstName(String name);

  @Key("full_name")
  @DefaultValue("Hello Mr. %s %s!")
  @DisableFeature(DisableableFeature.PARAMETER_FORMATTING)
  String fullName(String firstName, String lastName);

}

package owner_api_tutorial;

import org.aeonbits.owner.ConfigFactory;
import java.util.*;

public class PropertyUtil {
  public static void main(String args[]) throws Exception {
    Map<String, String> properties1 = new HashMap<>();

    // properties1.put("first_name", "Hari Krishna");
    // properties1.put("full_name", "Hari Krishna");

    ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class,
        properties1);

    System.out.println(cfg.firstName("Sandesh"));
    System.out.println(cfg.fullName("Sandesh", "Nair"));
  }
}


Output
Hello Mr. Sandesh!
Hello Mr. %s %s!



Previous                                                 Next                                                 Home

No comments:

Post a Comment