Source for javax.print.attribute.ResolutionSyntax

   1: /* ResolutionSyntax.java -- 
   2:    Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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)
   9: any later version.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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
  24: combination.
  25: 
  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. */
  37: 
  38: package javax.print.attribute;
  39: 
  40: import java.io.Serializable;
  41: 
  42: /**
  43:  * <code>ResolutionSyntax</code> is the abstract base class of all attribute 
  44:  * classes which provide a resolution as value (e.g. printer resolution).
  45:  * <p>
  46:  * A <code>ResolutionSyntax</code> instance consists of two integer values
  47:  * describing the resolution in feed and cross feed direction. The units of 
  48:  * the given values is determined by two defined constants:
  49:  * <ul>
  50:  * <li>DPCM - dots per centimeter</li>
  51:  * <li>DPI - dots per inch</li>
  52:  * </ul>
  53:  * </p>
  54:  * <p>
  55:  * A resolutions attribute is constructed by two values for the resolution and
  56:  * one of the two constants defining the actual units of the given values.
  57:  * </p>
  58:  * <p>
  59:  * There are different methods provided to return the resolution values in 
  60:  * either of the both units and to compare if a resolution is less than or
  61:  * equal to a given other resolution attribute.
  62:  * </p>
  63:  * <p>
  64:  * <b>Internal storage:</b><br>
  65:  * The resolutions are stored internally as dots per 100 inches (dphi). The 
  66:  * values of the provided constants for dots per inch (value 100) and dots
  67:  * per centimeter (value 254) are used as conversion factors to the internal
  68:  * storage units. To get the internal dphi values a multiplication of a given
  69:  * resolution value with its units constant value is needed. Retrieving the 
  70:  * resolution for specific units is done by dividing the internal stored 
  71:  * value through the units constant value. Clients are therefore able to 
  72:  * provide their own resolution units by supplying other conversion factors.
  73:  * Subclasses of <code>ResolutionSyntax</code> have access to the internal
  74:  * resolution values through the protected methods 
  75:  * {@link #getCrossFeedResolutionDphi()} and {@link #getFeedResolutionDphi()}.
  76:  * </p>
  77:  * 
  78:  * @author Michael Koch (konqueror@gmx.de)
  79:  */
  80: public abstract class ResolutionSyntax
  81:   implements Cloneable, Serializable
  82: {
  83:   private static final long serialVersionUID = 2706743076526672017L;
  84: 
  85:   /**
  86:    * Constant for units of dots per centimeter.
  87:    */
  88:   public static final int DPCM = 254;
  89: 
  90:   /**
  91:    * Constant for units of dots per inch
  92:    */
  93:   public static final int DPI = 100;
  94: 
  95:   private int crossFeedResolution;
  96:   private int feedResolution;
  97:   
  98:   /**
  99:    * 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 &lt; 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 &lt; 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: }