dbdb788248ee150826ea272732e72ddb4d693da3
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 public abstract class AltosUnits {
21
22         public abstract double value(double v, boolean imperial_units);
23
24         public abstract double inverse(double v, boolean imperial_units);
25
26         public abstract String show_units(boolean imperial_units);
27
28         public abstract String say_units(boolean imperial_units);
29
30         public abstract int show_fraction(int width, boolean imperial_units);
31
32         public double parse(String s, boolean imperial_units) throws NumberFormatException {
33                 double v = Double.parseDouble(s);
34                 return inverse(v, imperial_units);
35         }
36
37         public double parse(String s) throws NumberFormatException {
38                 return parse(s, AltosConvert.imperial_units);
39         }
40
41         public double value(double v) {
42                 return value(v, AltosConvert.imperial_units);
43         }
44
45         public double inverse(double v) {
46                 return inverse(v, AltosConvert.imperial_units);
47         }
48
49         public String show_units() {
50                 return show_units(AltosConvert.imperial_units);
51         }
52
53         public String say_units() {
54                 return say_units(AltosConvert.imperial_units);
55         }
56
57         public int show_fraction(int width) {
58                 return show_fraction(width, AltosConvert.imperial_units);
59         }
60
61         int say_fraction(boolean imperial_units) {
62                 return 0;
63         }
64
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));
67         }
68
69         private String say_format(boolean imperial_units) {
70                 return String.format("%%1.%df", say_fraction(imperial_units));
71         }
72
73         private String say_units_format(boolean imperial_units) {
74                 return String.format("%%1.%df %s", say_fraction(imperial_units), say_units(imperial_units));
75         }
76
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);
79         }
80
81         public String graph_format(int width) {
82                 return graph_format(width, AltosConvert.imperial_units);
83         }
84
85         public String show(int width, double v, boolean imperial_units) {
86                 return String.format(show_format(width, imperial_units), value(v, imperial_units));
87         }
88
89         public String show(int width, double v) {
90                 return show(width, v, AltosConvert.imperial_units);
91         }
92
93         public String say(double v, boolean imperial_units) {
94                 return String.format(say_format(imperial_units), value(v, imperial_units));
95         }
96
97         public String say(double v) {
98                 return say(v, AltosConvert.imperial_units);
99         }
100
101         public String say_units(double v, boolean imperial_units) {
102                 return String.format(say_units_format(imperial_units), value(v, imperial_units));
103         }
104
105         public String say_units(double v) {
106                 return say_units(v, AltosConvert.imperial_units);
107         }
108 }