001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.template.utility;
054:
055: import java.math.BigDecimal;
056: import java.math.BigInteger;
057: import java.util.ArrayList;
058: import java.util.Collections;
059: import java.util.List;
060:
061: /**
062: * @version $Id: OptimizerUtil.java,v 1.9 2003/02/25 11:52:58 szegedia Exp $
063: * @author Attila Szegedi
064: */
065: public class OptimizerUtil {
066: private static final BigInteger INTEGER_MIN = new BigInteger(
067: Integer.toString(Integer.MIN_VALUE));
068: private static final BigInteger INTEGER_MAX = new BigInteger(
069: Integer.toString(Integer.MAX_VALUE));
070: private static final BigInteger LONG_MIN = new BigInteger(Long
071: .toString(Long.MIN_VALUE));
072: private static final BigInteger LONG_MAX = new BigInteger(Long
073: .toString(Long.MAX_VALUE));
074:
075: private OptimizerUtil() {
076: }
077:
078: public static List optimizeListStorage(List list) {
079: switch (list.size()) {
080: case 0: {
081: return Collections.EMPTY_LIST;
082: }
083: case 1: {
084: return Collections12.singletonList(list.get(0));
085: }
086: default: {
087: if (list instanceof ArrayList) {
088: ((ArrayList) list).trimToSize();
089: }
090: return list;
091: }
092: }
093: }
094:
095: /**
096: * This is needed to reverse the extreme conversions in arithmetic
097: * operations so that numbers can be meaningfully used with models that
098: * don't know what to do with a BigDecimal. Of course, this will make
099: * impossible for these models (i.e. Jython) to receive a BigDecimal even if
100: * it was originally placed as such in the data model. However, since
101: * arithmetic operations aggressively erase the information regarding the
102: * original number type, we have no other choice to ensure expected operation
103: * in majority of cases.
104: */
105: public static Number optimizeNumberRepresentation(Number number) {
106: if (number instanceof BigDecimal) {
107: BigDecimal bd = (BigDecimal) number;
108: if (bd.scale() == 0) {
109: // BigDecimal -> BigInteger
110: number = bd.unscaledValue();
111: } else {
112: double d = bd.doubleValue();
113: if (d != Double.POSITIVE_INFINITY
114: && d != Double.NEGATIVE_INFINITY) {
115: // BigDecimal -> Double
116: return new Double(d);
117: }
118: }
119: }
120: if (number instanceof BigInteger) {
121: BigInteger bi = (BigInteger) number;
122: if (bi.compareTo(INTEGER_MAX) <= 0
123: && bi.compareTo(INTEGER_MIN) >= 0) {
124: // BigInteger -> Integer
125: return new Integer(bi.intValue());
126: }
127: if (bi.compareTo(LONG_MAX) <= 0
128: && bi.compareTo(LONG_MIN) >= 0) {
129: // BigInteger -> Long
130: return new Long(bi.longValue());
131: }
132: }
133: return number;
134: }
135: }
|