1 /*
2
3    Licensed to the Apache Software Foundation (ASF) under one or more
4    contributor license agreements.  See the NOTICE file distributed with
5    this work for additional information regarding copyright ownership.
6    The ASF licenses this file to You under the Apache License, Version 2.0
7    (the "License"); you may not use this file except in compliance with
8    the License.  You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17
18  */

19 package org.apache.batik.css.engine.value;
20
21 import org.w3c.dom.DOMException;
22 import org.w3c.dom.css.CSSPrimitiveValue;
23
24 /**
25  * This class represents float values.
26  *
27  * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: FloatValue.java 1733416 2016-03-03 07:07:13Z gadams $
29  */

30 public class FloatValue extends AbstractValue {
31
32     /**
33      * Returns the CSS text associated with the given type/value pair.
34      */

35     public static String getCssText(short unit, float value) {
36         if (unit < 0 || unit >= UNITS.length) {
37             throw new DOMException(DOMException.SYNTAX_ERR, "");
38         }
39         String s = String.valueOf(value);
40         if (s.endsWith(".0")) {
41             s = s.substring(0, s.length() - 2);
42         }
43         return s + UNITS[unit - CSSPrimitiveValue.CSS_NUMBER];
44     }
45
46     /**
47      * The unit types representations
48      */

49     protected static final String[] UNITS = {
50         """%""em""ex""px""cm""mm""in""pt",
51         "pc""deg""rad""grad""ms""s""Hz""kHz"""
52     };
53
54     /**
55      * The float value
56      */

57     protected float floatValue;
58
59     /**
60      * The unit type
61      */

62     protected short unitType;
63
64     /**
65      * Creates a new value.
66      */

67     public FloatValue(short unitType, float floatValue) {
68         this.unitType   = unitType;
69         this.floatValue = floatValue;
70     }
71
72     /**
73      * The type of the value.
74      */

75     public short getPrimitiveType() {
76         return unitType;
77     }
78
79     /**
80      * Returns the float value.
81      */

82     public float getFloatValue() {
83         return floatValue;
84     }
85
86     /**
87      *  A string representation of the current value.
88      */

89     public String getCssText() {
90         return getCssText(unitType, floatValue);
91     }
92
93     /**
94      * Returns a printable representation of this value.
95      */

96     public String toString() {
97         return getCssText();
98     }
99 }
100