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