Mega Code Archive
Unit measure
/*
* Ruler.java
*
* Created on Oct 1, 2007, 8:33:09 AM
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.HashMap;
/**
*
* @author gtoffoli
*/
public class Unit {
public static final double PIXEL = 1.0;
public static final double INCHES = 72.0;
public static final double CENTIMETERS = 28.3464;
public static final double MILLIMETERS = 2.83464;
/** Holds value of property unitName. */
private String unitName;
private String keyName;
/** Holds value of property conversionValue. */
private double conversionValue;
/** Creates a new instance of Unit */
public Unit(String unitName, double conversionValue) {
this(unitName, conversionValue, unitName);
}
public Unit(String unitName, double conversionValue, String keyName) {
this.unitName = unitName;
this.conversionValue = conversionValue;
this.setKeyName(keyName);
}
/** Getter for property unitName.
* @return Value of property unitName.
*
*/
public String getUnitName() {
return this.unitName;
}
/** Setter for property unitName.
* @param unitName New value of property unitName.
*
*/
public void setUnitName(String unitName) {
this.unitName = unitName;
}
/** Getter for property conversionValue.
* @return Value of property conversionValue.
*
*/
public double getConversionValue() {
return this.conversionValue;
}
/** Setter for property conversionValue.
* @param conversionValue New value of property conversionValue.
*
*/
public void setConversionValue(double conversionValue) {
this.conversionValue = conversionValue;
}
private static Unit[] units = null;
private static HashMap unitsMap = null;
public static Unit[] getStandardUnits()
{
if (units == null)
{
unitsMap = new HashMap();
units = new Unit[4];
units[0] = new Unit("pixels",Unit.PIXEL, "pixels");
unitsMap.put("pixels", units[0]);
units[1] = new Unit("inches",Unit.INCHES, "inches");
unitsMap.put("inches", units[1]);
units[2] = new Unit("cm", Unit.CENTIMETERS, "cm");
unitsMap.put("cm", units[2]);
units[3] = new Unit("mm", Unit.MILLIMETERS, "mm");
unitsMap.put("mm", units[3]);
}
return units;
}
public static int getUnitIndex(String unitName)
{
Unit[] units = getStandardUnits();
for (int i=0; i