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_11;
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 abstract String show_units(boolean imperial_units);
46 public abstract String say_units(boolean imperial_units);
48 public abstract int show_fraction(int width, boolean imperial_units);
50 private double value(double v) {
51 return value(v, AltosConvert.imperial_units);
54 private double inverse(double v) {
55 return inverse(v, AltosConvert.imperial_units);
58 private String show_units() {
59 return show_units(AltosConvert.imperial_units);
62 private String say_units() {
63 return say_units(AltosConvert.imperial_units);
66 private int show_fraction(int width) {
67 return show_fraction(width, AltosConvert.imperial_units);
70 private int say_fraction(boolean imperial_units) {
74 private String show_format(AltosUnitsRange range, int width) {
75 return String.format("%%%d.%df %s", width, range.show_fraction(width), range.show_units);
78 private String say_format(AltosUnitsRange range) {
79 return String.format("%%1.%df", range.say_fraction());
82 private String say_units_format(AltosUnitsRange range) {
83 return String.format("%%1.%df %s", range.say_fraction(), range.say_units);
86 public String show(int width, double v, boolean imperial_units) {
87 AltosUnitsRange range = range(v, imperial_units);
89 return String.format(show_format(range, width), range.value(v));
92 public String say(double v, boolean imperial_units) {
93 AltosUnitsRange range = range(v, imperial_units);
95 return String.format(say_format(range), range.value(v));
98 public String say_units(double v, boolean imperial_units) {
99 AltosUnitsRange range = range(v, imperial_units);
101 return String.format(say_units_format(range), range.value(v));
104 public String show(int width, double v) {
105 return show(width, v, AltosConvert.imperial_units);
108 public String say(double v) {
109 return say(v, AltosConvert.imperial_units);
112 public String say_units(double v) {
113 return say_units(v, AltosConvert.imperial_units);
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;
121 public String parse_units() {
122 return parse_units(AltosConvert.imperial_units);
125 public double parse_value(double v, boolean imperial_units) {
126 return first_range(imperial_units).value(v);
129 public double parse_value(double v) {
130 return parse_value(v, AltosConvert.imperial_units);
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;
138 public String graph_units() {
139 return graph_units(AltosConvert.imperial_units);
142 public double graph_value(double v, boolean imperial_units) {
143 return first_range(imperial_units).value(v);
146 public double graph_value(double v) {
147 return graph_value(v, AltosConvert.imperial_units);
150 private String graph_format(AltosUnitsRange range, int width) {
151 return String.format(String.format("%%%d.%df", width, range.show_fraction(width)), 0.0);
154 public String graph_format(int width, boolean imperial_units) {
155 return graph_format(first_range(imperial_units), width);
158 public String graph_format(int width) {
159 return graph_format(width, AltosConvert.imperial_units);
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);
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);
173 public double parse_locale(String s) throws ParseException {
174 return parse_locale(s, AltosConvert.imperial_units);
177 public double parse_net(String s) throws ParseException {
178 return parse_net(s, AltosConvert.imperial_units);
181 public AltosUnits() {
182 range_metric = new AltosUnitsRange[1];
184 range_metric[0] = new AltosUnitsRange(this, false) {
185 double value(double v) {
186 return units.value(v, false);
189 int show_fraction(int width) {
190 return units.show_fraction(width, false);
194 return units.say_fraction(false);
198 range_imperial = new AltosUnitsRange[1];
200 range_imperial[0] = new AltosUnitsRange(this, true) {
201 double value(double v) {
202 return units.value(v, true);
205 int show_fraction(int width) {
206 return units.show_fraction(width, true);
210 return units.say_fraction(true);