create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / PrintUnit.java
1 /*
2  * PrintUnit.java
3  */
4 package net.sf.openrocket.gui.print;
5
6 /**
7  * Utilities for print units.
8  */
9 public enum PrintUnit {
10     FOOT {
11         public double toInches(double d) { return d*12; }
12         public double toMillis(double d) { return d/FEET_PER_MM; }
13         public double toCentis(double d) { return d/(FEET_PER_MM*TEN); }
14         public double toMeters(double d) { return d/(FEET_PER_MM*TEN*TEN*TEN); }
15         public double toPoints(double d) { return (d * POINTS_PER_INCH * 12); }
16         public double convert(double d, PrintUnit u) { return u.toInches(d)/12; }
17     },
18     INCHES {
19         public double toInches(double d) { return d; }
20         public double toMillis(double d) { return d/INCHES_PER_MM; }
21         public double toCentis(double d) { return d/(INCHES_PER_MM*TEN); }
22         public double toMeters(double d) { return d/(INCHES_PER_MM*TEN*TEN*TEN); }
23         public double toPoints(double d) { return (d * POINTS_PER_INCH); }
24         public double convert(double d, PrintUnit u) { return u.toInches(d); }
25     },
26     MILLIMETERS {
27         public double toInches(double d) { return d * INCHES_PER_MM; }
28         public double toMillis(double d) { return d; }
29         public double toCentis(double d) { return d/TEN; }
30         public double toMeters(double d) { return d/(TEN*TEN*TEN); }
31         public double toPoints(double d) { return INCHES.toPoints(toInches(d)); }
32         public double convert(double d, PrintUnit u) { return u.toMillis(d); }
33     },
34     CENTIMETERS {
35         public double toInches(double d) { return d * INCHES_PER_MM * TEN; }
36         public double toMillis(double d) { return d * TEN; }
37         public double toCentis(double d) { return d; }
38         public double toMeters(double d) { return d/(TEN*TEN); }
39         public double toPoints(double d) { return INCHES.toPoints(toInches(d)); }
40         public double convert(double d, PrintUnit u) { return u.toCentis(d); }
41     },
42     METERS {
43         public double toInches(double d) { return d * INCHES_PER_MM * TEN * TEN * TEN; }
44         public double toMillis(double d) { return d * TEN * TEN * TEN; }
45         public double toCentis(double d) { return d * TEN * TEN; }
46         public double toMeters(double d) { return d; }
47         public double toPoints(double d) { return INCHES.toPoints(toInches(d)); }
48         public double convert(double d, PrintUnit u) { return u.toMeters(d); }
49     },
50     POINTS {
51         public double toInches(double d) { return d/POINTS_PER_INCH; }
52         public double toMillis(double d) { return d/(POINTS_PER_INCH * INCHES_PER_MM); }
53         public double toCentis(double d) { return toMillis(d)/TEN; }
54         public double toMeters(double d) { return toMillis(d)/(TEN*TEN*TEN); }
55         public double toPoints(double d) { return d; }
56         public double convert(double d, PrintUnit u) { return u.toPoints(d); }
57     };
58
59     // Handy constants for conversion methods
60     public static final double INCHES_PER_MM = 0.0393700787d;
61     public static final double FEET_PER_MM = INCHES_PER_MM /12;
62     public static final double MM_PER_INCH = 1.0d/INCHES_PER_MM;
63     public static final long TEN = 10;
64     /**
65      * PPI is Postscript Point and is a standard of 72.  Java2D also uses this internally as a pixel-per-inch, so pixels
66      * and points are for the most part interchangeable (unless the defaults are changed), which makes translating
67      * between the screen and a print job easier.
68      *
69      * Not to be confused with Dots-Per-Inch, which is printer and print mode dependent.
70      */
71     public static final int POINTS_PER_INCH = 72;
72
73     // To maintain full signature compatibility with 1.5, and to improve the
74     // clarity of the generated javadoc (see 6287639: Abstract methods in
75     // enum classes should not be listed as abstract), method convert
76     // etc. are not declared abstract but otherwise act as abstract methods.
77
78     /**
79      * Convert the given length in the given unit to this
80      * unit.  Conversions from finer to coarser granularities
81      * truncate, so may lose precision.
82      *
83      * <p>For example, to convert 10 inches to point, use:
84      * <tt>PrintUnit.POINTS.convert(10L, PrintUnit.INCHES)</tt>
85      *
86      * @param sourceLength the length in the given <tt>sourceUnit</tt>
87      * @param sourceUnit the unit of the <tt>sourceDuration</tt> argument
88      *
89      * @return the converted length in this unit,
90      * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
91      * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
92      */
93     public double convert(double sourceLength, PrintUnit sourceUnit) {
94         throw new AbstractMethodError();
95     }
96
97     /**
98      * Equivalent to <tt>INCHES.convert(length, this)</tt>.
99      *
100      * @param length  the length
101      *
102      * @return the converted length,
103      * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
104      * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
105      * @see #convert
106      */
107     public double toInches(double length) {
108         throw new AbstractMethodError();
109     }
110
111     /**
112      * Equivalent to <tt>MILLIMETERS.convert(length, this)</tt>.
113      *
114      * @param length  the length
115      *
116      * @return the converted length,
117      * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
118      * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
119      * @see #convert
120      */
121     public double toMillis(double length) {
122         throw new AbstractMethodError();
123     }
124
125     /**
126      * Equivalent to <tt>CENTIMETERS.convert(length, this)</tt>.
127      *
128      * @param length  the length
129      *
130      * @return the converted length,
131      * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
132      * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
133      * @see #convert
134      */
135     public double toCentis(double length) {
136         throw new AbstractMethodError();
137     }
138
139     /**
140      * Equivalent to <tt>METERS.convert(length, this)</tt>.
141      *
142      * @param length  the length
143      *
144      * @return the converted length,
145      * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
146      * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
147      * @see #convert
148      */
149     public double toMeters(double length) {
150         throw new AbstractMethodError();
151     }
152
153     /**
154      * Equivalent to <tt>POINTS.convert(length, this)</tt>.
155      *
156      * @param length  the length
157      *
158      * @return the converted length,
159      * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
160      * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
161      * @see #convert
162      */
163     public double toPoints(double length) {
164         throw new AbstractMethodError();
165     }
166
167 }