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; version 2 of the License.
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.
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.
18 package org.altusmetrum.altoslib_5;
20 public abstract class AltosUnits {
22 public abstract double value(double v, boolean imperial_units);
24 public abstract double inverse(double v, boolean imperial_units);
26 public abstract String show_units(boolean imperial_units);
28 public abstract String say_units(boolean imperial_units);
30 public abstract int show_fraction(int width, boolean imperial_units);
32 public double parse(String s, boolean imperial_units) throws NumberFormatException {
33 double v = Double.parseDouble(s);
34 return inverse(v, imperial_units);
37 public double parse(String s) throws NumberFormatException {
38 return parse(s, AltosConvert.imperial_units);
41 public double value(double v) {
42 return value(v, AltosConvert.imperial_units);
45 public double inverse(double v) {
46 return inverse(v, AltosConvert.imperial_units);
49 public String show_units() {
50 return show_units(AltosConvert.imperial_units);
53 public String say_units() {
54 return say_units(AltosConvert.imperial_units);
57 public int show_fraction(int width) {
58 return show_fraction(width, AltosConvert.imperial_units);
61 int say_fraction(boolean imperial_units) {
65 private String show_format(int width, boolean imperial_units) {
66 return String.format("%%%d.%df %s", width, show_fraction(width, imperial_units), show_units(imperial_units));
69 private String say_format(boolean imperial_units) {
70 return String.format("%%1.%df", say_fraction(imperial_units));
73 private String say_units_format(boolean imperial_units) {
74 return String.format("%%1.%df %s", say_fraction(imperial_units), say_units(imperial_units));
77 public String graph_format(int width, boolean imperial_units) {
78 return String.format(String.format("%%%d.%df", width, show_fraction(width, imperial_units)), 0.0);
81 public String graph_format(int width) {
82 return graph_format(width, AltosConvert.imperial_units);
85 public String show(int width, double v, boolean imperial_units) {
86 return String.format(show_format(width, imperial_units), value(v, imperial_units));
89 public String show(int width, double v) {
90 return show(width, v, AltosConvert.imperial_units);
93 public String say(double v, boolean imperial_units) {
94 return String.format(say_format(imperial_units), value(v, imperial_units));
97 public String say(double v) {
98 return say(v, AltosConvert.imperial_units);
101 public String say_units(double v, boolean imperial_units) {
102 return String.format(say_units_format(imperial_units), value(v, imperial_units));
105 public String say_units(double v) {
106 return say_units(v, AltosConvert.imperial_units);