1 package com.github.mygreen.cellformatter;
2
3 import java.util.List;
4
5 import com.github.mygreen.cellformatter.tokenizer.CustomFormatTokenizer;
6 import com.github.mygreen.cellformatter.tokenizer.Token;
7 import com.github.mygreen.cellformatter.tokenizer.TokenStore;
8
9
10
11
12
13
14
15
16
17 public class CustomFormatterFactory {
18
19 private ConditionTextFormatterFactoryml#ConditionTextFormatterFactory">ConditionTextFormatterFactory textFormatterFactory = new ConditionTextFormatterFactory();
20
21 private ConditionDateFormatterFactoryml#ConditionDateFormatterFactory">ConditionDateFormatterFactory dateFormatterFactory = new ConditionDateFormatterFactory();
22
23 private ConditionNumberFormatterFactory#ConditionNumberFormatterFactory">ConditionNumberFormatterFactory numberFormatterFactory = new ConditionNumberFormatterFactory();
24
25
26
27
28
29
30
31 public CustomFormatter create(final String pattern) {
32
33 final CustomFormatTokenizertomFormatTokenizer.html#CustomFormatTokenizer">CustomFormatTokenizer tokenizer = new CustomFormatTokenizer();
34 final TokenStore allStore = tokenizer.parse(pattern);
35 if(allStore.getTokens().isEmpty()) {
36
37 return CustomFormatter.DEFAULT_FORMATTER;
38
39 } else if(pattern.equalsIgnoreCase("General")) {
40 return CustomFormatter.DEFAULT_FORMATTER;
41 }
42
43
44 final List<TokenStore> sections = allStore.split(Token.SYMBOL_SEMI_COLON);
45 if(sections.size() > 4) {
46 throw new CustomFormatterParseException(pattern,
47 String.format("section size over 4. but '%s' number of %d secitions.", pattern, sections.size()));
48 }
49
50 final CustomFormatterormatter.html#CustomFormatter">CustomFormatter formatter = new CustomFormatter(pattern);
51 boolean containsTextFormatter = false;
52 for(TokenStore section : sections) {
53
54 final ConditionFormatter conditionFormatter;
55 if(textFormatterFactory.isTextPattern(section)) {
56 conditionFormatter = textFormatterFactory.create(section);
57 containsTextFormatter = true;
58
59 } else if(dateFormatterFactory.isDatePattern(section)) {
60 conditionFormatter = dateFormatterFactory.create(section);
61
62 } else if(numberFormatterFactory.isNumberPattern(section)) {
63 conditionFormatter = numberFormatterFactory.create(section);
64
65 } else {
66 conditionFormatter = numberFormatterFactory.create(section);
67
68 }
69
70 formatter.addConditionFormatter(conditionFormatter);
71
72 }
73
74
75 int sectionSize = sections.size();
76 if(containsTextFormatter) {
77
78 sectionSize--;
79 }
80
81
82 boolean hasConditionFirst = false;
83
84 for(int i=0; i < sectionSize; i++) {
85
86 final ConditionFormatter conditionFormatter = formatter.getConditionFormatters().get(i);
87 if(conditionFormatter.getOperator() != null) {
88 if(i == 0) {
89 hasConditionFirst = true;
90 }
91 continue;
92 }
93
94 if(sectionSize <= 1) {
95
96 conditionFormatter.setOperator(ConditionOperator.ALL);
97
98 } else if(sectionSize == 2) {
99 if(i==0) {
100
101 conditionFormatter.setOperator(ConditionOperator.NON_NEGATIVE);
102
103 } else if(i==1) {
104
105 if(hasConditionFirst) {
106 conditionFormatter.setOperator(ConditionOperator.ALL);
107 } else {
108 conditionFormatter.setOperator(ConditionOperator.NEGATIVE);
109 }
110 }
111
112 } else if(sectionSize == 3) {
113 if(i==0) {
114
115 conditionFormatter.setOperator(ConditionOperator.POSITIVE);
116
117 } else if(i==1) {
118
119 conditionFormatter.setOperator(ConditionOperator.NEGATIVE);
120
121 } else {
122
123 conditionFormatter.setOperator(ConditionOperator.ALL);
124
125 }
126
127 } else if(sectionSize == 4) {
128 if(i==0) {
129
130 conditionFormatter.setOperator(ConditionOperator.POSITIVE);
131
132 } else if(i==1) {
133
134 conditionFormatter.setOperator(ConditionOperator.NEGATIVE);
135
136 } else if(i==2) {
137
138 conditionFormatter.setOperator(ConditionOperator.ZERO);
139
140 } else {
141
142 conditionFormatter.setOperator(ConditionOperator.ALL);
143
144 }
145
146 } else {
147 throw new CustomFormatterParseException(pattern,
148 String.format("section size over 4. but '%s' number of %d secitions.", pattern, sections.size()));
149 }
150
151 }
152
153 return formatter;
154 }
155
156 }