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