View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.layout;
21  
22  import org.apache.myfaces.tobago.internal.util.StringUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.io.Serializable;
27  
28  /**
29   * In JSPs the class {@link org.apache.myfaces.tobago.layout.MeasureEditor} will convert the string literals.
30   */
31  public final class Measure implements Serializable {
32  
33    private static final long serialVersionUID = 1L;
34  
35    private static final Logger LOG = LoggerFactory.getLogger(Measure.class);
36  
37    public static final Measure ZERO = valueOf(0);
38  
39    // todo: refactor and consolidate with LayoutToken
40  
41    private final Double d;
42    private final Integer i;
43    private final Unit unit;
44  
45    public Measure(int i, Unit unit) {
46      this.d = null;
47      this.i = i;
48      this.unit = unit;
49    }
50  
51    public Measure(double d, Unit unit) {
52      this.d = d;
53      this.i = null;
54      this.unit = unit;
55    }
56  
57    public Measure(String string, Unit unit) {
58      if (string.contains(".")) {
59        this.d = Double.parseDouble(string);
60        this.i = null;
61      } else {
62        this.d = null;
63        this.i = Integer.parseInt(string);
64      }
65      this.unit = unit;
66    }
67  
68    public static Measure valueOf(final Measure value) {
69      if (value == null) {
70        return ZERO;
71      }
72      return value;
73    }
74  
75    public static Measure valueOf(final int value) {
76      return new Measure(value, Unit.PX);
77    }
78  
79    public static Measure valueOf(final Integer value) {
80      if (value == null) {
81        return ZERO;
82      }
83      return valueOf(value.intValue());
84    }
85  
86    public static Measure valueOf(final Number value) {
87      if (value == null) {
88        return ZERO;
89      }
90      return valueOf(value.doubleValue());
91    }
92  
93    public static Measure valueOf(final String value) {
94      try {
95        if (StringUtils.isEmpty(value)) {
96          return null;
97        }
98        final int length = value.length();
99        if (value.endsWith("%")) {
100         return new Measure(value.substring(0, length - 1), Unit.PERCENT);
101       }
102       if (length >= 2 && Character.isLetter(value.charAt(length - 2))) {
103         return new Measure(value.substring(0, length - 2), Unit.valueOf(value.substring(length - 2).toUpperCase()));
104       }
105       return new Measure(value, Unit.PX);
106 
107     } catch (final RuntimeException e) {
108       LOG.warn("Can't parse to any measure: '" + value + "'");
109     }
110     return null;
111   }
112 
113   public static Measure valueOf(final Object object) {
114     if (object instanceof Measure) {
115       return valueOf((Measure) object);
116     }
117     if (object instanceof Number) {
118       return valueOf((Number) object);
119     }
120     if (object instanceof String) {
121       return valueOf((String) object);
122     }
123     if (object == null) {
124       return ZERO;
125     }
126     return valueOf(object.toString());
127   }
128 
129   public String serialize() {
130     return "" + (i != null ? i : "") + (d != null ? d : "") + unit.getValue();
131   }
132 
133   public String toString() {
134     return serialize();
135   }
136 
137   @Override
138   public boolean equals(Object o) {
139     if (this == o) {
140       return true;
141     }
142     if (o == null || getClass() != o.getClass()) {
143       return false;
144     }
145 
146     Measure measure = (Measure) o;
147 
148     if (d != null ? !d.equals(measure.d) : measure.d != null) {
149       return false;
150     }
151     if (i != null ? !i.equals(measure.i) : measure.i != null) {
152       return false;
153     }
154     return unit == measure.unit;
155 
156   }
157 
158   @Override
159   public int hashCode() {
160     int result = d != null ? d.hashCode() : 0;
161     result = 31 * result + (i != null ? i.hashCode() : 0);
162     result = 31 * result + unit.hashCode();
163     return result;
164   }
165 
166   private enum Unit {
167 
168     EM,
169     PX,
170     EX,
171     PT,
172     CM,
173     MM,
174     IN,
175     PC,
176     PERCENT;
177 
178     private final String value;
179 
180     Unit() {
181       value = name().equals("PERCENT") ? "%" : name().toLowerCase();
182     }
183 
184     String getValue() {
185       return value;
186     }
187   }
188 }