Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/print/attribute/ResolutionSyntax.java
38918 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package javax.print.attribute;2728import java.io.Serializable;2930/**31* Class ResolutionSyntax is an abstract base class providing the common32* implementation of all attributes denoting a printer resolution.33* <P>34* A resolution attribute's value consists of two items, the cross feed35* direction resolution and the feed direction resolution. A resolution36* attribute may be constructed by supplying the two values and indicating the37* units in which the values are measured. Methods are provided to return a38* resolution attribute's values, indicating the units in which the values are39* to be returned. The two most common resolution units are dots per inch (dpi)40* and dots per centimeter (dpcm), and exported constants {@link #DPI41* DPI} and {@link #DPCM DPCM} are provided for42* indicating those units.43* <P>44* Once constructed, a resolution attribute's value is immutable.45* <P>46* <B>Design</B>47* <P>48* A resolution attribute's cross feed direction resolution and feed direction49* resolution values are stored internally using units of dots per 100 inches50* (dphi). Storing the values in dphi rather than, say, metric units allows51* precise integer arithmetic conversions between dpi and dphi and between dpcm52* and dphi: 1 dpi = 100 dphi, 1 dpcm = 254 dphi. Thus, the values can be stored53* into and retrieved back from a resolution attribute in either units with no54* loss of precision. This would not be guaranteed if a floating point55* representation were used. However, roundoff error will in general occur if a56* resolution attribute's values are created in one units and retrieved in57* different units; for example, 600 dpi will be rounded to 236 dpcm, whereas58* the true value (to five figures) is 236.22 dpcm.59* <P>60* Storing the values internally in common units of dphi lets two resolution61* attributes be compared without regard to the units in which they were62* created; for example, 300 dpcm will compare equal to 762 dpi, as they both63* are stored as 76200 dphi. In particular, a lookup service can64* match resolution attributes based on equality of their serialized65* representations regardless of the units in which they were created. Again,66* using integers for internal storage allows precise equality comparisons to be67* done, which would not be guaranteed if a floating point representation were68* used.69* <P>70* The exported constant {@link #DPI DPI} is actually the71* conversion factor by which to multiply a value in dpi to get the value in72* dphi. Likewise, the exported constant {@link #DPCM DPCM} is the73* conversion factor by which to multiply a value in dpcm to get the value in74* dphi. A client can specify a resolution value in units other than dpi or dpcm75* by supplying its own conversion factor. However, since the internal units of76* dphi was chosen with supporting only the external units of dpi and dpcm in77* mind, there is no guarantee that the conversion factor for the client's units78* will be an exact integer. If the conversion factor isn't an exact integer,79* resolution values in the client's units won't be stored precisely.80* <P>81*82* @author David Mendenhall83* @author Alan Kaminsky84*/85public abstract class ResolutionSyntax implements Serializable, Cloneable {8687private static final long serialVersionUID = 2706743076526672017L;8889/**90* Cross feed direction resolution in units of dots per 100 inches (dphi).91* @serial92*/93private int crossFeedResolution;9495/**96* Feed direction resolution in units of dots per 100 inches (dphi).97* @serial98*/99private int feedResolution;100101/**102* Value to indicate units of dots per inch (dpi). It is actually the103* conversion factor by which to multiply dpi to yield dphi (100).104*/105public static final int DPI = 100;106107/**108* Value to indicate units of dots per centimeter (dpcm). It is actually109* the conversion factor by which to multiply dpcm to yield dphi (254).110*/111public static final int DPCM = 254;112113114/**115* Construct a new resolution attribute from the given items.116*117* @param crossFeedResolution118* Cross feed direction resolution.119* @param feedResolution120* Feed direction resolution.121* @param units122* Unit conversion factor, e.g. {@link #DPI DPI} or123* {@link #DPCM DPCM}.124*125* @exception IllegalArgumentException126* (unchecked exception) Thrown if {@code crossFeedResolution < 1}127* or {@code feedResolution < 1} or {@code units < 1}.128*/129public ResolutionSyntax(int crossFeedResolution, int feedResolution,130int units) {131132if (crossFeedResolution < 1) {133throw new IllegalArgumentException("crossFeedResolution is < 1");134}135if (feedResolution < 1) {136throw new IllegalArgumentException("feedResolution is < 1");137}138if (units < 1) {139throw new IllegalArgumentException("units is < 1");140}141142this.crossFeedResolution = crossFeedResolution * units;143this.feedResolution = feedResolution * units;144}145146/**147* Convert a value from dphi to some other units. The result is rounded to148* the nearest integer.149*150* @param dphi151* Value (dphi) to convert.152* @param units153* Unit conversion factor, e.g. {@link #DPI <CODE>DPI</CODE>} or154* {@link #DPCM <CODE>DPCM</CODE>}.155*156* @return The value of <CODE>dphi</CODE> converted to the desired units.157*158* @exception IllegalArgumentException159* (unchecked exception) Thrown if <CODE>units</CODE> < 1.160*/161private static int convertFromDphi(int dphi, int units) {162if (units < 1) {163throw new IllegalArgumentException(": units is < 1");164}165int round = units / 2;166return (dphi + round) / units;167}168169/**170* Get this resolution attribute's resolution values in the given units.171* The values are rounded to the nearest integer.172*173* @param units174* Unit conversion factor, e.g. {@link #DPI DPI} or175* {@link #DPCM DPCM}.176*177* @return A two-element array with the cross feed direction resolution178* at index 0 and the feed direction resolution at index 1.179*180* @exception IllegalArgumentException181* (unchecked exception) Thrown if {@code units < 1}.182*/183public int[] getResolution(int units) {184return new int[] { getCrossFeedResolution(units),185getFeedResolution(units)186};187}188189/**190* Returns this resolution attribute's cross feed direction resolution in191* the given units. The value is rounded to the nearest integer.192*193* @param units194* Unit conversion factor, e.g. {@link #DPI DPI} or195* {@link #DPCM DPCM}.196*197* @return Cross feed direction resolution.198*199* @exception IllegalArgumentException200* (unchecked exception) Thrown if {@code units < 1}.201*/202public int getCrossFeedResolution(int units) {203return convertFromDphi (crossFeedResolution, units);204}205206/**207* Returns this resolution attribute's feed direction resolution in the208* given units. The value is rounded to the nearest integer.209*210* @param units211* Unit conversion factor, e.g. {@link #DPI DPI} or {@link212* #DPCM DPCM}.213*214* @return Feed direction resolution.215*216* @exception IllegalArgumentException217* (unchecked exception) Thrown if {@code units < 1}.218*/219public int getFeedResolution(int units) {220return convertFromDphi (feedResolution, units);221}222223/**224* Returns a string version of this resolution attribute in the given units.225* The string takes the form <CODE>"<I>C</I>x<I>F</I> <I>U</I>"</CODE>,226* where <I>C</I> is the cross feed direction resolution, <I>F</I> is the227* feed direction resolution, and <I>U</I> is the units name. The values are228* rounded to the nearest integer.229*230* @param units231* Unit conversion factor, e.g. {@link #DPI CODE>DPI} or {@link232* #DPCM DPCM}.233* @param unitsName234* Units name string, e.g. <CODE>"dpi"</CODE> or <CODE>"dpcm"</CODE>. If235* null, no units name is appended to the result.236*237* @return String version of this resolution attribute.238*239* @exception IllegalArgumentException240* (unchecked exception) Thrown if {@code units < 1}.241*/242public String toString(int units, String unitsName) {243StringBuffer result = new StringBuffer();244result.append(getCrossFeedResolution (units));245result.append('x');246result.append(getFeedResolution (units));247if (unitsName != null) {248result.append (' ');249result.append (unitsName);250}251return result.toString();252}253254255/**256* Determine whether this resolution attribute's value is less than or257* equal to the given resolution attribute's value. This is true if all258* of the following conditions are true:259* <UL>260* <LI>261* This attribute's cross feed direction resolution is less than or equal to262* the <CODE>other</CODE> attribute's cross feed direction resolution.263* <LI>264* This attribute's feed direction resolution is less than or equal to the265* <CODE>other</CODE> attribute's feed direction resolution.266* </UL>267*268* @param other Resolution attribute to compare with.269*270* @return True if this resolution attribute is less than or equal to the271* <CODE>other</CODE> resolution attribute, false otherwise.272*273* @exception NullPointerException274* (unchecked exception) Thrown if <CODE>other</CODE> is null.275*/276public boolean lessThanOrEquals(ResolutionSyntax other) {277return (this.crossFeedResolution <= other.crossFeedResolution &&278this.feedResolution <= other.feedResolution);279}280281282/**283* Returns whether this resolution attribute is equivalent to the passed in284* object. To be equivalent, all of the following conditions must be true:285* <OL TYPE=1>286* <LI>287* <CODE>object</CODE> is not null.288* <LI>289* <CODE>object</CODE> is an instance of class ResolutionSyntax.290* <LI>291* This attribute's cross feed direction resolution is equal to292* <CODE>object</CODE>'s cross feed direction resolution.293* <LI>294* This attribute's feed direction resolution is equal to295* <CODE>object</CODE>'s feed direction resolution.296* </OL>297*298* @param object Object to compare to.299*300* @return True if <CODE>object</CODE> is equivalent to this resolution301* attribute, false otherwise.302*/303public boolean equals(Object object) {304305return(object != null &&306object instanceof ResolutionSyntax &&307this.crossFeedResolution ==308((ResolutionSyntax) object).crossFeedResolution &&309this.feedResolution ==310((ResolutionSyntax) object).feedResolution);311}312313/**314* Returns a hash code value for this resolution attribute.315*/316public int hashCode() {317return(((crossFeedResolution & 0x0000FFFF)) |318((feedResolution & 0x0000FFFF) << 16));319}320321/**322* Returns a string version of this resolution attribute. The string takes323* the form <CODE>"<I>C</I>x<I>F</I> dphi"</CODE>, where <I>C</I> is the324* cross feed direction resolution and <I>F</I> is the feed direction325* resolution. The values are reported in the internal units of dphi.326*/327public String toString() {328StringBuffer result = new StringBuffer();329result.append(crossFeedResolution);330result.append('x');331result.append(feedResolution);332result.append(" dphi");333return result.toString();334}335336337/**338* Returns this resolution attribute's cross feed direction resolution in339* units of dphi. (For use in a subclass.)340*341* @return Cross feed direction resolution.342*/343protected int getCrossFeedResolutionDphi() {344return crossFeedResolution;345}346347/**348* Returns this resolution attribute's feed direction resolution in units349* of dphi. (For use in a subclass.)350*351* @return Feed direction resolution.352*/353protected int getFeedResolutionDphi() {354return feedResolution;355}356357}358359360