1 package com.github.mygreen.cellformatter;
2
3 import java.util.List;
4 import java.util.Locale;
5 import java.util.concurrent.CopyOnWriteArrayList;
6
7 import com.github.mygreen.cellformatter.callback.Callback;
8 import com.github.mygreen.cellformatter.number.FormattedNumber;
9 import com.github.mygreen.cellformatter.number.NumberFactory;
10 import com.github.mygreen.cellformatter.term.NumberTerm;
11 import com.github.mygreen.cellformatter.term.Term;
12 import com.github.mygreen.cellformatter.tokenizer.Token;
13
14
15
16
17
18
19
20
21 public class ConditionNumberFormatter extends ConditionFormatter {
22
23
24
25
26 private List<Term<FormattedNumber>> terms = new CopyOnWriteArrayList<>();
27
28
29
30
31 private NumberFactory numberFactory;
32
33 public ConditionNumberFormatter(final String pattern) {
34 super(pattern);
35 }
36
37 @Override
38 public FormatterType getType() {
39 return FormatterType.Number;
40 }
41
42 @Override
43 public boolean isMatch(final CommonCell cell) {
44 if(!cell.isNumber()) {
45 return false;
46 }
47
48 final double value = cell.getNumberCellValue();
49 return getOperator().isMatch(value);
50 }
51
52 @SuppressWarnings({"unchecked", "rawtypes"})
53 @Override
54 public CellFormatResult format(final CommonCell cell, final Locale runtimeLocale) {
55
56 final double number = cell.getNumberCellValue();
57 final FormattedNumber numObj = numberFactory.create(number);
58
59 final StringBuilder sb = new StringBuilder();
60 if(getOperator().equals(ConditionOperator.ALL) && numObj.isNegative()) {
61
62 sb.append("-");
63 }
64
65 for(Term<FormattedNumber> term : terms) {
66 sb.append(term.format(numObj, getLocale(), runtimeLocale));
67 }
68
69
70
71
72 String value = sb.toString();
73 for(Callback callback : getCallbacks()) {
74
75 final Locale locale;
76 if(getLocale() != null) {
77 locale = getLocale().getLocale();
78 } else {
79 locale = runtimeLocale;
80 }
81
82 if(!callback.isApplicable(locale)) {
83 continue;
84 }
85
86 value = callback.call(number, value, locale, null);
87 }
88
89 final CellFormatResultormatResult.html#CellFormatResult">CellFormatResult result = new CellFormatResult();
90 result.setValue(number);
91 result.setText(value);
92 result.setTextColor(getColor());
93 result.setSectionPattern(getPattern());
94 result.setCellType(FormatCellType.Number);
95
96 return result;
97 }
98
99
100
101
102
103 public void addTerm(final Term<FormattedNumber> term) {
104 this.terms.add(term);
105 }
106
107
108
109
110
111 public List<Term<FormattedNumber>> getTerms() {
112 return terms;
113 }
114
115
116
117
118
119
120 public boolean containsSymbolTerm(Token.Symbol symbol) {
121
122 for(Term<FormattedNumber> term : terms) {
123 if(!(term instanceof NumberTerm.SymbolTerm)) {
124 continue;
125 }
126
127 final NumberTerm.SymbolTerm symbolTerm = (NumberTerm.SymbolTerm) term;
128 if(symbolTerm.getToken().equals(symbol)) {
129 return true;
130 }
131 }
132
133 return false;
134
135 }
136
137 public NumberFactory getNumberFactory() {
138 return numberFactory;
139 }
140
141 public void setNumberFactory(NumberFactory numberFactory) {
142 this.numberFactory = numberFactory;
143 }
144 }