1 package com.github.mygreen.cellformatter.number;
2
3
4
5
6
7
8
9
10
11 public abstract class NumberFactory {
12
13
14
15
16
17 public static NumberFactory nativeNumber() {
18 return new NumberFactory() {
19
20 @Override
21 public FormattedNumber create(double value) {
22 return new NativeNumber(value);
23 }
24 };
25 }
26
27
28
29
30
31
32
33
34 public static DecimalNumberFactory decimalNumber(int scale, boolean useSeparator, final int permilles) {
35 return new DecimalNumberFactory(scale, useSeparator, permilles);
36 }
37
38
39
40
41
42
43
44
45 public static PercentNumberFactory percentNumber(int scale, boolean useSeparator, final int permilles) {
46 return new PercentNumberFactory(scale, useSeparator, permilles);
47 }
48
49
50
51
52
53
54
55 public static ExponentNumberFactory exponentNumber(int scale, boolean useSeparator) {
56 return new ExponentNumberFactory(scale, useSeparator);
57 }
58
59
60
61
62
63
64
65
66 public static FractionNumberFactory fractionNumber(final int denominator,
67 final boolean exactDenom, final boolean wholeType) {
68 return new FractionNumberFactory(denominator, exactDenom, wholeType);
69 }
70
71
72
73
74
75
76 public abstract FormattedNumber create(double value);
77
78
79
80
81
82 public static class DecimalNumberFactory extends NumberFactory {
83
84 private int scale;
85
86 private boolean useSeparator;
87
88 private int permilles;
89
90 public DecimalNumberFactory(final int scale, final boolean useSeparator, final int permilles) {
91 this.scale = scale;
92 this.useSeparator = useSeparator;
93 this.permilles = permilles;
94 }
95
96 @Override
97 public FormattedNumber create(double value) {
98 FormattedNumber number = new DecimalNumber(value, scale, permilles);
99 number.setUseSeparator(useSeparator);
100 return number;
101 }
102
103 }
104
105
106
107
108
109 public static class ExponentNumberFactory extends NumberFactory {
110
111 private int scale;
112
113 private boolean useSeparator;
114
115 public ExponentNumberFactory(final int scale, final boolean useSeparator) {
116 this.scale = scale;
117 this.useSeparator = useSeparator;
118 }
119
120 @Override
121 public FormattedNumber create(double value) {
122 FormattedNumber number = new ExponentNumber(value, scale);
123 number.setUseSeparator(useSeparator);
124 return number;
125 }
126
127 }
128
129
130
131
132
133 public static class PercentNumberFactory extends NumberFactory {
134
135 private int scale;
136
137 private boolean useSeparator;
138
139 private int permilles;
140
141 public PercentNumberFactory(final int scale, final boolean useSeparator, final int permilles) {
142 this.scale = scale;
143 this.useSeparator = useSeparator;
144 this.permilles = permilles;
145 }
146
147 @Override
148 public FormattedNumber create(double value) {
149 FormattedNumber number = new PercentNumber(value, scale, permilles);
150 number.setUseSeparator(useSeparator);
151 return number;
152 }
153
154 }
155
156
157
158
159
160 public static class FractionNumberFactory extends NumberFactory {
161
162
163 private int denominator;
164
165
166 private boolean exactDenom;
167
168
169 private boolean wholeType;
170
171 public FractionNumberFactory(final int denominator, final boolean exactDenom, final boolean wholeType) {
172 this.denominator = denominator;
173 this.exactDenom = exactDenom;
174 this.wholeType = wholeType;
175 }
176
177 @Override
178 public FormattedNumber create(double value) {
179
180 FractionNumber number;
181 if(exactDenom) {
182 number = FractionNumber.createExactDenominator(value, denominator, wholeType);
183 } else {
184 number = FractionNumber.createMaxDenominator(value, denominator, wholeType);
185 }
186
187 return number;
188 }
189
190
191
192 }
193
194 }