Source for javax.print.attribute.Size2DSyntax

   1: /* Size2DSyntax.java -- 
   2:    Copyright (C) 2003 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 Size2DSyntax implements Cloneable, Serializable
  46: {
  47:   /**
  48:    * Constant for units of dots per mircometer to describe an inch.
  49:    */
  50:   public static final int INCH = 25400;
  51: 
  52:   /**
  53:    * Constant for units of dots per mircometer to describe a centimeter.
  54:    */
  55:   public static final int MM = 1000;
  56: 
  57:   private int x;
  58:   private int y;
  59: 
  60:   /**
  61:    * Creates a <code>Size2DSyntax</code> object with the given arguments.
  62:    *
  63:    * @param x the size in x direction
  64:    * @param y the size in y direction
  65:    * @param units the units to use for the sizes
  66:    *
  67:    * @exception IllegalArgumentException if preconditions fail
  68:    */
  69:   protected Size2DSyntax(float x, float y, int units)
  70:   {
  71:     if (x < 0.0f || y < 0.0f)
  72:       throw new IllegalArgumentException("x and/or y may not be less than 0");
  73: 
  74:     if (units < 1)
  75:       throw new IllegalArgumentException("units may not be less then 1");
  76: 
  77:     this.x = (int) (x * units + 0.5f);
  78:     this.y = (int) (y * units + 0.5f);
  79:   }
  80: 
  81:   /**
  82:    * Creates a <code>Size2DSyntax</code> object with the given arguments.
  83:    *
  84:    * @param x the size in x direction
  85:    * @param y the size in y direction
  86:    * @param units the units to use for the sizes
  87:    *
  88:    * @exception IllegalArgumentException if preconditions fail
  89:    */
  90:   protected Size2DSyntax(int x, int y, int units)
  91:   {
  92:     if (x < 0 || y < 0)
  93:       throw new IllegalArgumentException("x and/or y may not be less then 0");
  94: 
  95:     if (units < 1)
  96:       throw new IllegalArgumentException("units may not be less then 1");
  97: 
  98:     this.x = x * units;
  99:     this.y = y * units;
 100:   }
 101: 
 102:   /**
 103:    * Tests of obj is equal to this object.
 104:    *
 105:    * @param obj the object to test
 106:    *
 107:    * @returns true if both objects are equal, false otherwise.
 108:    */
 109:   public boolean equals(Object obj)
 110:   {
 111:     if (! (obj instanceof Size2DSyntax))
 112:       return false;
 113: 
 114:     Size2DSyntax tmp = (Size2DSyntax) obj;
 115: 
 116:     return (x == tmp.getXMicrometers()
 117:             && y == tmp.getYMicrometers());
 118:   }
 119: 
 120:   /**
 121:    * Return the size described in this object as a two field array.
 122:    * Index 0 contains the size in x direction, index 1 the size in
 123:    * y direction.
 124:    *
 125:    * @param units the units to use
 126:    *
 127:    * @return the array that describes the size
 128:    *
 129:    * @exception IllegalArgumentException if units < 1
 130:    */
 131:   public float[] getSize(int units)
 132:   {
 133:     float[] size = new float[2];
 134:     size[0] = getX(units);
 135:     size[1] = getY(units);
 136:     return size;
 137:   }
 138: 
 139:   /**
 140:    * Return the size in x direction.
 141:    *
 142:    * @param units the units to use
 143:    *
 144:    * @return the size value
 145:    *
 146:    * @exception IllegalArgumentException if units < 1
 147:    */
 148:   public float getX(int units)
 149:   {
 150:     if (units < 1)
 151:       throw new IllegalArgumentException("units may not be less then 1");
 152: 
 153:     return ((float) x) / ((float) units);
 154:   }
 155: 
 156:   /**
 157:    * Returns the size in x direction in mircometers.
 158:    *
 159:    * @return the size value
 160:    */
 161:   protected int getXMicrometers()
 162:   {
 163:     return x;
 164:   }
 165: 
 166:   /**
 167:    * Return the size in y direction.
 168:    *
 169:    * @param units the units to use
 170:    *
 171:    * @return the size value
 172:    *
 173:    * @exception IllegalArgumentException if units < 1
 174:    */
 175:   public float getY(int units)
 176:   {
 177:     if (units < 1)
 178:       throw new IllegalArgumentException("units may not be less then 1");
 179: 
 180:     return ((float) y) / ((float) units);
 181:   }
 182:   
 183:   /**
 184:    * Returns the size in y direction in mircometers.
 185:    *
 186:    * @return the size value
 187:    */
 188:   protected int getYMicrometers()
 189:   {
 190:     return y;
 191:   }
 192: 
 193:   /**
 194:    * Returns the hashcode for this object.
 195:    *
 196:    * @return the hashcode
 197:    */
 198:   public int hashCode()
 199:   {
 200:     return x + y;
 201:   }
 202: 
 203:   /**
 204:    * Returns the string representation for this object.
 205:    *
 206:    * @return the string representation
 207:    */
 208:   public String toString()
 209:   {
 210:     return toString(1, "um");
 211:   }
 212: 
 213:   /**
 214:    * Returns the string representation for this object.
 215:    *
 216:    * @param units the units to use
 217:    * @param unitsName the name of the units
 218:    *
 219:    * @return the string representation
 220:    */
 221:   public String toString(int units, String unitsName)
 222:   {
 223:     return "" + getX(units) + "x" + getY(units) + " " + unitsName;
 224:   }
 225: }