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