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