1 /**
2 * Copyright 2009 Timothy Johnston Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
3 * file except in compliance with the License. You may obtain a copy of the License at
4 *
5 * http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 * specific language governing permissions and limitations under the License.
10 */
11 package com.timjohnstondev.unitconverter.model;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 /**
17 * A collection of {@link Property}s and their unit symbols/names from the data source.
18 */
19 public class PropertyList extends ArrayList <Property>
20 {
21 /**
22 * Returns a list of all the {@link Property} names in the data source. This list is organized by the order the
23 * properties appear in the data source.
24 *
25 * @return a list of the {@code Property} names
26 */
27 public final List <String> getPropertyNames()
28 {
29 final List <String> propertyNames = new ArrayList <String>();
30 for (Property property : this)
31 {
32 propertyNames.add(property.getName());
33 }
34 return propertyNames;
35 }
36
37 /**
38 * Returns the {@link Conversion} factor or formula for the given {@link Property} for going from one set of units to
39 * another.
40 *
41 * @param propertyName the name of the {@code Property} that will be converted, such as temperature, pressure or
42 * length
43 * @param fromSymbol the original units
44 * @param toSymbol the desired units
45 * @return the {@code Conversion} factor or formula to perform the conversion
46 */
47 public final String getConversion(final String propertyName, final String fromSymbol, final String toSymbol)
48 {
49 final Property property = getPropertyByName(propertyName);
50 return property.getConversion(fromSymbol, toSymbol);
51 }
52
53 /**
54 * Returns a list of all the symbols for the given {@link Property} in the data source. The order of the symbols is
55 * determined by the underlying {@code Property}. This list order was initially based on the order the symbols appear
56 * in the data source for the {@code Property}.
57 *
58 * @param propertyName the name of the {@code Property} that is the basis for the units
59 * @return a list of the symbols
60 */
61 public final List <String> getSymbols(final String propertyName)
62 {
63 final Property property = getPropertyByName(propertyName);
64 return property.getSymbols();
65 }
66
67 /**
68 * Returns a list, minus the symbol provided, of the symbols for the given {@link Property} in the data source. The
69 * order of the symbols is determined by the underlying {@code Property}. This list order was initially based on the
70 * order the symbols appear in the data source for this {@code Property}.
71 *
72 * @param property the name of the {@code Property} that is the basis for the units
73 * @param fromSymbol the symbol to leave out of the list
74 * @return a list of the symbols
75 */
76 public final List <String> getSymbols(final String property, final String fromSymbol)
77 {
78 final List <String> symbols = getSymbols(property);
79 symbols.remove(fromSymbol);
80 return symbols;
81 }
82
83 /**
84 * Returns this symbol's name in unabbreviated words for this {@link Property}.
85 *
86 * @param propertyName the name of the {@code Property} that is the basis for this unit
87 * @param unitSymbol the symbol that represents the returned name
88 * @return the name for this symbol
89 */
90 public final String getUnitName(final String propertyName, final String unitSymbol)
91 {
92 final Property property = getPropertyByName(propertyName);
93 return property.getUnitName(unitSymbol);
94 }
95
96 private Property getPropertyByName(final String propertyName)
97 {
98 Property property = null;
99 for (Property prop : this)
100 {
101 if (propertyName.equals(prop.getName()))
102 {
103 property = prop;
104 break;
105 }
106 }
107 return property;
108 }
109
110 /**
111 * Updates the unit separator symbol
112 *
113 * @param symbol the new unit separator symbol
114 */
115 public final void setUnitSeparator(final String symbol)
116 {
117 for (Property prop : this)
118 {
119 prop.setUnitSeparator(symbol);
120 }
121 }
122
123 /**
124 * Returns {@code true} if this {@code Property} can do mole to mass and mass to mole unit conversions, else {@code
125 * false}
126 *
127 * @param propertyName name of the property
128 * @return {@code true} if this {@code Property} can do mole to mass and mass to mole unit conversions, else {@code
129 * false}
130 */
131 public final boolean usesMoles(final String propertyName)
132 {
133 final Property property = getPropertyByName(propertyName);
134 return property.usesMoles();
135 }
136 }