001 /* ResolutionSyntax.java -- 002 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package javax.print.attribute; 039 040 import java.io.Serializable; 041 042 /** 043 * <code>ResolutionSyntax</code> is the abstract base class of all attribute 044 * classes which provide a resolution as value (e.g. printer resolution). 045 * <p> 046 * A <code>ResolutionSyntax</code> instance consists of two integer values 047 * describing the resolution in feed and cross feed direction. The units of 048 * the given values is determined by two defined constants: 049 * <ul> 050 * <li>DPCM - dots per centimeter</li> 051 * <li>DPI - dots per inch</li> 052 * </ul> 053 * </p> 054 * <p> 055 * A resolutions attribute is constructed by two values for the resolution and 056 * one of the two constants defining the actual units of the given values. 057 * </p> 058 * <p> 059 * There are different methods provided to return the resolution values in 060 * either of the both units and to compare if a resolution is less than or 061 * equal to a given other resolution attribute. 062 * </p> 063 * <p> 064 * <b>Internal storage:</b><br> 065 * The resolutions are stored internally as dots per 100 inches (dphi). The 066 * values of the provided constants for dots per inch (value 100) and dots 067 * per centimeter (value 254) are used as conversion factors to the internal 068 * storage units. To get the internal dphi values a multiplication of a given 069 * resolution value with its units constant value is needed. Retrieving the 070 * resolution for specific units is done by dividing the internal stored 071 * value through the units constant value. Clients are therefore able to 072 * provide their own resolution units by supplying other conversion factors. 073 * Subclasses of <code>ResolutionSyntax</code> have access to the internal 074 * resolution values through the protected methods 075 * {@link #getCrossFeedResolutionDphi()} and {@link #getFeedResolutionDphi()}. 076 * </p> 077 * 078 * @author Michael Koch (konqueror@gmx.de) 079 */ 080 public abstract class ResolutionSyntax 081 implements Cloneable, Serializable 082 { 083 private static final long serialVersionUID = 2706743076526672017L; 084 085 /** 086 * Constant for units of dots per centimeter. 087 */ 088 public static final int DPCM = 254; 089 090 /** 091 * Constant for units of dots per inch 092 */ 093 public static final int DPI = 100; 094 095 private int crossFeedResolution; 096 private int feedResolution; 097 098 /** 099 * Creates a <code>ResolutionSyntax</code> object with the given arguments. 100 * 101 * @param crossFeedResolution the cross feed resolution 102 * @param feedResolution the feed resolution 103 * @param units the unit to use (e.g. {@link #DPCM} or {@link #DPI}) 104 * 105 * @exception IllegalArgumentException if preconditions fail 106 */ 107 public ResolutionSyntax(int crossFeedResolution, int feedResolution, 108 int units) 109 { 110 if (crossFeedResolution < 1 111 || feedResolution < 1 112 || units < 1) 113 throw new IllegalArgumentException("no argument may be less than 1"); 114 115 this.crossFeedResolution = crossFeedResolution * units; 116 this.feedResolution = feedResolution * units; 117 } 118 119 /** 120 * Tests if the given object is equal to this object. 121 * 122 * @param obj the object to test 123 * 124 * @return <code>true</code> if both objects are equal, 125 * <code>false</code> otherwise. 126 */ 127 public boolean equals(Object obj) 128 { 129 if(! (obj instanceof ResolutionSyntax)) 130 return false; 131 132 ResolutionSyntax tmp = (ResolutionSyntax) obj; 133 134 return (crossFeedResolution == tmp.getCrossFeedResolutionDphi() 135 && feedResolution == tmp.getFeedResolutionDphi()); 136 } 137 138 /** 139 * Returns the cross feed resolution for the given units. 140 * 141 * @param units the unit to use (e.g. {@link #DPCM} or {@link #DPI}) 142 * @return The resolution for the given units. 143 * 144 * @exception IllegalArgumentException if units < 1 145 */ 146 public int getCrossFeedResolution(int units) 147 { 148 if (units < 1) 149 throw new IllegalArgumentException("units may not be less then 1"); 150 151 return crossFeedResolution / units; 152 } 153 154 /** 155 * Returns the raw cross feed resolution in dots per 100 inches. 156 * 157 * @return The raw resolution. 158 */ 159 protected int getCrossFeedResolutionDphi() 160 { 161 return crossFeedResolution; 162 } 163 164 /** 165 * Returns the feed resolution for the given units. 166 * 167 * @param units the unit to use (e.g. {@link #DPCM} or {@link #DPI}) 168 * @return The resolution for the given units. 169 * 170 * @exception IllegalArgumentException if units < 1 171 */ 172 public int getFeedResolution(int units) 173 { 174 if (units < 1) 175 throw new IllegalArgumentException("units may not be less then 1"); 176 177 return feedResolution / units; 178 } 179 180 /** 181 * Returns the raw feed resolution in dots per 100 inches. 182 * 183 * @return The raw resolution. 184 */ 185 protected int getFeedResolutionDphi() 186 { 187 return feedResolution; 188 } 189 190 /** 191 * Returns the resolution as two field array. Index 0 is the cross feed 192 * resolution, index 1 the feed resolution. 193 * 194 * @param units the units to use 195 * 196 * @return The array with the resolutions. 197 */ 198 public int[] getResolution(int units) 199 { 200 int[] resolution = new int[2]; 201 resolution[0] = getCrossFeedResolution(units); 202 resolution[1] = getFeedResolution(units); 203 return resolution; 204 } 205 206 /** 207 * Returns the hashcode for this object. 208 * 209 * @return The hashcode. 210 */ 211 public int hashCode() 212 { 213 return crossFeedResolution + feedResolution; 214 } 215 216 /** 217 * Checks if the given resolution attribute is a lower or equal 218 * to this resolution object. 219 * 220 * @param other the resolution to check against 221 * 222 * @return <code>true</code> if other resolution attribute describes 223 * a lower or equal resolution, <code>false</code> otherwise. 224 */ 225 public boolean lessThanOrEquals(ResolutionSyntax other) 226 { 227 if (other == null) 228 throw new NullPointerException("other may not be null"); 229 230 return (crossFeedResolution <= other.getCrossFeedResolutionDphi() 231 && feedResolution <= other.getFeedResolutionDphi()); 232 } 233 234 /** 235 * Returns the string representation for this object. 236 * <p> 237 * The returned string is in the form "CxF dphi" with C standing 238 * for the cross feed and F for the feed direction resolution. 239 * Units used are dots per 100 inches (dphi). 240 * </p> 241 * @return The string representation. 242 */ 243 public String toString() 244 { 245 return toString(1, "dphi"); 246 } 247 248 /** 249 * Returns the string representation for this object. 250 * <p> 251 * The returned string is in the form "CxF U" with C standing 252 * for the cross feed and F for the feed direction resolution. 253 * U denotes the units name if one is supplied. 254 * </p> 255 * 256 * @param units the units to use 257 * @param unitsName the name of the units. If <code>null</code> 258 * it is ommitted from the string representation. 259 * 260 * @return The string representation. 261 */ 262 public String toString(int units, String unitsName) 263 { 264 if (unitsName == null) 265 return getCrossFeedResolution(units) + "x" + getFeedResolution(units); 266 267 return ("" + getCrossFeedResolution(units) 268 + "x" + getFeedResolution(units) 269 + " " + unitsName); 270 } 271 }