Friday, 20 May 2016

Java: Owner: Specifying custom tokenizer


By implementing Tokenizer interface, @TokenizerClass annotation you can specify a custom tokeniser.
import org.aeonbits.owner.Tokenizer;

public class DollarTokenizer implements Tokenizer{

  @Override
  public String[] tokens(String values) {
    return values.split("\\$");
  }

}

import org.aeonbits.owner.Tokenizer;

public class SpaceTokenizer implements Tokenizer{

  @Override
  public String[] tokens(String values) {
    return values.split(" ");
  }

}

import java.util.List;

import org.aeonbits.owner.Config;

public interface ProjectConfig extends Config {

  @TokenizerClass(DollarTokenizer.class)
  @DefaultValue("Saturday$Sunday")
  String[] days();

  @TokenizerClass(SpaceTokenizer.class)
  @DefaultValue("January February March April")
  String[] month();

  @Separator("_")
  @DefaultValue("1988_1989")
  List<Integer> year();
}

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("days", "Tuesday$Wednesday$Thrusday$Friday");
    properties1.put("month", "October November December");

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

    String[] days = cfg.days();
    String[] months = cfg.month();
    List<Integer> years = cfg.year();

    System.out.println("----------------------");
    for (String day : days) {
      System.out.println(day);
    }

    System.out.println("----------------------");
    for (String month : months) {
      System.out.println(month);
    }

    System.out.println("----------------------");
    for (int year : years) {
      System.out.println(year);
    }
  }
}


Output
----------------------
Tuesday
Wednesday
Thrusday
Friday
----------------------
October
November
December
----------------------
1988
1989

You can also apply @TokenizerClass annotation on class level.

import java.util.List;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.Config.TokenizerClass;

@TokenizerClass(DollarTokenizer.class)
public interface ProjectConfig extends Config {

  @DefaultValue("Saturday$Sunday")
  String[] days();

  @DefaultValue("January February March April")
  String[] month();

  @TokenizerClass(SpaceTokenizer.class)
  @DefaultValue("1988 1989")
  List<Integer> year();
}

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("days", "Tuesday$Wednesday$Thrusday$Friday");
    properties1.put("month", "October$November$December");

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

    String[] days = cfg.days();
    String[] months = cfg.month();
    List<Integer> years = cfg.year();

    System.out.println("----------------------");
    for (String day : days) {
      System.out.println(day);
    }

    System.out.println("----------------------");
    for (String month : months) {
      System.out.println(month);
    }

    System.out.println("----------------------");
    for (int year : years) {
      System.out.println(year);
    }
  }
}


Output
----------------------
Tuesday
Wednesday
Thrusday
Friday
----------------------
October
November
December
----------------------
1988
1989






Previous                                                 Next                                                 Home

No comments:

Post a Comment