1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*
* Copyright (C) 2001-2013 Michael Fuchs
*
* This file is part of herold.
*
* herold is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* herold is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with herold. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dbdoclet.unit;
import java.text.NumberFormat;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbdoclet.service.StringServices;
/**
* Längenangabe mit zugehöriger Einheit.
*
* @author michael
*/
public class Length {
private static final LengthUnit DEFAULT_UNIT = LengthUnit.MILLIMETER;
private static Log logger = LogFactory.getLog(Length.class);
private Double distance = null;
private boolean excludePercent = true;
private Locale locale = Locale.getDefault();
private LengthUnit unit = LengthUnit.MILLIMETER;
public Length() {
this(Locale.getDefault(), null, null);
}
public Length(Double distance) {
this(Locale.getDefault(), distance, DEFAULT_UNIT);
}
public Length(Double distance, LengthUnit unit) {
this(Locale.getDefault(), distance, unit);
}
public Length(float distance, LengthUnit unit) {
this(Locale.getDefault(), new Double(distance), unit);
}
public Length(int top) {
this(Locale.getDefault(), new Double(top), DEFAULT_UNIT);
}
public Length(Locale locale, Double distance, LengthUnit unit) {
this.setLocale(locale);
this.distance = distance;
this.unit = unit;
}
public static double toMillimeter(Object value) {
if (value instanceof Length == false) {
throw new IllegalArgumentException(
"The argument value must be of type Distance!");
}
Length length = (Length) value;
if (length.getUnit() == LengthUnit.POINT) {
return (length.getLength() / 72.0f) * 25.4f;
}
if (length.getUnit() == LengthUnit.INCH) {
return length.getLength() * 25.4f;
}
if (length.getUnit() == LengthUnit.CENTIMETER) {
return length.getLength() * 10;
}
return length.getLength();
}
public static Length valueOf(String value) {
return valueOf(value, true);
}
public static Length valueOf(String value, boolean excludePercent) {
Length distance = new Length(Locale.US, null, DEFAULT_UNIT);
distance.setExcludePercent(excludePercent);
if (distance.processValue(Locale.US, value) == false) {
return null;
}
return distance;
}
public void decrDistance(float decr) {
if (distance != null) {
distance -= decr;
}
}
public Length deepCopy() {
if (locale == null) {
locale = Locale.getDefault();
}
if (unit == null) {
unit = LengthUnit.MILLIMETER;
}
if (distance == null) {
return new Length(locale, null, unit);
}
Length copy = new Length(locale, new Double(distance), unit);
return copy;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Length other = (Length) obj;
if (distance == null && other.distance != null) {
return false;
}
if (distance == null && other.distance == null) {
return false;
}
if (distance.equals(other.distance) == false) {
return false;
}
if (unit == null && other.unit != null) {
return false;
}
if (unit.equals(other.unit) == false) {
return false;
}
return true;
}
public boolean excludePercent() {
return excludePercent;
}
public float getLength() {
if (distance == null) {
return 0;
}
return distance.floatValue();
}
public Locale getLocale() {
return locale;
}
public LengthUnit getUnit() {
return unit;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((distance == null) ? 0 : distance.hashCode());
result = prime * result + ((unit == null) ? 0 : unit.hashCode());
return result;
}
public void incrDistance(float incr) {
if (distance != null) {
distance += incr;
}
}
public Length minus(Length operand) {
if (operand == null) {
return this.deepCopy();
}
double sum = this.toMillimeter() - operand.toMillimeter();
return new Length(sum, LengthUnit.MILLIMETER);
}
public Length plus(Length operand) {
if (operand == null) {
return this.deepCopy();
}
double sum = this.toMillimeter() + operand.toMillimeter();
return new Length(sum, LengthUnit.MILLIMETER);
}
public boolean processValue(Locale locale, String value) {
if (value == null || value.trim().length() == 0) {
unit = null;
distance = null;
return false;
}
String tu = null;
double td;
value = value.trim().toLowerCase();
for (LengthUnit unit : LengthUnit.values()) {
if (excludePercent() == true && unit.equals(LengthUnit.PERCENT)) {
continue;
}
if (value.endsWith(unit.getAbbreviation())) {
tu = unit.getAbbreviation();
break;
}
}
if (tu == null) {
tu = LengthUnit.POINT.getAbbreviation();
}
value = StringServices.cutSuffix(value, tu);
try {
NumberFormat formatter = NumberFormat.getInstance(locale);
Number num = formatter.parse(value);
td = num.doubleValue();
} catch (Throwable oops) {
logger.error("Invalid value " + value + " for length.", oops);
return false;
}
unit = LengthUnit.valueOfAbbrev(tu);
distance = td;
return true;
}
public void setDistance(double distance) {
this.distance = distance;
}
public void setExcludePercent(boolean excludePercent) {
this.excludePercent = excludePercent;
}
public void setLength(double length) {
this.distance = length;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public void setLocalizedValue(String value) {
processValue(getLocale(), value);
}
public void setUnit(LengthUnit unit) {
this.unit = unit;
}
public double toMillimeter() {
return toMillimeter(this);
}
public String toNormalizedString() {
if (distance == null) {
return null;
}
if (unit == LengthUnit.PERCENT) {
return String.format(Locale.US, "%.0f%s", distance,
unit.getAbbreviation());
}
return String.format(Locale.US, "%.2f%s", distance,
unit.getAbbreviation());
}
public double toPoint() {
double millis = toMillimeter();
return millis * 2.834645669;
}
@Override
public String toString() {
if (distance == null) {
return null;
}
return String.format(getLocale(), "%.2f %s", distance,
unit.getAbbreviation());
}
}
|