source: osm/applications/editors/josm/plugins/epsg31287/src/com/jhlabs/map/Unit.java@ 23660

Last change on this file since 23660 was 23660, checked in by fichtennadel, 14 years ago

Initial commit of epsg31287 plugin - local version 3

  • Property svn:mime-type set to text/plain
File size: 2.3 KB
Line 
1/*
2Copyright 2006 Jerry Huxtable
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package com.jhlabs.map;
18
19import java.io.*;
20import java.text.*;
21
22public 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}
Note: See TracBrowser for help on using the repository browser.