6b4fff0c2b0a80bd928f1c66562b7236516f014f
[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_8;
19
20 import java.text.*;
21
22 public abstract class AltosUnits {
23
24         public abstract double value(double v, boolean imperial_units);
25
26         public abstract double inverse(double v, boolean imperial_units);
27
28         public abstract String show_units(boolean imperial_units);
29
30         public abstract String say_units(boolean imperial_units);
31
32         public abstract int show_fraction(int width, boolean imperial_units);
33
34         public double parse_locale(String s, boolean imperial_units) throws ParseException {
35                 double v = AltosParse.parse_double_locale(s);
36                 return inverse(v, imperial_units);
37         }
38
39         public double parse_net(String s, boolean imperial_units) throws ParseException {
40                 double v = AltosParse.parse_double_net(s);
41                 return inverse(v, imperial_units);
42         }
43
44         public double parse_locale(String s) throws ParseException {
45                 return parse_locale(s, AltosConvert.imperial_units);
46         }
47
48         public double parse_net(String s) throws ParseException {
49                 return parse_net(s, AltosConvert.imperial_units);
50         }
51
52         public double value(double v) {
53                 return value(v, AltosConvert.imperial_units);
54         }
55
56         public double inverse(double v) {
57                 return inverse(v, AltosConvert.imperial_units);
58         }
59
60         public String show_units() {
61                 return show_units(AltosConvert.imperial_units);
62         }
63
64         public String say_units() {
65                 return say_units(AltosConvert.imperial_units);
66         }
67
68         public int show_fraction(int width) {
69                 return show_fraction(width, AltosConvert.imperial_units);
70         }
71
72         int say_fraction(boolean imperial_units) {
73                 return 0;
74         }
75
76         private String show_format(int width, boolean imperial_units) {
77                 return String.format("%%%d.%df %s", width, show_fraction(width, imperial_units), show_units(imperial_units));
78         }
79
80         private String say_format(boolean imperial_units) {
81                 return String.format("%%1.%df", say_fraction(imperial_units));
82         }
83
84         private String say_units_format(boolean imperial_units) {
85                 return String.format("%%1.%df %s", say_fraction(imperial_units), say_units(imperial_units));
86         }
87
88         public String graph_format(int width, boolean imperial_units) {
89                 return String.format(String.format("%%%d.%df", width, show_fraction(width, imperial_units)), 0.0);
90         }
91
92         public String graph_format(int width) {
93                 return graph_format(width, AltosConvert.imperial_units);
94         }
95
96         public String show(int width, double v, boolean imperial_units) {
97                 return String.format(show_format(width, imperial_units), value(v, imperial_units));
98         }
99
100         public String show(int width, double v) {
101                 return show(width, v, AltosConvert.imperial_units);
102         }
103
104         public String say(double v, boolean imperial_units) {
105                 return String.format(say_format(imperial_units), value(v, imperial_units));
106         }
107
108         public String say(double v) {
109                 return say(v, AltosConvert.imperial_units);
110         }
111
112         public String say_units(double v, boolean imperial_units) {
113                 return String.format(say_units_format(imperial_units), value(v, imperial_units));
114         }
115
116         public String say_units(double v) {
117                 return say_units(v, AltosConvert.imperial_units);
118         }
119 }