KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > core > ComparisonExpression


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.core;
54
55 import java.util.Date JavaDoc;
56 import freemarker.template.*;
57
58 /**
59  * A class that handles comparisons.
60  * @author <a HREF="mailto:jon@revusky.com">Jonathan Revusky</a>
61  * @author Attila Szegedi
62  */

63
64 final class ComparisonExpression extends BooleanExpression {
65
66     static final int EQUALS=1;
67     static final int NOT_EQUALS=2;
68     static final int LESS_THAN=3;
69     static final int GREATER_THAN=4;
70     static final int LESS_THAN_EQUALS=5;
71     static final int GREATER_THAN_EQUALS=6;
72
73     private final Expression left;
74     private final Expression right;
75     private final int operation;
76     private final String JavaDoc opString;
77
78     ComparisonExpression(Expression left, Expression right, String JavaDoc opString) {
79         this.left = left;
80         this.right = right;
81         opString = opString.intern();
82         this.opString = opString;
83         if (opString == "==" || opString == "=") {
84             operation = EQUALS;
85         }
86         else if (opString == "!=") {
87             operation = NOT_EQUALS;
88         }
89         else if (opString == "gt" || opString == "\\gt" || opString == ">" || opString == "&gt;") {
90             operation = GREATER_THAN;
91         }
92         else if (opString == "gte" || opString == "\\gte" || opString == ">=" || opString == "&gt;=") {
93             operation = GREATER_THAN_EQUALS;
94         }
95         else if (opString== "lt" || opString == "\\lt" || opString == "<" || opString == "&lt;") {
96             operation = LESS_THAN;
97         }
98         else if (opString == "lte" || opString == "\\lte" || opString == "<=" || opString == "&lt;=") {
99             operation = LESS_THAN_EQUALS;
100         }
101         else {
102             throw new RuntimeException JavaDoc("Unknown comparison operator " + opString);
103         }
104     }
105
106     /*
107      * WARNING! This algorithm is duplicated in SequenceBuiltins.modelsEqual.
108      * Thus, if you update this method, then you have to update that too!
109      */

110     boolean isTrue(Environment env) throws TemplateException {
111         TemplateModel ltm = left.getAsTemplateModel(env);
112         TemplateModel rtm = right.getAsTemplateModel(env);
113         if (env != null && env.isClassicCompatible()) {
114             if (ltm == null) {
115                 ltm = TemplateScalarModel.EMPTY_STRING;
116             }
117             if (rtm == null) {
118                 rtm = TemplateScalarModel.EMPTY_STRING;
119             }
120         }
121         assertNonNull(ltm, left, env);
122         assertNonNull(rtm, right, env);
123         int comp = 0;
124         if(ltm instanceof TemplateNumberModel && rtm instanceof TemplateNumberModel) {
125             Number JavaDoc first = EvaluationUtil.getNumber((TemplateNumberModel)ltm, left, env);
126             Number JavaDoc second = EvaluationUtil.getNumber((TemplateNumberModel)rtm, right, env);
127             ArithmeticEngine ae =
128                 env != null
129                     ? env.getArithmeticEngine()
130                     : getTemplate().getArithmeticEngine();
131             comp = ae.compareNumbers(first, second);
132         }
133         else if(ltm instanceof TemplateDateModel && rtm instanceof TemplateDateModel) {
134             TemplateDateModel ltdm = (TemplateDateModel)ltm;
135             TemplateDateModel rtdm = (TemplateDateModel)rtm;
136             int ltype = ltdm.getDateType();
137             int rtype = rtdm.getDateType();
138             if(ltype != rtype) {
139                 throw new TemplateException(
140                     "Can not compare dates of different type. Left date is of "
141                     + TemplateDateModel.TYPE_NAMES.get(ltype)
142                     + " type, right date is of "
143                     + TemplateDateModel.TYPE_NAMES.get(rtype) + " type.",
144                     env);
145             }
146             if(ltype == TemplateDateModel.UNKNOWN) {
147                 throw new TemplateException(
148                     "Left date is of UNKNOWN type, and can not be compared.", env);
149             }
150             if(rtype == TemplateDateModel.UNKNOWN) {
151                 throw new TemplateException(
152                     "Right date is of UNKNOWN type, and can not be compared.", env);
153             }
154             
155             Date JavaDoc first = EvaluationUtil.getDate(ltdm, left, env);
156             Date JavaDoc second = EvaluationUtil.getDate(rtdm, right, env);
157             comp = first.compareTo(second);
158         }
159         else if(ltm instanceof TemplateScalarModel && rtm instanceof TemplateScalarModel) {
160             if(operation != EQUALS && operation != NOT_EQUALS) {
161                 throw new TemplateException("Can not use operator " + opString + " on string values.", env);
162             }
163             String JavaDoc first = EvaluationUtil.getString((TemplateScalarModel)ltm, left, env);
164             String JavaDoc second = EvaluationUtil.getString((TemplateScalarModel)rtm, right, env);
165             comp = env.getCollator().compare(first, second);
166         }
167         else if(ltm instanceof TemplateBooleanModel && rtm instanceof TemplateBooleanModel) {
168             if(operation != EQUALS && operation != NOT_EQUALS) {
169                 throw new TemplateException("Can not use operator " + opString + " on boolean values.", env);
170             }
171             boolean first = ((TemplateBooleanModel)ltm).getAsBoolean();
172             boolean second = ((TemplateBooleanModel)rtm).getAsBoolean();
173             comp = (first ? 1 : 0) - (second ? 1 : 0);
174         }
175         // Here we handle compatibility issues
176
else if(env.isClassicCompatible()) {
177             String JavaDoc first = left.getStringValue(env);
178             String JavaDoc second = right.getStringValue(env);
179             comp = env.getCollator().compare(first, second);
180         }
181         else {
182             throw new TemplateException(
183                 "The only legal comparisons are between two numbers, two strings, or two dates.\n"
184                 + "Left hand operand is a " + ltm.getClass().getName() + "\n"
185                 + "Right hand operand is a " + rtm.getClass().getName() + "\n"
186                 , env);
187         }
188         switch (operation) {
189             case EQUALS:
190                 return comp == 0;
191             case NOT_EQUALS:
192                 return comp != 0;
193             case LESS_THAN :
194                 return comp < 0;
195             case GREATER_THAN :
196                 return comp > 0;
197             case LESS_THAN_EQUALS :
198                 return comp <= 0;
199             case GREATER_THAN_EQUALS :
200                 return comp >= 0;
201             default :
202                 throw new TemplateException("unknown operation", env);
203         }
204     }
205
206     public String JavaDoc getCanonicalForm() {
207         return left.getCanonicalForm() + ' ' + opString + ' ' + right.getCanonicalForm();
208     }
209
210     boolean isLiteral() {
211         return constantValue != null || (left.isLiteral() && right.isLiteral());
212     }
213
214     Expression _deepClone(String JavaDoc name, Expression subst) {
215         return new ComparisonExpression(left.deepClone(name, subst), right.deepClone(name, subst), opString);
216     }
217 }
218
Popular Tags