Switch from GPLv2 to GPLv2+
[fw/altos] / altoslib / AltosUnits.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.text.*;
22
23 public abstract class AltosUnits {
24
25         AltosUnitsRange[]       range_metric, range_imperial;
26
27         private AltosUnitsRange range(double v, boolean imperial_units) {
28                 AltosUnitsRange[]       ranges = imperial_units ? range_imperial : range_metric;
29
30                 for (int i = ranges.length - 1; i > 0; i--)
31                         if (v >= ranges[i].lower_limit)
32                                 return ranges[i];
33                 return ranges[0];
34         }
35
36         private AltosUnitsRange first_range(boolean imperial_units) {
37                 return imperial_units ? range_imperial[0] : range_metric[0];
38         }
39
40         public abstract double value(double v, boolean imperial_units);
41
42         public abstract double inverse(double v, boolean imperial_units);
43
44         public abstract String show_units(boolean imperial_units);
45
46         public abstract String say_units(boolean imperial_units);
47
48         public abstract int show_fraction(int width, boolean imperial_units);
49
50         private double value(double v) {
51                 return value(v, AltosConvert.imperial_units);
52         }
53
54         private double inverse(double v) {
55                 return inverse(v, AltosConvert.imperial_units);
56         }
57
58         private String show_units() {
59                 return show_units(AltosConvert.imperial_units);
60         }
61
62         private String say_units() {
63                 return say_units(AltosConvert.imperial_units);
64         }
65
66         private int show_fraction(int width) {
67                 return show_fraction(width, AltosConvert.imperial_units);
68         }
69
70         private int say_fraction(boolean imperial_units) {
71                 return 0;
72         }
73
74         private String show_format(AltosUnitsRange range, int width) {
75                 return String.format("%%%d.%df %s", width, range.show_fraction(width), range.show_units);
76         }
77
78         private String say_format(AltosUnitsRange range) {
79                 return String.format("%%1.%df", range.say_fraction());
80         }
81
82         private String say_units_format(AltosUnitsRange range)  {
83                 return String.format("%%1.%df %s", range.say_fraction(), range.say_units);
84         }
85
86         public String show(int width, double v, boolean imperial_units) {
87                 AltosUnitsRange range = range(v, imperial_units);
88
89                 return String.format(show_format(range, width), range.value(v));
90         }
91
92         public String say(double v, boolean imperial_units) {
93                 AltosUnitsRange range = range(v, imperial_units);
94
95                 return String.format(say_format(range), range.value(v));
96         }
97
98         public String say_units(double v, boolean imperial_units) {
99                 AltosUnitsRange range = range(v, imperial_units);
100
101                 return String.format(say_units_format(range), range.value(v));
102         }
103
104         public String show(int width, double v) {
105                 return show(width, v, AltosConvert.imperial_units);
106         }
107
108         public String say(double v) {
109                 return say(v, AltosConvert.imperial_units);
110         }
111
112         public String say_units(double v) {
113                 return say_units(v, AltosConvert.imperial_units);
114         }
115
116         /* Parsing functions. Use the first range of the type */
117         public String parse_units(boolean imperial_units) {
118                 return first_range(imperial_units).show_units;
119         }
120
121         public String parse_units() {
122                 return parse_units(AltosConvert.imperial_units);
123         }
124
125         public double parse_value(double v, boolean imperial_units) {
126                 return first_range(imperial_units).value(v);
127         }
128
129         public double parse_value(double v) {
130                 return parse_value(v, AltosConvert.imperial_units);
131         }
132
133         /* Graphing functions. Use the first range of the type */
134         public String graph_units(boolean imperial_units) {
135                 return first_range(imperial_units).show_units;
136         }
137
138         public String graph_units() {
139                 return graph_units(AltosConvert.imperial_units);
140         }
141
142         public double graph_value(double v, boolean imperial_units) {
143                 return first_range(imperial_units).value(v);
144         }
145
146         public double graph_value(double v) {
147                 return graph_value(v, AltosConvert.imperial_units);
148         }
149
150         private String graph_format(AltosUnitsRange range, int width) {
151                 return String.format(String.format("%%%d.%df", width, range.show_fraction(width)), 0.0);
152         }
153
154         public String graph_format(int width, boolean imperial_units) {
155                 return graph_format(first_range(imperial_units), width);
156         }
157
158         public String graph_format(int width) {
159                 return graph_format(width, AltosConvert.imperial_units);
160         }
161
162         /* Parsing functions. */
163         public double parse_locale(String s, boolean imperial_units) throws ParseException {
164                 double v = AltosParse.parse_double_locale(s);
165                 return inverse(v, imperial_units);
166         }
167
168         public double parse_net(String s, boolean imperial_units) throws ParseException {
169                 double v = AltosParse.parse_double_net(s);
170                 return inverse(v, imperial_units);
171         }
172
173         public double parse_locale(String s) throws ParseException {
174                 return parse_locale(s, AltosConvert.imperial_units);
175         }
176
177         public double parse_net(String s) throws ParseException {
178                 return parse_net(s, AltosConvert.imperial_units);
179         }
180
181         public AltosUnits() {
182                 range_metric = new AltosUnitsRange[1];
183
184                 range_metric[0] = new AltosUnitsRange(this, false) {
185                                 double value(double v) {
186                                         return units.value(v, false);
187                                 }
188
189                                 int show_fraction(int width) {
190                                         return units.show_fraction(width, false);
191                                 }
192
193                                 int say_fraction() {
194                                         return units.say_fraction(false);
195                                 }
196                         };
197
198                 range_imperial = new AltosUnitsRange[1];
199
200                 range_imperial[0] = new AltosUnitsRange(this, true) {
201                                 double value(double v) {
202                                         return units.value(v, true);
203                                 }
204
205                                 int show_fraction(int width) {
206                                         return units.show_fraction(width, true);
207                                 }
208
209                                 int say_fraction() {
210                                         return units.say_fraction(true);
211                                 }
212                         };
213         }
214 }