001 package org.apache.myfaces.tobago.layout; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.slf4j.Logger; 021 import org.slf4j.LoggerFactory; 022 023 /* 024 * Date: 23.01.2008 20:21:08 025 */ 026 public final class PixelMeasure extends Measure { 027 028 private static final Logger LOG = LoggerFactory.getLogger(PixelMeasure.class); 029 030 static final Measure[] PIXEL_CACHE; 031 static final int PIXEL_CACHE_MAX = 4000; 032 033 static { 034 PIXEL_CACHE = new Measure[PIXEL_CACHE_MAX + 1]; 035 for (int i = 0; i < PIXEL_CACHE.length; i++) { 036 PIXEL_CACHE[i] = new PixelMeasure(i); 037 } 038 } 039 040 private final int pixel; 041 042 private PixelMeasure(int pixel) { 043 this.pixel = pixel; 044 } 045 046 static Measure pixelValueOf(int value) { 047 if (value >= 0 && value <= PixelMeasure.PIXEL_CACHE_MAX) { 048 return PixelMeasure.PIXEL_CACHE[value]; 049 } 050 return new PixelMeasure(value); 051 } 052 053 public Measure add(Measure m) { 054 if (m == null) { 055 return this; 056 } else { 057 return pixelValueOf(pixel + m.getPixel()); 058 } 059 } 060 061 public Measure add(int m) { 062 return pixelValueOf(pixel + m); 063 } 064 065 public Measure multiply(int times) { 066 return pixelValueOf(pixel * times); 067 } 068 069 public Measure divide(int times) { 070 return pixelValueOf(pixel / times); 071 } 072 073 public Measure subtractNotNegative(Measure m) { 074 if (m == null) { 075 return this; 076 } else if (m.getPixel() > pixel) { 077 LOG.warn("Not enough space! value=" + pixel); 078 return ZERO; 079 } else { 080 return pixelValueOf(pixel - m.getPixel()); 081 } 082 } 083 084 public Measure subtract(Measure m) { 085 if (m == null) { 086 return this; 087 } else { 088 return pixelValueOf(pixel - m.getPixel()); 089 } 090 } 091 092 public Measure subtract(int m) { 093 return pixelValueOf(pixel - m); 094 } 095 096 public int getPixel() { 097 return pixel; 098 } 099 100 @Override 101 public String toString() { 102 return pixel + "px"; 103 } 104 105 @Override 106 public boolean equals(Object o) { 107 if (this == o) { 108 return true; 109 } 110 if (o == null || getClass() != o.getClass()) { 111 return false; 112 } 113 114 PixelMeasure that = (PixelMeasure) o; 115 116 if (pixel != that.pixel) { 117 return false; 118 } 119 120 return true; 121 } 122 123 @Override 124 public int hashCode() { 125 return pixel; 126 } 127 }