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.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  public final class PixelMeasure extends Measure {
26  
27    private static final Logger LOG = LoggerFactory.getLogger(PixelMeasure.class);
28  
29    static final PixelMeasure[] PIXEL_CACHE;
30    static final int PIXEL_CACHE_MAX = 4000;
31    
32    static {
33      PIXEL_CACHE = new PixelMeasure[PIXEL_CACHE_MAX + 1];
34      for (int i = 0; i < PIXEL_CACHE.length; i++) {
35        PIXEL_CACHE[i] = new PixelMeasure(i);
36      }
37    }
38    
39    private final int pixel;
40  
41    private PixelMeasure(int pixel) {
42      this.pixel = pixel;
43    }
44  
45    static PixelMeasure pixelValueOf(int value) {
46      if (value >= 0 && value <= PixelMeasure.PIXEL_CACHE_MAX) {
47        return PixelMeasure.PIXEL_CACHE[value];
48      }
49      return new PixelMeasure(value);
50    }
51    
52    public Measure add(Measure m) {
53      if (m == null) {
54        return this;
55      } else {
56        return pixelValueOf(pixel + m.getPixel());
57      }
58    }
59  
60    public Measure add(int m) {
61      return pixelValueOf(pixel + m);
62    }
63  
64    public Measure multiply(int times) {
65      return pixelValueOf(pixel * times);
66    }
67  
68    public Measure divide(int times) {
69      return pixelValueOf(pixel / times);
70    }
71  
72    public Measure subtractNotNegative(Measure m) {
73      if (m == null) {
74        return this;
75      } else if (m.getPixel() > pixel) {
76        LOG.warn("Not enough space! value=" + pixel);
77        return ZERO;
78      } else {
79        return pixelValueOf(pixel - m.getPixel());
80      }
81    }
82  
83    public Measure subtract(Measure m) {
84      if (m == null) {
85        return this;
86      } else {
87        return pixelValueOf(pixel - m.getPixel());
88      }
89    }
90  
91    public Measure subtract(int m) {
92        return pixelValueOf(pixel - m);
93    }
94  
95    public int getPixel() {
96      return pixel;
97    }
98  
99    @Override
100   public String toString() {
101     return serialize();
102   }
103 
104   @Override
105   public String serialize() {
106     return pixel + "px";
107   }
108 
109   @Override
110   public boolean equals(Object o) {
111     if (this == o) {
112       return true;
113     }
114     if (o == null || getClass() != o.getClass()) {
115       return false;
116     }
117 
118     PixelMeasure that = (PixelMeasure) o;
119 
120     if (pixel != that.pixel) {
121       return false;
122     }
123 
124     return true;
125   }
126 
127   @Override
128   public int hashCode() {
129     return pixel;
130   }
131 }