Units.java

00001 package edu.stanford.hci.r3.units;
00002 
00014 public abstract class Units implements Cloneable {
00015 
00020         public static final double POINTS_PER_INCH = 72;
00021 
00027         public static Units add(Units a, Units b) {
00028                 return a.getUnitsObjectOfSameTypeWithValue(a.getValue() + b.getValueIn(a));
00029         }
00030 
00036         public static Units subtract(Units a, Units b) {
00037                 return a.getUnitsObjectOfSameTypeWithValue(a.getValue() - b.getValueIn(a));
00038         }
00039 
00040         private String unitName;
00041 
00046         private double value;
00047 
00053         protected Units(double val) {
00054                 value = val;
00055                 unitName = getClass().getSimpleName(); // use the java name for the units name
00056         }
00057 
00065         public Units(double val, String name) {
00066                 value = val;
00067                 unitName = name; // use the custom units name
00068         }
00069 
00073         protected Units clone() {
00074                 try {
00075                         return (Units) super.clone();
00076                 } catch (CloneNotSupportedException e) {
00077                         e.printStackTrace();
00078                 }
00079                 return null;
00080         }
00081 
00087         @Override
00088         public boolean equals(Object o) {
00089                 if (o instanceof Units) {
00090                         final Units other = (Units) o;
00091 
00092                         // if we get our value in the other units, is it equal to the value of the other unit?
00093                         return getValueIn(other) == other.getValue();
00094                 }
00095                 // not a units object!
00096                 return false;
00097         }
00098 
00108         public double getConversionTo(Units destUnits) {
00109                 return destUnits.getNumberOfUnitsInOneInch() / getNumberOfUnitsInOneInch();
00110         }
00111 
00120         public Units getCopy() {
00121                 // tricky, huh? =)
00122                 return getUnitsObjectOfSameLengthIn(this);
00123         }
00124 
00128         protected abstract double getNumberOfUnitsInOneInch();
00129 
00133         public String getUnitName() {
00134                 return unitName;
00135         }
00136 
00144         public Units getUnitsObjectOfSameLengthIn(Units destUnits) {
00145                 final Units dest = destUnits.clone();
00146                 dest.value = getValueIn(dest);
00147                 return dest;
00148         }
00149 
00156         public Units getUnitsObjectOfSameTypeWithValue(double val) {
00157                 final Units dest = this.clone();
00158                 dest.value = val;
00159                 return dest;
00160         }
00161 
00165         public double getValue() {
00166                 return value;
00167         }
00168 
00178         public double getValueIn(Units destUnits) {
00179                 return value * getConversionTo(destUnits);
00180         }
00181 
00185         public double getValueInCentimeters() {
00186                 return getValueIn(Centimeters.ONE);
00187         }
00188 
00197         public double getValueInInches() {
00198                 return getValueIn(Inches.ONE);
00199         }
00200 
00204         public double getValueInMillimeters() {
00205                 return getValueIn(Millimeters.ONE);
00206         }
00207 
00213         public double getValueInPatternDots() {
00214                 return getValueIn(PatternDots.ONE);
00215         }
00216 
00223         public double getValueInPixels() {
00224                 return getValueIn(Pixels.ONE);
00225         }
00226 
00232         public double getValueInPoints() {
00233                 return getValueIn(Points.ONE);
00234         }
00235 
00241         public Inches toInches() {
00242                 return new Inches(getValueInInches());
00243         }
00244 
00249         public Pixels toPixels() {
00250                 return new Pixels(getValueInPixels());
00251         }
00252 
00258         public Points toPoints() {
00259                 return new Points(getValueInPoints());
00260         }
00261 
00265         public String toString() {
00266                 return value + " " + getUnitName();
00267         }
00268 
00269 }

Generated on Sat Apr 14 18:21:39 2007 for R3 Paper Toolkit by  doxygen 1.4.7