Table of Contents ⇒ Java Algorithm Library-Artery-Sample
package jp.avaj.lib.algo;
import jp.avaj.lib.test.L;
/**
Example of using ArAutoValueConverter(Field type conversion)
・ Mutual conversion of the following types.
・ String
・ Boolean
・ Integer
・ Long
・ Big Decimal
・ ArYoubi ⇒ Enum but special treatment.
・ Enum
・ Date
・ Calendar
・ ArDate
・ Of course, it may not be possible to convert.Returns null if conversion is not possible.Make no exception.
 */
public class Q06_00 {
  public static void main(String[] args) {
    //Show all combinations will be long, so show only some.
    Object result;
    L.p("\n==== String⇒Boolean");
    //This conversion is ArAutoValueConverter.stringTrueValues,ArAutoValueConverter.Can be changed by rewriting stringFalseValues.
    {
      result = ArAutoValueConverter.convert("TRUE",Boolean.class);
      L.p(result.getClass().getSimpleName());
      L.p(result+"");
    }
    L.p("\n==== String⇒ArYoubi");
    {
      result = ArAutoValueConverter.convert("Sun",ArYoubi.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
    L.p("\n==== String⇒Enum:Static Enum from String in the Enum(String)Need to be");
    {
      result = ArAutoValueConverter.convert("Choki",Jyanken.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
    L.p("\n==== Boolean⇒String");
    //This conversion is ArAutoValueConverter.stringTrueValues,ArAutoValueConverter.Can be changed by rewriting stringFalseValues.
    {
      result = ArAutoValueConverter.convert(new Boolean(false),String.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
    L.p("\n==== Boolean⇒Integer");
    //This conversion is ArAutoValueConverter.integerTrueValues,AutoValueConverter.Can be changed by rewriting integerFalseValues.
    {
      result = ArAutoValueConverter.convert(new Boolean(false),Integer.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
    L.p("\n==== Integer⇒ArYoubi");
    //This conversion is ArYoubi.Can be changed by rewriting intConversion
    {
      result = ArAutoValueConverter.convert(new Integer(1),ArYoubi.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
    L.p("\n==== Integer⇒Enum:Static Enum from Int to the Enum(Integer)Need to be");
    {
      result = ArAutoValueConverter.convert(new Integer(2),Jyanken.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
    L.p("\n==== ArYoubi⇒String");
    //This conversion is Youbi.defaultLang,Youbi.defaultLength,Youbi.Can be changed by rewriting the defaultBracket.
    {
      result = ArAutoValueConverter.convert(ArYoubi.TUE,String.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
    L.p("\n==== Enum⇒Integer:Integer to Int to the Enum()Need to be);");
    {
      result = ArAutoValueConverter.convert(Jyanken.Paa,Integer.class);
      L.p(result.getClass().getSimpleName());
      L.p(result.toString());
    }
  }
  static enum Jyanken {
    Guu("Goo",1),Choki("Choki",2),Paa("Par",3);
    private Jyanken(String name,int val) {
      this.name = name;
      this.val = val;
    }
    public String toString() {
      return this.name();
    }
    public int toInt() {
      return this.val;
    }
    public static Jyanken fromString(String str) {
      if ("Goo".equals(str)) { return Guu; }
      if ("Choki".equals(str)) { return Choki; }
      if ("Par".equals(str)) { return Paa; }
      return null;
    }
    public static Jyanken fromInt(Integer v) {
      if (v == 1) { return Guu; }
      if (v == 2) { return Choki; }
      if (v == 3) { return Paa; }
      return null;
    }
    private String name;
    private int val;
  }
}
The result is as follows.
result.ext
==== String⇒Boolean
Boolean
true
==== String⇒ArYoubi
ArYoubi
(Day)
==== String⇒Enum:Static Enum from String in the Enum(String)Need to be
Jyanken
Choki
==== Boolean⇒String
String
FALSE
==== Boolean⇒Integer
Integer
0
==== Integer⇒ArYoubi
ArYoubi
(Month)
==== Integer⇒Enum:Static Enum from Int to the Enum(Integer)Need to be
Jyanken
Choki
==== ArYoubi⇒String
String
(fire)
==== Enum⇒Integer:Integer to Int to the Enum()Need to be);
Integer
3
        Recommended Posts