1
2
3
4
5
6
7
8
9
10
11
12 package com.timjohnstondev.unitconverter.model;
13
14 import java.math.BigDecimal;
15 import java.math.RoundingMode;
16 import java.util.ArrayList;
17 import java.util.List;
18
19
20
21
22
23 public class Property implements Comparable <Property>
24 {
25 private int order;
26 private String name;
27 private List <Conversion> conversions = new ArrayList <Conversion>();
28 private boolean hasFormula;
29 private boolean usesMoles;
30
31
32
33
34
35
36
37
38 public Property(final String newName, final boolean isFormulaBased, final boolean canUseMoles)
39 {
40 name = newName;
41 hasFormula = isFormulaBased;
42 usesMoles = canUseMoles;
43 }
44
45
46
47
48
49
50 public final int getOrder()
51 {
52 return order;
53 }
54
55
56
57
58
59
60 public final void setOrder(final int newOrder)
61 {
62 order = newOrder;
63 }
64
65
66
67
68
69
70 public final String getName()
71 {
72 return name;
73 }
74
75
76
77
78
79
80
81
82 public final boolean usesMoles()
83 {
84 return usesMoles;
85 }
86
87
88
89
90
91
92 public final boolean hasFormula()
93 {
94 return hasFormula;
95 }
96
97
98
99
100
101
102 public final void addConversion(final Conversion conversion)
103 {
104 conversions.add(conversion);
105 }
106
107
108
109
110
111
112 final List <String> getSymbols()
113 {
114 final List <String> symbols = new ArrayList <String>();
115 for (Conversion conversion : conversions)
116 {
117 if (!symbols.contains(conversion.getFormattedUnitSymbol()))
118 {
119 symbols.add(conversion.getFormattedUnitSymbol());
120 }
121 }
122 return symbols;
123 }
124
125
126
127
128
129
130
131
132
133 final String getConversion(final String fromSymbol, final String toSymbol)
134 {
135 String result = "";
136 if (hasFormula)
137 {
138 result = getFormula(fromSymbol, toSymbol);
139 }
140 else
141 {
142 final BigDecimal fromFactor = new BigDecimal(getConversion(fromSymbol));
143 final BigDecimal toFactor = new BigDecimal(getConversion(toSymbol));
144 final BigDecimal quotient = toFactor.divide(fromFactor, 30, RoundingMode.HALF_UP);
145 result = quotient.stripTrailingZeros().toPlainString();
146 }
147 return result;
148 }
149
150
151
152
153
154
155
156 final String getUnitName(final String unitSymbol)
157 {
158 String unitName = "";
159 for (Conversion conversion : conversions)
160 {
161 if (unitSymbol.equals(conversion.getFormattedUnitSymbol()))
162 {
163 unitName = conversion.getUnitName();
164 }
165 }
166 return unitName;
167 }
168
169 private String getFormula(final String fromSymbol, final String toSymbol)
170 {
171 String formula = "";
172 for (Conversion conversion : conversions)
173 {
174 final String fromUnitSymbol = conversion.getUnitSymbol();
175 final String toUnitSymbol = ((ConversionFormula) conversion).getToUnitSymbol();
176 if (fromSymbol.equals(fromUnitSymbol) && toSymbol.equals(toUnitSymbol))
177 {
178 formula = conversion.getConversion();
179 break;
180 }
181 }
182 return formula;
183 }
184
185 private String getConversion(final String symbol)
186 {
187 String value = "";
188 for (Conversion conversion : conversions)
189 {
190 if (symbol.equals(conversion.getFormattedUnitSymbol()))
191 {
192 value = conversion.getConversion();
193 break;
194 }
195 }
196 return value;
197 }
198
199
200
201
202
203
204 public final void setUnitSeparator(final String symbol)
205 {
206 for (Conversion conversion : conversions)
207 {
208 conversion.setUnitSeparator(symbol);
209 }
210 }
211
212 @Override
213 public final int compareTo(final Property prop)
214 {
215 return order - prop.getOrder();
216 }
217 }