0N/A/*
2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage javax.print.attribute;
0N/A
0N/Aimport java.io.Serializable;
0N/A
0N/A/**
0N/A * Class Size2DSyntax is an abstract base class providing the common
0N/A * implementation of all attributes denoting a size in two dimensions.
0N/A * <P>
0N/A * A two-dimensional size attribute's value consists of two items, the X
0N/A * dimension and the Y dimension. A two-dimensional size attribute may be
0N/A * constructed by supplying the two values and indicating the units in which the
0N/A * values are measured. Methods are provided to return a two-dimensional size
0N/A * attribute's values, indicating the units in which the values are to be
0N/A * returned. The two most common size units are inches (in) and millimeters
0N/A * (mm), and exported constants {@link #INCH <CODE>INCH</CODE>} and {@link #MM
0N/A * <CODE>MM</CODE>} are provided for indicating those units.
0N/A * <P>
0N/A * Once constructed, a two-dimensional size attribute's value is immutable.
0N/A * <P>
0N/A * <B>Design</B>
0N/A * <P>
0N/A * A two-dimensional size attribute's X and Y dimension values are stored
0N/A * internally as integers in units of micrometers (&#181;m), where 1 micrometer
0N/A * = 10<SUP>-6</SUP> meter = 1/1000 millimeter = 1/25400 inch. This permits
0N/A * dimensions to be represented exactly to a precision of 1/1000 mm (= 1
0N/A * &#181;m) or 1/100 inch (= 254 &#181;m). If fractional inches are expressed in
0N/A * negative powers of two, this permits dimensions to be represented exactly to
0N/A * a precision of 1/8 inch (= 3175 &#181;m) but not 1/16 inch (because 1/16 inch
0N/A * does not equal an integral number of &#181;m).
0N/A * <P>
0N/A * Storing the dimensions internally in common units of &#181;m lets two size
0N/A * attributes be compared without regard to the units in which they were
0N/A * created; for example, 8.5 in will compare equal to 215.9 mm, as they both are
0N/A * stored as 215900 &#181;m. For example, a lookup service can
0N/A * match resolution attributes based on equality of their serialized
0N/A * representations regardless of the units in which they were created. Using
0N/A * integers for internal storage allows precise equality comparisons to be done,
0N/A * which would not be guaranteed if an internal floating point representation
0N/A * were used. Note that if you're looking for U.S. letter sized media in metric
0N/A * units, you have to search for a media size of 215.9 x 279.4 mm; rounding off
0N/A * to an integral 216 x 279 mm will not match.
0N/A * <P>
0N/A * The exported constant {@link #INCH <CODE>INCH</CODE>} is actually the
0N/A * conversion factor by which to multiply a value in inches to get the value in
0N/A * &#181;m. Likewise, the exported constant {@link #MM <CODE>MM</CODE>} is the
0N/A * conversion factor by which to multiply a value in mm to get the value in
0N/A * &#181;m. A client can specify a resolution value in units other than inches
0N/A * or mm by supplying its own conversion factor. However, since the internal
0N/A * units of &#181;m was chosen with supporting only the external units of inch
0N/A * and mm in mind, there is no guarantee that the conversion factor for the
0N/A * client's units will be an exact integer. If the conversion factor isn't an
0N/A * exact integer, resolution values in the client's units won't be stored
0N/A * precisely.
0N/A * <P>
0N/A *
0N/A * @author Alan Kaminsky
0N/A */
0N/Apublic abstract class Size2DSyntax implements Serializable, Cloneable {
0N/A
0N/A private static final long serialVersionUID = 5584439964938660530L;
0N/A
0N/A /**
0N/A * X dimension in units of micrometers (&#181;m).
0N/A * @serial
0N/A */
0N/A private int x;
0N/A
0N/A /**
0N/A * Y dimension in units of micrometers (&#181;m).
0N/A * @serial
0N/A */
0N/A private int y;
0N/A
0N/A /**
0N/A * Value to indicate units of inches (in). It is actually the conversion
0N/A * factor by which to multiply inches to yield &#181;m (25400).
0N/A */
0N/A public static final int INCH = 25400;
0N/A
0N/A /**
0N/A * Value to indicate units of millimeters (mm). It is actually the
0N/A * conversion factor by which to multiply mm to yield &#181;m (1000).
0N/A */
0N/A public static final int MM = 1000;
0N/A
0N/A
0N/A /**
0N/A * Construct a new two-dimensional size attribute from the given
0N/A * floating-point values.
0N/A *
0N/A * @param x X dimension.
0N/A * @param y Y dimension.
0N/A * @param units
0N/A * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
0N/A * {@link #MM <CODE>MM</CODE>}.
0N/A *
0N/A * @exception IllegalArgumentException
0N/A * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE>
0N/A * < 0 or <CODE>units</CODE> < 1.
0N/A */
0N/A protected Size2DSyntax(float x, float y, int units) {
0N/A if (x < 0.0f) {
0N/A throw new IllegalArgumentException("x < 0");
0N/A }
0N/A if (y < 0.0f) {
0N/A throw new IllegalArgumentException("y < 0");
0N/A }
0N/A if (units < 1) {
0N/A throw new IllegalArgumentException("units < 1");
0N/A }
0N/A this.x = (int) (x * units + 0.5f);
0N/A this.y = (int) (y * units + 0.5f);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new two-dimensional size attribute from the given integer
0N/A * values.
0N/A *
0N/A * @param x X dimension.
0N/A * @param y Y dimension.
0N/A * @param units
0N/A * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
0N/A * {@link #MM <CODE>MM</CODE>}.
0N/A *
0N/A * @exception IllegalArgumentException
0N/A * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE>
0N/A * < 0 or <CODE>units</CODE> < 1.
0N/A */
0N/A protected Size2DSyntax(int x, int y, int units) {
0N/A if (x < 0) {
0N/A throw new IllegalArgumentException("x < 0");
0N/A }
0N/A if (y < 0) {
0N/A throw new IllegalArgumentException("y < 0");
0N/A }
0N/A if (units < 1) {
0N/A throw new IllegalArgumentException("units < 1");
0N/A }
0N/A this.x = x * units;
0N/A this.y = y * units;
0N/A }
0N/A
0N/A /**
0N/A * Convert a value from micrometers to some other units. The result is
0N/A * returned as a floating-point number.
0N/A *
0N/A * @param x
0N/A * Value (micrometers) to convert.
0N/A * @param units
0N/A * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
0N/A * {@link #MM <CODE>MM</CODE>}.
0N/A *
0N/A * @return The value of <CODE>x</CODE> converted to the desired units.
0N/A *
0N/A * @exception IllegalArgumentException
0N/A * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
0N/A */
0N/A private static float convertFromMicrometers(int x, int units) {
0N/A if (units < 1) {
0N/A throw new IllegalArgumentException("units is < 1");
0N/A }
0N/A return ((float)x) / ((float)units);
0N/A }
0N/A
0N/A /**
0N/A * Get this two-dimensional size attribute's dimensions in the given units
0N/A * as floating-point values.
0N/A *
0N/A * @param units
0N/A * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
0N/A * {@link #MM <CODE>MM</CODE>}.
0N/A *
0N/A * @return A two-element array with the X dimension at index 0 and the Y
0N/A * dimension at index 1.
0N/A *
0N/A * @exception IllegalArgumentException
0N/A * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
0N/A */
0N/A public float[] getSize(int units) {
0N/A return new float[] {getX(units), getY(units)};
0N/A }
0N/A
0N/A /**
0N/A * Returns this two-dimensional size attribute's X dimension in the given
0N/A * units as a floating-point value.
0N/A *
0N/A * @param units
0N/A * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
0N/A * {@link #MM <CODE>MM</CODE>}.
0N/A *
0N/A * @return X dimension.
0N/A *
0N/A * @exception IllegalArgumentException
0N/A * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
0N/A */
0N/A public float getX(int units) {
0N/A return convertFromMicrometers(x, units);
0N/A }
0N/A
0N/A /**
0N/A * Returns this two-dimensional size attribute's Y dimension in the given
0N/A * units as a floating-point value.
0N/A *
0N/A * @param units
0N/A * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
0N/A * {@link #MM <CODE>MM</CODE>}.
0N/A *
0N/A * @return Y dimension.
0N/A *
0N/A * @exception IllegalArgumentException
0N/A * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
0N/A */
0N/A public float getY(int units) {
0N/A return convertFromMicrometers(y, units);
0N/A }
0N/A
0N/A /**
0N/A * Returns a string version of this two-dimensional size attribute in the
0N/A * given units. The string takes the form <CODE>"<I>X</I>x<I>Y</I>
0N/A * <I>U</I>"</CODE>, where <I>X</I> is the X dimension, <I>Y</I> is the Y
0N/A * dimension, and <I>U</I> is the units name. The values are displayed in
0N/A * floating point.
0N/A *
0N/A * @param units
0N/A * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or
0N/A * {@link #MM <CODE>MM</CODE>}.
0N/A * @param unitsName
0N/A * Units name string, e.g. <CODE>"in"</CODE> or <CODE>"mm"</CODE>. If
0N/A * null, no units name is appended to the result.
0N/A *
0N/A * @return String version of this two-dimensional size attribute.
0N/A *
0N/A * @exception IllegalArgumentException
0N/A * (unchecked exception) Thrown if <CODE>units</CODE> < 1.
0N/A */
0N/A public String toString(int units, String unitsName) {
0N/A StringBuffer result = new StringBuffer();
0N/A result.append(getX (units));
0N/A result.append('x');
0N/A result.append(getY (units));
0N/A if (unitsName != null) {
0N/A result.append(' ');
0N/A result.append(unitsName);
0N/A }
0N/A return result.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns whether this two-dimensional size attribute is equivalent to the
0N/A * passed in object. To be equivalent, all of the following conditions must
0N/A * be true:
0N/A * <OL TYPE=1>
0N/A * <LI>
0N/A * <CODE>object</CODE> is not null.
0N/A * <LI>
0N/A * <CODE>object</CODE> is an instance of class Size2DSyntax.
0N/A * <LI>
0N/A * This attribute's X dimension is equal to <CODE>object</CODE>'s X
0N/A * dimension.
0N/A * <LI>
0N/A * This attribute's Y dimension is equal to <CODE>object</CODE>'s Y
0N/A * dimension.
0N/A * </OL>
0N/A *
0N/A * @param object Object to compare to.
0N/A *
0N/A * @return True if <CODE>object</CODE> is equivalent to this
0N/A * two-dimensional size attribute, false otherwise.
0N/A */
0N/A public boolean equals(Object object) {
0N/A return(object != null &&
0N/A object instanceof Size2DSyntax &&
0N/A this.x == ((Size2DSyntax) object).x &&
0N/A this.y == ((Size2DSyntax) object).y);
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code value for this two-dimensional size attribute.
0N/A */
0N/A public int hashCode() {
0N/A return (((x & 0x0000FFFF) ) |
0N/A ((y & 0x0000FFFF) << 16));
0N/A }
0N/A
0N/A /**
0N/A * Returns a string version of this two-dimensional size attribute. The
0N/A * string takes the form <CODE>"<I>X</I>x<I>Y</I> um"</CODE>, where
0N/A * <I>X</I> is the X dimension and <I>Y</I> is the Y dimension.
0N/A * The values are reported in the internal units of micrometers.
0N/A */
0N/A public String toString() {
0N/A StringBuffer result = new StringBuffer();
0N/A result.append(x);
0N/A result.append('x');
0N/A result.append(y);
0N/A result.append(" um");
0N/A return result.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns this two-dimensional size attribute's X dimension in units of
0N/A * micrometers (&#181;m). (For use in a subclass.)
0N/A *
0N/A * @return X dimension (&#181;m).
0N/A */
0N/A protected int getXMicrometers(){
0N/A return x;
0N/A }
0N/A
0N/A /**
0N/A * Returns this two-dimensional size attribute's Y dimension in units of
0N/A * micrometers (&#181;m). (For use in a subclass.)
0N/A *
0N/A * @return Y dimension (&#181;m).
0N/A */
0N/A protected int getYMicrometers() {
0N/A return y;
0N/A }
0N/A
0N/A}