1 /* Size2DSyntax.java --
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax
.print
.attribute
;
40 import java
.io
.Serializable
;
43 * @author Michael Koch
45 public abstract class Size2DSyntax
implements Cloneable
, Serializable
48 * Constant for units of dots per mircometer to describe an inch.
50 public static final int INCH
= 25400;
53 * Constant for units of dots per mircometer to describe a centimeter.
55 public static final int MM
= 1000;
61 * Creates a <code>Size2DSyntax</code> object with the given arguments.
63 * @param x the size in x direction
64 * @param y the size in y direction
65 * @param units the units to use for the sizes
67 * @exception IllegalArgumentException if preconditions fail
69 protected Size2DSyntax(float x
, float y
, int units
)
71 if (x
< 0.0f
|| y
< 0.0f
)
72 throw new IllegalArgumentException("x and/or y may not be less than 0");
75 throw new IllegalArgumentException("units may not be less then 1");
77 this.x
= (int) (x
* units
+ 0.5f
);
78 this.y
= (int) (y
* units
+ 0.5f
);
82 * Creates a <code>Size2DSyntax</code> object with the given arguments.
84 * @param x the size in x direction
85 * @param y the size in y direction
86 * @param units the units to use for the sizes
88 * @exception IllegalArgumentException if preconditions fail
90 protected Size2DSyntax(int x
, int y
, int units
)
93 throw new IllegalArgumentException("x and/or y may not be less then 0");
96 throw new IllegalArgumentException("units may not be less then 1");
103 * Tests of obj is equal to this object.
105 * @param obj the object to test
107 * @returns true if both objects are equal, false otherwise.
109 public boolean equals(Object obj
)
111 if (! (obj
instanceof Size2DSyntax
))
114 Size2DSyntax tmp
= (Size2DSyntax
) obj
;
116 return (x
== tmp
.getXMicrometers()
117 && y
== tmp
.getYMicrometers());
121 * Return the size described in this object as a two field array.
122 * Index 0 contains the size in x direction, index 1 the size in
125 * @param units the units to use
127 * @return the array that describes the size
129 * @exception IllegalArgumentException if units < 1
131 public float[] getSize(int units
)
133 float[] size
= new float[2];
134 size
[0] = getX(units
);
135 size
[1] = getY(units
);
140 * Return the size in x direction.
142 * @param units the units to use
144 * @return the size value
146 * @exception IllegalArgumentException if units < 1
148 public float getX(int units
)
151 throw new IllegalArgumentException("units may not be less then 1");
153 return ((float) x
) / ((float) units
);
157 * Returns the size in x direction in mircometers.
159 * @return the size value
161 protected int getXMicrometers()
167 * Return the size in y direction.
169 * @param units the units to use
171 * @return the size value
173 * @exception IllegalArgumentException if units < 1
175 public float getY(int units
)
178 throw new IllegalArgumentException("units may not be less then 1");
180 return ((float) y
) / ((float) units
);
184 * Returns the size in y direction in mircometers.
186 * @return the size value
188 protected int getYMicrometers()
194 * Returns the hashcode for this object.
196 * @return the hashcode
198 public int hashCode()
204 * Returns the string representation for this object.
206 * @return the string representation
208 public String
toString()
210 return toString(1, "um");
214 * Returns the string representation for this object.
216 * @param units the units to use
217 * @param unitsName the name of the units
219 * @return the string representation
221 public String
toString(int units
, String unitsName
)
223 return "" + getX(units
) + "x" + getY(units
) + " " + unitsName
;