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