Friday 20 May 2016

Java: Owner: Type conversion


Owner API automatically convert primitive types and enums.
public enum Day {
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY, SATURDAY
}

import org.aeonbits.owner.Config;

public interface ProjectConfig extends Config {

  @Key("first_name")
  @DefaultValue("Krishna")
  String firstName();
  
  @DefaultValue("SUNDAY")
  Day day();
  
  @DefaultValue("5.4")
  float height();

}

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("height", "5.8");
    properties1.put("day", "FRIDAY");

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

    System.out.println(cfg.firstName());
    System.out.println(cfg.day());
    System.out.println(cfg.height());
  }
}


Output
Krishna
FRIDAY
5.8




Previous                                                 Next                                                 Home

No comments:

Post a Comment