1 | /*
|
---|
2 | Copyright 2006 Jerry Huxtable
|
---|
3 |
|
---|
4 | Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | you may not use this file except in compliance with the License.
|
---|
6 | You may obtain a copy of the License at
|
---|
7 |
|
---|
8 | http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 |
|
---|
10 | Unless required by applicable law or agreed to in writing, software
|
---|
11 | distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | See the License for the specific language governing permissions and
|
---|
14 | limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | package com.jhlabs.map;
|
---|
18 |
|
---|
19 | import java.io.*;
|
---|
20 | import java.text.*;
|
---|
21 |
|
---|
22 | public class Unit implements Serializable {
|
---|
23 |
|
---|
24 | static final long serialVersionUID = -6704954923429734628L;
|
---|
25 |
|
---|
26 | public final static int ANGLE_UNIT = 0;
|
---|
27 | public final static int LENGTH_UNIT = 1;
|
---|
28 | public final static int AREA_UNIT = 2;
|
---|
29 | public final static int VOLUME_UNIT = 3;
|
---|
30 |
|
---|
31 | public String name, plural, abbreviation;
|
---|
32 | public double value;
|
---|
33 | public static NumberFormat format;
|
---|
34 |
|
---|
35 | static {
|
---|
36 | format = NumberFormat.getNumberInstance();
|
---|
37 | format.setMaximumFractionDigits(2);
|
---|
38 | format.setGroupingUsed(false);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public Unit(String name, String plural, String abbreviation, double value) {
|
---|
42 | this.name = name;
|
---|
43 | this.plural = plural;
|
---|
44 | this.abbreviation = abbreviation;
|
---|
45 | this.value = value;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public double toBase(double n) {
|
---|
49 | return n * value;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public double fromBase(double n) {
|
---|
53 | return n / value;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public double parse(String s) throws NumberFormatException {
|
---|
57 | try {
|
---|
58 | return format.parse(s).doubleValue();
|
---|
59 | }
|
---|
60 | catch (java.text.ParseException e) {
|
---|
61 | throw new NumberFormatException(e.getMessage());
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public String format(double n) {
|
---|
66 | return format.format(n)+" "+abbreviation;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public String format(double n, boolean abbrev) {
|
---|
70 | if (abbrev)
|
---|
71 | return format.format(n)+" "+abbreviation;
|
---|
72 | return format.format(n);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public String format(double x, double y, boolean abbrev) {
|
---|
76 | if (abbrev)
|
---|
77 | return format.format(x)+"/"+format.format(y)+" "+abbreviation;
|
---|
78 | return format.format(x)+"/"+format.format(y);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public String format(double x, double y) {
|
---|
82 | return format(x, y, true);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public String toString() {
|
---|
86 | return plural;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public boolean equals(Object o) {
|
---|
90 | if (o instanceof Unit) {
|
---|
91 | return ((Unit)o).value == value;
|
---|
92 | }
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|