package foundation.code;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import foundation.code.segment.DaySegment;
|
import foundation.code.segment.DynamicStringSegment;
|
import foundation.code.segment.MonthSegment;
|
import foundation.code.segment.MonthlyNoSegment;
|
import foundation.code.segment.NoSegment;
|
import foundation.code.segment.StringSegment;
|
import foundation.code.segment.YearSegment;
|
import foundation.code.segment.YearlyNoSegment;
|
import foundation.variant.expression.Segment;
|
import foundation.variant.provider.IVariantsProvider;
|
|
|
public abstract class SegmentCreator {
|
|
private static Map<String, Class<? extends SegmentCreator>> segmentCreatorMap;
|
protected CodeRule codeRule;
|
protected int size;
|
|
static {
|
initCreatorMap();
|
}
|
|
public SegmentCreator() {
|
|
}
|
|
protected void init(CodeRule rule) {
|
|
}
|
|
public static SegmentCreator getInstance(CodeRule codeRule, Segment segment) {
|
try {
|
if (!segment.isVariant()) {
|
return new StringSegment(segment);
|
}
|
else {
|
String name = segment.getName();
|
|
if (name == null) {
|
return null;
|
}
|
|
String lower = name.toLowerCase();
|
|
if (lower.startsWith("dynamic(")) {
|
String rule = name.substring("dynamic(".length(), name.length() - 1);
|
codeRule.setRule(rule);
|
|
lower = "dynamic";
|
}
|
|
Class<? extends SegmentCreator> clazz = segmentCreatorMap.get(lower);
|
|
if (clazz == null) {
|
return null;
|
}
|
|
SegmentCreator creator = clazz.newInstance();
|
creator.setCodeRule(codeRule);
|
creator.setSize(name.length());
|
|
return creator;
|
}
|
}
|
catch (Exception e) {
|
return new StringSegment(segment);
|
}
|
}
|
|
public abstract String getNextSequenceValue(String dynamic, IVariantsProvider... entitys);
|
|
public abstract String getCurrSequenceValue(String dynamic, IVariantsProvider... entitys);
|
|
public abstract String getTempValue(String dynamic, IVariantsProvider... entitys);
|
|
protected String format(String value) {
|
if (value == null) {
|
return value;
|
}
|
|
int length = value.length();
|
|
if (length > size) {
|
return value.substring(0, size);
|
}
|
else if (length == size) {
|
return value;
|
}
|
else {
|
while (value.length() < size) {
|
value = "0" + value;
|
}
|
|
return value;
|
}
|
}
|
|
private static void initCreatorMap() {
|
segmentCreatorMap = new HashMap<String, Class<? extends SegmentCreator>>();
|
|
segmentCreatorMap.put("yyyy", YearSegment.class);
|
segmentCreatorMap.put("yy", YearSegment.class);
|
segmentCreatorMap.put("mm", MonthSegment.class);
|
segmentCreatorMap.put("dd", DaySegment.class);
|
segmentCreatorMap.put("00", NoSegment.class);
|
segmentCreatorMap.put("000", NoSegment.class);
|
segmentCreatorMap.put("0000", NoSegment.class);
|
segmentCreatorMap.put("00000", NoSegment.class);
|
segmentCreatorMap.put("000000", NoSegment.class);
|
segmentCreatorMap.put("0000000", NoSegment.class);
|
segmentCreatorMap.put("00000000", NoSegment.class);
|
segmentCreatorMap.put("000000000", NoSegment.class);
|
segmentCreatorMap.put("0000000000", NoSegment.class);
|
segmentCreatorMap.put("dynamic", DynamicStringSegment.class);
|
segmentCreatorMap.put("m00", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m0000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m00000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m000000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m0000000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m00000000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m000000000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("m0000000000", MonthlyNoSegment.class);
|
segmentCreatorMap.put("y00", YearlyNoSegment.class);
|
segmentCreatorMap.put("y000", YearlyNoSegment.class);
|
segmentCreatorMap.put("y0000", YearlyNoSegment.class);
|
segmentCreatorMap.put("y00000", YearlyNoSegment.class);
|
segmentCreatorMap.put("y000000", YearlyNoSegment.class);
|
segmentCreatorMap.put("y0000000", YearlyNoSegment.class);
|
segmentCreatorMap.put("y00000000", YearlyNoSegment.class);
|
segmentCreatorMap.put("y000000000", YearlyNoSegment.class);
|
segmentCreatorMap.put("y0000000000", YearlyNoSegment.class);
|
}
|
|
public void setSize(int size) {
|
this.size = size;
|
}
|
|
protected void setCodeRule(CodeRule codeRule) {
|
this.codeRule = codeRule;
|
init(codeRule);
|
}
|
|
}
|