enum factory Map.
static final Map<String, Period> lookup = new HashMap<>();
enum Period {
Month("month") {
@Override
void process(int x, int y, double v) {
System.out.println(this + "-process(" + x + "," + y + "," + v + ")");
}
},
Year("year") {
@Override
void process(int x, int y, double v) {
System.out.println(this + "-process(" + x + "," + y + "," + v + ")");
}
},
Quarter("quarter") {
@Override
void process(int x, int y, double v) {
System.out.println(this + "-process(" + x + "," + y + "," + v + ")");
}
},
HalfYear("half-year") {
@Override
void process(int x, int y, double v) {
System.out.println(this + "-process(" + x + "," + y + "," + v + ")");
}
};
Period(String inData) {
lookup.put(inData, this);
}
abstract void process(int x, int y, double v);
static void process(String data) {
String[] parts = data.split(";");
Period p = lookup.get(parts[3]);
if (p != null) {
p.process(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Double.parseDouble(parts[2]));
}
}
}
public void test() {
String[] test = {"650;0;1.5;month",
"614;0;2.88;year",
"466;0;2.48;week",
"716;0;4.6;half-year",
"718;0;2.6;quarter",};
for (String s : test) {
Period.process(s);
}
}
:
Month-process(650,0,1.5)
Year-process(614,0,2.88)
HalfYear-process(716,0,4.6)
Quarter-process(718,0,2.6)
, if, , - .