001/*
002 * SVG Salamander
003 * Copyright (c) 2004, Mark McKay
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or 
007 * without modification, are permitted provided that the following
008 * conditions are met:
009 *
010 *   - Redistributions of source code must retain the above 
011 *     copyright notice, this list of conditions and the following
012 *     disclaimer.
013 *   - Redistributions in binary form must reproduce the above
014 *     copyright notice, this list of conditions and the following
015 *     disclaimer in the documentation and/or other materials 
016 *     provided with the distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
029 * OF THE POSSIBILITY OF SUCH DAMAGE. 
030 * 
031 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
032 * projects can be found at http://www.kitfox.com
033 *
034 * Created on February 18, 2004, 2:43 PM
035 */
036
037package com.kitfox.svg.xml;
038
039import java.io.Serializable;
040
041/**
042 * @author Mark McKay
043 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
044 */
045public class NumberWithUnits implements Serializable
046{
047    public static final long serialVersionUID = 0;
048    
049    public static final int UT_UNITLESS = 0;
050    public static final int UT_PX = 1;  //Pixels
051    public static final int UT_CM = 2;  //Centimeters
052    public static final int UT_MM = 3;  //Millimeters
053    public static final int UT_IN = 4;  //Inches
054    public static final int UT_EM = 5;  //Default font height
055    public static final int UT_EX = 6;  //Height of character 'x' in default font
056    public static final int UT_PT = 7;  //Points - 1/72 of an inch
057    public static final int UT_PC = 8;  //Picas - 1/6 of an inch
058    public static final int UT_PERCENT = 9;  //Percent - relative width
059
060    float value = 0f;
061    int unitType = UT_UNITLESS;
062
063    /** Creates a new instance of NumberWithUnits */
064    public NumberWithUnits()
065    {
066    }
067
068    public NumberWithUnits(String value)
069    {
070        set(value);
071    }
072
073    public NumberWithUnits(float value, int unitType)
074    {
075        this.value = value;
076        this.unitType = unitType;
077    }
078
079    public float getValue() { return value; }
080    public int getUnits() { return unitType; }
081
082    public void set(String value)
083    {
084        this.value = XMLParseUtil.findFloat(value);
085        unitType = UT_UNITLESS;
086
087        if (value.indexOf("px") != -1) { unitType = UT_PX; return; }
088        if (value.indexOf("cm") != -1) { unitType = UT_CM; return; }
089        if (value.indexOf("mm") != -1) { unitType = UT_MM; return; }
090        if (value.indexOf("in") != -1) { unitType = UT_IN; return; }
091        if (value.indexOf("em") != -1) { unitType = UT_EM; return; }
092        if (value.indexOf("ex") != -1) { unitType = UT_EX; return; }
093        if (value.indexOf("pt") != -1) { unitType = UT_PT; return; }
094        if (value.indexOf("pc") != -1) { unitType = UT_PC; return; }
095        if (value.indexOf("%") != -1) { unitType = UT_PERCENT; return; }
096    }
097
098    public static String unitsAsString(int unitIdx)
099    {
100        switch (unitIdx)
101        {
102            default:
103                return "";
104            case UT_PX:
105                return "px";
106            case UT_CM:
107                return "cm";
108            case UT_MM:
109                return "mm";
110            case UT_IN:
111                return "in";
112            case UT_EM:
113                return "em";
114            case UT_EX:
115                return "ex";
116            case UT_PT:
117                return "pt";
118            case UT_PC:
119                return "pc";
120            case UT_PERCENT:
121                return "%";
122        }
123    }
124
125    @Override
126    public String toString()
127    {
128        return "" + value + unitsAsString(unitType);
129    }
130
131    @Override
132    public boolean equals(Object obj)
133    {
134        if (obj == null) {
135            return false;
136        }
137        if (getClass() != obj.getClass()) {
138            return false;
139        }
140        final NumberWithUnits other = (NumberWithUnits) obj;
141        if (Float.floatToIntBits(this.value) != Float.floatToIntBits(other.value)) {
142            return false;
143        }
144        if (this.unitType != other.unitType) {
145            return false;
146        }
147        return true;
148    }
149
150    @Override
151    public int hashCode()
152    {
153        int hash = 5;
154        hash = 37 * hash + Float.floatToIntBits(this.value);
155        hash = 37 * hash + this.unitType;
156        return hash;
157    }
158
159
160}