KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > template > utility > OptimizerUtil


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.template.utility;
54
55 import java.math.BigDecimal JavaDoc;
56 import java.math.BigInteger JavaDoc;
57 import java.util.ArrayList JavaDoc;
58 import java.util.Collections JavaDoc;
59 import java.util.List JavaDoc;
60
61 /**
62  * @version $Id: OptimizerUtil.java,v 1.9 2003/02/25 11:52:58 szegedia Exp $
63  * @author Attila Szegedi
64  */

65 public class OptimizerUtil
66 {
67     private static final BigInteger JavaDoc INTEGER_MIN = new BigInteger JavaDoc(Integer.toString(Integer.MIN_VALUE));
68     private static final BigInteger JavaDoc INTEGER_MAX = new BigInteger JavaDoc(Integer.toString(Integer.MAX_VALUE));
69     private static final BigInteger JavaDoc LONG_MIN = new BigInteger JavaDoc(Long.toString(Long.MIN_VALUE));
70     private static final BigInteger JavaDoc LONG_MAX = new BigInteger JavaDoc(Long.toString(Long.MAX_VALUE));
71
72     private OptimizerUtil()
73     {
74     }
75     
76     public static List JavaDoc optimizeListStorage(List JavaDoc list)
77     {
78         switch(list.size())
79         {
80             case 0:
81             {
82                 return Collections.EMPTY_LIST;
83             }
84             case 1:
85             {
86                 return Collections12.singletonList(list.get(0));
87             }
88             default:
89             {
90                 if(list instanceof ArrayList JavaDoc)
91                 {
92                     ((ArrayList JavaDoc)list).trimToSize();
93                 }
94                 return list;
95             }
96         }
97     }
98     
99     /**
100      * This is needed to reverse the extreme conversions in arithmetic
101      * operations so that numbers can be meaningfully used with models that
102      * don't know what to do with a BigDecimal. Of course, this will make
103      * impossible for these models (i.e. Jython) to receive a BigDecimal even if
104      * it was originally placed as such in the data model. However, since
105      * arithmetic operations aggressively erase the information regarding the
106      * original number type, we have no other choice to ensure expected operation
107      * in majority of cases.
108      */

109     public static Number JavaDoc optimizeNumberRepresentation(Number JavaDoc number)
110     {
111         if(number instanceof BigDecimal JavaDoc)
112         {
113             BigDecimal JavaDoc bd = (BigDecimal JavaDoc) number;
114             if(bd.scale() == 0)
115             {
116                 // BigDecimal -> BigInteger
117
number = bd.unscaledValue();
118             }
119             else
120             {
121                 double d = bd.doubleValue();
122                 if(d != Double.POSITIVE_INFINITY && d != Double.NEGATIVE_INFINITY)
123                 {
124                     // BigDecimal -> Double
125
return new Double JavaDoc(d);
126                 }
127             }
128         }
129         if(number instanceof BigInteger JavaDoc)
130         {
131             BigInteger JavaDoc bi = (BigInteger JavaDoc)number;
132             if(bi.compareTo(INTEGER_MAX) <= 0 && bi.compareTo(INTEGER_MIN) >= 0)
133             {
134                 // BigInteger -> Integer
135
return new Integer JavaDoc(bi.intValue());
136             }
137             if(bi.compareTo(LONG_MAX) <= 0 && bi.compareTo(LONG_MIN) >= 0)
138             {
139                 // BigInteger -> Long
140
return new Long JavaDoc(bi.longValue());
141             }
142         }
143         return number;
144     }
145 }
146
Popular Tags