ao-tools/ao-telem: Print out 'log_max' value. Clean up compiler warnings.
[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_13;
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 String string_value(double v, boolean imperial_units) {
45                 return new Double(value(v, imperial_units)).toString();
46         }
47
48         public abstract String show_units(boolean imperial_units);
49
50         public abstract String say_units(boolean imperial_units);
51
52         public abstract int show_fraction(int width, boolean imperial_units);
53
54         private double value(double v) {
55                 return value(v, AltosConvert.imperial_units);
56         }
57
58         private double inverse(double v) {
59                 return inverse(v, AltosConvert.imperial_units);
60         }
61
62         private String show_units() {
63                 return show_units(AltosConvert.imperial_units);
64         }
65
66         private String say_units() {
67                 return say_units(AltosConvert.imperial_units);
68         }
69
70         private int show_fraction(int width) {
71                 return show_fraction(width, AltosConvert.imperial_units);
72         }
73
74         private int say_fraction(boolean imperial_units) {
75                 return 0;
76         }
77
78         private String show_format(AltosUnitsRange range, int width) {
79                 return String.format("%%%d.%df %s", width, range.show_fraction(width), range.show_units);
80         }
81
82         private String say_format(AltosUnitsRange range) {
83                 return String.format("%%1.%df", range.say_fraction());
84         }
85
86         private String say_units_format(AltosUnitsRange range)  {
87                 return String.format("%%1.%df %s", range.say_fraction(), range.say_units);
88         }
89
90         public String show(int width, double v, boolean imperial_units) {
91                 AltosUnitsRange range = range(v, imperial_units);
92
93                 return String.format(show_format(range, width), range.value(v));
94         }
95
96         public String say(double v, boolean imperial_units) {
97                 AltosUnitsRange range = range(v, imperial_units);
98
99                 return String.format(say_format(range), range.value(v));
100         }
101
102         public String say_units(double v, boolean imperial_units) {
103                 AltosUnitsRange range = range(v, imperial_units);
104
105                 return String.format(say_units_format(range), range.value(v));
106         }
107
108         public String show(int width, double v) {
109                 return show(width, v, AltosConvert.imperial_units);
110         }
111
112         public String say(double v) {
113                 return say(v, AltosConvert.imperial_units);
114         }
115
116         public String say_units(double v) {
117                 return say_units(v, AltosConvert.imperial_units);
118         }
119
120         public String string_value(double v) {
121                 return string_value(v, AltosConvert.imperial_units);
122         }
123
124         /* Parsing functions. Use the first range of the type */
125         public String parse_units(boolean imperial_units) {
126                 return first_range(imperial_units).show_units;
127         }
128
129         public String parse_units() {
130                 return parse_units(AltosConvert.imperial_units);
131         }
132
133         public double parse_value(double v, boolean imperial_units) {
134                 return first_range(imperial_units).value(v);
135         }
136
137         public double parse_value(double v) {
138                 return parse_value(v, AltosConvert.imperial_units);
139         }
140
141         /* Graphing functions. Use the first range of the type */
142         public String graph_units(boolean imperial_units) {
143                 return first_range(imperial_units).show_units;
144         }
145
146         public String graph_units() {
147                 return graph_units(AltosConvert.imperial_units);
148         }
149
150         public double graph_value(double v, boolean imperial_units) {
151                 return first_range(imperial_units).value(v);
152         }
153
154         public double graph_value(double v) {
155                 return graph_value(v, AltosConvert.imperial_units);
156         }
157
158         private String graph_format(AltosUnitsRange range, int width) {
159                 return String.format(String.format("%%%d.%df", width, range.show_fraction(width)), 0.0);
160         }
161
162         public String graph_format(int width, boolean imperial_units) {
163                 return graph_format(first_range(imperial_units), width);
164         }
165
166         public String graph_format(int width) {
167                 return graph_format(width, AltosConvert.imperial_units);
168         }
169
170         /* Parsing functions. */
171         public double parse_locale(String s, boolean imperial_units) throws ParseException {
172                 double v = AltosParse.parse_double_locale(s);
173                 return inverse(v, imperial_units);
174         }
175
176         public double parse_net(String s, boolean imperial_units) throws ParseException {
177                 double v = AltosParse.parse_double_net(s);
178                 return inverse(v, imperial_units);
179         }
180
181         public double parse_locale(String s) throws ParseException {
182                 return parse_locale(s, AltosConvert.imperial_units);
183         }
184
185         public double parse_net(String s) throws ParseException {
186                 return parse_net(s, AltosConvert.imperial_units);
187         }
188
189         public AltosUnits() {
190                 range_metric = new AltosUnitsRange[1];
191
192                 range_metric[0] = new AltosUnitsRange(this, false) {
193                                 double value(double v) {
194                                         return units.value(v, false);
195                                 }
196
197                                 int show_fraction(int width) {
198                                         return units.show_fraction(width, false);
199                                 }
200
201                                 int say_fraction() {
202                                         return units.say_fraction(false);
203                                 }
204                         };
205
206                 range_imperial = new AltosUnitsRange[1];
207
208                 range_imperial[0] = new AltosUnitsRange(this, true) {
209                                 double value(double v) {
210                                         return units.value(v, true);
211                                 }
212
213                                 int show_fraction(int width) {
214                                         return units.show_fraction(width, true);
215                                 }
216
217                                 int say_fraction() {
218                                         return units.say_fraction(true);
219                                 }
220                         };
221         }
222 }