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  import java.util.List;
28  
29  /**
30   *  In JSPs the class {@link org.apache.myfaces.tobago.layout.MeasureEditor} will convert the string literals.
31   */
32  public abstract class Measure implements Serializable {
33  
34    private static final long serialVersionUID = 1L;
35  
36    private static final Logger LOG = LoggerFactory.getLogger(Measure.class);
37  
38    public static final Measure ZERO = valueOf(0);
39    public static final Measure MAX = valueOf(Integer.MAX_VALUE);
40  
41    // todo: refactor and consolidate with LayoutToken
42    
43    public static Measure valueOf(final Measure value) {
44      if (value == null) {
45        return ZERO;
46      }
47      return value;
48    }
49  
50    public static Measure valueOf(final int value) {
51      return PixelMeasure.pixelValueOf(value);
52    }
53  
54    public static Measure valueOf(final Integer value) {
55      if (value == null) {
56        return ZERO;
57      }
58      return valueOf(value.intValue());
59    }
60  
61    public static Measure valueOf(final Number value) {
62      if (value == null) {
63        return ZERO;
64      }
65      return valueOf(value.intValue());
66    }
67  
68    public static Measure valueOf(final String value) {
69      if (StringUtils.isEmpty(value)) {
70        return ZERO;
71      }
72      try {
73        final int dot = value.indexOf('.');
74        if (dot == 0) {
75          return Measure.ZERO;
76        }
77        if (dot > 0) {
78          return Measure.valueOf(Integer.parseInt(value.substring(0, dot)));
79        }
80        if (value.endsWith("px")) {
81          return Measure.valueOf(Integer.parseInt(value.substring(0, value.length() - 2)));
82        }
83        return Measure.valueOf(Integer.parseInt(value));
84  
85      } catch (final NumberFormatException e) {
86        throw new IllegalArgumentException("Can't parse to any measure: '" + value + "'", e);
87      }
88    }
89  
90    public static Measure valueOf(final Object object) {
91      if (object instanceof Measure) {
92        return valueOf((Measure) object);
93      }
94      if (object instanceof Number) {
95        return valueOf((Number) object);
96      }
97      if (object instanceof String) {
98        return valueOf((String) object);
99      }
100     if (object == null) {
101       return ZERO;
102     }
103     return valueOf(object.toString());
104   }
105 
106   /**
107    * @deprecated since 1.5.0, please use valueOf()
108    */
109   @Deprecated
110   public static Measure parse(final String value) {
111     return valueOf(value);
112   }
113 
114   public abstract Measure add(Measure m);
115 
116   public abstract Measure add(int m);
117 
118   public abstract Measure multiply(int times);
119 
120   public abstract Measure divide(int times);
121 
122   /**
123    * @deprecated since 1.5.0, please use subtractNotNegative
124    */
125   @Deprecated
126   public Measure substractNotNegative(final Measure m) {
127     return subtractNotNegative(m);
128   }
129 
130   public abstract Measure subtractNotNegative(Measure m);
131 
132   public abstract Measure subtract(Measure m);
133 
134   public abstract Measure subtract(int m);
135 
136   public boolean greaterThan(final Measure measure) {
137     return measure != null && getPixel() > measure.getPixel();
138   }
139 
140   public boolean greaterOrEqualThan(final Measure measure) {
141     return measure != null && getPixel() >= measure.getPixel();
142   }
143 
144   public boolean lessThan(final Measure measure) {
145     return measure != null && getPixel() < measure.getPixel();
146   }
147 
148   public boolean lessOrEqualThan(final Measure measure) {
149     return measure != null && getPixel() <= measure.getPixel();
150   }
151 
152   public abstract int getPixel();
153 
154   public abstract String serialize();
155 
156   /**
157    * Returns the maximum. If all parameters are null, than the result is {@value #ZERO}.
158    */
159   public static Measure max(final List<Measure> list) {
160     Measure max = ZERO;
161     for (final Measure measure : list) {
162       if (max.lessThan(measure)) {
163         max = measure;
164       }
165     }
166     return max;
167   }
168 
169   /**
170    * Returns the minimum. If all parameters are null, than the result is {@value #MAX}.
171    */
172   public static Measure min(final List<Measure> list) {
173     Measure min = MAX;
174     for (final Measure measure : list) {
175       if (min.greaterThan(measure)) {
176         min = measure;
177       }
178     }
179     return min;
180   }
181 
182   /**
183    * Returns the maximum. If all parameters are null, than the result is {@value #ZERO}.
184    */
185   public static Measure max(final Measure m1, final Measure m2) {
186     if (m1 != null) {
187       return m1.lessThan(m2) ? m2 : m1;
188     } else {
189       return m2 != null ? m2 : ZERO;
190     }
191   }
192 
193   /**
194    * Returns the minimum. If all parameters are null, than the result is {@value #MAX}.
195    */
196   public static Measure min(final Measure m1, final Measure m2) {
197     if (m1 != null) {
198       return m1.greaterThan(m2) ? m2 : m1;
199     } else {
200       return m2 != null ? m2 : MAX;
201     }
202   }
203 }