Source for javax.print.attribute.ResolutionSyntax

   1: /* ResolutionSyntax.java -- 
   2:    Copyright (C) 2003, 2004  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:  * @author Michael Koch
  44:  */
  45: public abstract class ResolutionSyntax
  46:   implements Cloneable, Serializable
  47: {
  48:   private static final long serialVersionUID = 2706743076526672017L;
  49: 
  50:   /**
  51:    * Constant for units of dots per centimeter.
  52:    */
  53:   public static final int DPCM = 254;
  54: 
  55:   /**
  56:    * Constant for units of dots per inch
  57:    */
  58:   public static final int DPI = 100;
  59: 
  60:   private int crossFeedResolution;
  61:   private int feedResolution;
  62:   
  63:   /**
  64:    * Creates a <code>ResolutionSyntax</code> object with the given arguments.
  65:    *
  66:    * @param crossFeedResolution the cross feed resolution
  67:    * @param feedResolution the feed resolution
  68:    * @param units the unit to use
  69:    *
  70:    * @exception IllegalArgumentException if preconditions fail
  71:    */
  72:   public ResolutionSyntax(int crossFeedResolution, int feedResolution,
  73:                           int units)
  74:   {
  75:     if (crossFeedResolution < 1
  76:         || feedResolution < 1
  77:         || units < 1)
  78:       throw new IllegalArgumentException("no argument may be less than 1");
  79: 
  80:     this.crossFeedResolution = crossFeedResolution * units;
  81:     this.feedResolution = feedResolution * units;
  82:   }
  83: 
  84:   /**
  85:    * Tests of obj is equal to this object.
  86:    *
  87:    * @param obj the object to test
  88:    *
  89:    * @return true if both objects are equal, false otherwise.
  90:    */
  91:   public boolean equals(Object obj)
  92:   {
  93:     if(! (obj instanceof ResolutionSyntax))
  94:       return false;
  95: 
  96:     ResolutionSyntax tmp = (ResolutionSyntax) obj;
  97:     
  98:     return (crossFeedResolution == tmp.getCrossFeedResolutionDphi()
  99:             && feedResolution == tmp.getFeedResolutionDphi());
 100:   }
 101: 
 102:   /**
 103:    * Returns the cross feed resolution in units.
 104:    *
 105:    * @return the resolution
 106:    *
 107:    * @exception IllegalArgumentException if units < 1
 108:    */
 109:   public int getCrossFeedResolution(int units)
 110:   {
 111:     if (units < 1)
 112:       throw new IllegalArgumentException("units may not be less then 1");
 113: 
 114:     return (crossFeedResolution + units) / units;
 115:   }
 116: 
 117:   /**
 118:    * Returns the raw cross feed resolution in units.
 119:    *
 120:    * @return the raw resolution
 121:    */
 122:   protected int getCrossFeedResolutionDphi()
 123:   {
 124:     return crossFeedResolution;
 125:   }
 126: 
 127:   /**
 128:    * Returns the feed resolution in units.
 129:    *
 130:    * @return the resolution
 131:    *
 132:    * @exception IllegalArgumentException if units < 1
 133:    */
 134:   public int getFeedResolution(int units)
 135:   {
 136:     if (units < 1)
 137:       throw new IllegalArgumentException("units may not be less then 1");
 138: 
 139:     return (crossFeedResolution + units) / units;
 140:   }
 141: 
 142:   /**
 143:    * Returns the raw feed resolution in units.
 144:    *
 145:    * @return the raw resolution
 146:    */
 147:   protected int getFeedResolutionDphi()
 148:   {
 149:     return feedResolution;
 150:   }
 151:   
 152:   /**
 153:    * Returns the resolution as two field array. Index 0 is the cross feed
 154:    * resolution, index 1 the feed resolution.
 155:    *
 156:    * @param units the units to use
 157:    *
 158:    * @return the array with the resolutions
 159:    */
 160:   public int[] getResolution(int units)
 161:   {
 162:     int[] resolution = new int[2];
 163:     resolution[0] = getCrossFeedResolution(units);
 164:     resolution[1] = getFeedResolution(units);
 165:     return resolution;
 166:   }
 167: 
 168:   /**
 169:    * Returns the hashcode for this object.
 170:    *
 171:    * @return the hashcode
 172:    */
 173:   public int hashCode()
 174:   {
 175:     return crossFeedResolution + feedResolution;
 176:   }
 177: 
 178:   /**
 179:    * Checks of other is a lower or equal resolution.
 180:    *
 181:    * @param other the resolution to check against
 182:    *
 183:    * @return true if other describes a lower or equal resolution
 184:    */
 185:   public boolean lessThanOrEquals(ResolutionSyntax other)
 186:   {
 187:     if (other == null)
 188:       throw new NullPointerException("other may not be null");
 189: 
 190:     return (crossFeedResolution <= other.getCrossFeedResolutionDphi()
 191:             && feedResolution <= other.getFeedResolutionDphi());
 192:   }
 193: 
 194:   /**
 195:    * Returns the string representation for this object.
 196:    *
 197:    * @return the string representation
 198:    */
 199:   public String toString()
 200:   {
 201:     return toString(1, "dphi");
 202:   }
 203: 
 204:   /**
 205:    * Returns the string representation for this object.
 206:    *
 207:    * @param units the units to use
 208:    * @param unitsName the name of the units
 209:    *
 210:    * @return the string representation
 211:    */
 212:   public String toString(int units, String unitsName)
 213:   {
 214:     return ("" + getCrossFeedResolution(units)
 215:             + "x" + getFeedResolution(units)
 216:             + " " + unitsName);
 217:   }
 218: }