2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_14;
23 public abstract class AltosUnits {
25 AltosUnitsRange[] range_metric, range_imperial;
27 private AltosUnitsRange range(double v, boolean imperial_units) {
28 AltosUnitsRange[] ranges = imperial_units ? range_imperial : range_metric;
30 for (int i = ranges.length - 1; i > 0; i--)
31 if (v >= ranges[i].lower_limit)
36 private AltosUnitsRange first_range(boolean imperial_units) {
37 return imperial_units ? range_imperial[0] : range_metric[0];
40 public abstract double value(double v, boolean imperial_units);
42 public abstract double inverse(double v, boolean imperial_units);
44 public String string_value(double v, boolean imperial_units) {
45 return Double.valueOf(value(v, imperial_units)).toString();
48 public abstract String show_units(boolean imperial_units);
50 public abstract String say_units(boolean imperial_units);
52 public abstract int show_fraction(int width, boolean imperial_units);
54 private double value(double v) {
55 return value(v, AltosConvert.imperial_units);
58 private double inverse(double v) {
59 return inverse(v, AltosConvert.imperial_units);
62 private String show_units() {
63 return show_units(AltosConvert.imperial_units);
66 private String say_units() {
67 return say_units(AltosConvert.imperial_units);
70 private int show_fraction(int width) {
71 return show_fraction(width, AltosConvert.imperial_units);
74 private int say_fraction(boolean imperial_units) {
78 private String show_format(AltosUnitsRange range, int width) {
79 return String.format("%%%d.%df %s", width, range.show_fraction(width), range.show_units);
82 private String say_format(AltosUnitsRange range) {
83 return String.format("%%1.%df", range.say_fraction());
86 private String say_units_format(AltosUnitsRange range) {
87 return String.format("%%1.%df %s", range.say_fraction(), range.say_units);
90 public String show(int width, double v, boolean imperial_units) {
91 AltosUnitsRange range = range(v, imperial_units);
93 return String.format(show_format(range, width), range.value(v));
96 public String say(double v, boolean imperial_units) {
97 AltosUnitsRange range = range(v, imperial_units);
99 return String.format(say_format(range), range.value(v));
102 public String say_units(double v, boolean imperial_units) {
103 AltosUnitsRange range = range(v, imperial_units);
105 return String.format(say_units_format(range), range.value(v));
108 public String show(int width, double v) {
109 return show(width, v, AltosConvert.imperial_units);
112 public String say(double v) {
113 return say(v, AltosConvert.imperial_units);
116 public String say_units(double v) {
117 return say_units(v, AltosConvert.imperial_units);
120 public String string_value(double v) {
121 return string_value(v, AltosConvert.imperial_units);
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;
129 public String parse_units() {
130 return parse_units(AltosConvert.imperial_units);
133 public double parse_value(double v, boolean imperial_units) {
134 return first_range(imperial_units).value(v);
137 public double parse_value(double v) {
138 return parse_value(v, AltosConvert.imperial_units);
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;
146 public String graph_units() {
147 return graph_units(AltosConvert.imperial_units);
150 public double graph_value(double v, boolean imperial_units) {
151 return first_range(imperial_units).value(v);
154 public double graph_value(double v) {
155 return graph_value(v, AltosConvert.imperial_units);
158 private String graph_format(AltosUnitsRange range, int width) {
159 return String.format(String.format("%%%d.%df", width, range.show_fraction(width)), 0.0);
162 public String graph_format(int width, boolean imperial_units) {
163 return graph_format(first_range(imperial_units), width);
166 public String graph_format(int width) {
167 return graph_format(width, AltosConvert.imperial_units);
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);
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);
181 public double parse_locale(String s) throws ParseException {
182 return parse_locale(s, AltosConvert.imperial_units);
185 public double parse_net(String s) throws ParseException {
186 return parse_net(s, AltosConvert.imperial_units);
189 public AltosUnits() {
190 range_metric = new AltosUnitsRange[1];
192 range_metric[0] = new AltosUnitsRange(this, false) {
193 double value(double v) {
194 return units.value(v, false);
197 int show_fraction(int width) {
198 return units.show_fraction(width, false);
202 return units.say_fraction(false);
206 range_imperial = new AltosUnitsRange[1];
208 range_imperial[0] = new AltosUnitsRange(this, true) {
209 double value(double v) {
210 return units.value(v, true);
213 int show_fraction(int width) {
214 return units.show_fraction(width, true);
218 return units.say_fraction(true);