View Javadoc

1   /*
2    * This file is part of Pease Plate Template Engine.
3    * 
4    * Pease Plate Template Engine is free software: you can redistribute
5    * it and/or modify it under the terms of the GNU Lesser General 
6    * Public License as published by the Free Software Foundation, 
7    * either version 3 of the License, or any later version.
8    * 
9    * Pease Plate Template Engine is distributed in the hope that it 
10   * will be useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12   * See the GNU Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public 
15   * License along with Pease Plate Template Engine. If not, see 
16   * <http://www.gnu.org/licenses/>.
17   * 
18   * Copyright (c) 2008 Manfred HANTSCHEL
19   */
20  package org.peaseplate.internal.lang.command;
21  
22  import org.peaseplate.TemplateRuntimeException;
23  import org.peaseplate.locator.TemplateLocator;
24  
25  public class LessThanCommand extends AbstractCompareCommand {
26  
27  	public LessThanCommand(TemplateLocator locator, int line, int column, ICommand leftCommand, ICommand rightCommand) {
28  		super(locator, line, column, leftCommand, rightCommand);
29  	}
30  
31  	/**
32  	 * @see org.peaseplate.internal.lang.command.AbstractNumericCommand#getSign()
33  	 */
34  	@Override
35  	public String getSign() {
36  		return "<";
37  	}
38  
39  	/**
40  	 * @see org.peaseplate.internal.lang.command.AbstractCompareCommand#evaluateNull()
41  	 */
42  	@Override
43      public Boolean evaluateNull() throws TemplateRuntimeException {
44  		return Boolean.FALSE;
45  	}
46  	
47  	/**
48  	 * @see org.peaseplate.internal.lang.command.AbstractCompareCommand#evaluateComparable(java.lang.Comparable, java.lang.Comparable)
49  	 */
50  	@SuppressWarnings("unchecked")
51      @Override
52  	public <TYPE> Boolean evaluateComparable(Comparable<TYPE> left, Comparable<TYPE> right) throws TemplateRuntimeException {
53  		TYPE typedRight = null;
54  		
55  		try {
56  			typedRight = (TYPE)right;
57  		}
58  		catch (ClassCastException e) {
59  			Class<?> leftType = (left != null) ? left.getClass() : Void.class;
60  			Class<?> rightType = (right != null) ? right.getClass() : Void.class;
61  
62  			throw new TemplateRuntimeException(
63  				getLocator(), getLine(), getColumn(),
64  				"Operation " + getSign() + " not supported on operands of type " + leftType + " and " + rightType, e
65  			);
66  		}
67  		
68  		return Boolean.valueOf(left.compareTo(typedRight) < 0);
69  	}
70  	
71  	/**
72  	 * @see org.peaseplate.internal.lang.command.AbstractCompareCommand#evaluateNonComparable(java.lang.Object, java.lang.Object)
73  	 */
74      @Override
75      @SuppressWarnings("unchecked")
76  	public Boolean evaluateNonComparable(Object left, Object right) throws TemplateRuntimeException {
77  		if ((left instanceof Comparable) && (right instanceof Comparable)) {
78  			Comparable<Object> leftComparable = null;
79  			
80  			try {
81  				leftComparable = (Comparable<Object>)left;
82  			}
83  			catch (ClassCastException e) {
84  				throw new TemplateRuntimeException(
85  					getLocator(), getLine(), getColumn(),
86  					"Operation " + getSign() + " not supported on operands of type " + left.getClass() + " and " + right.getClass(), e
87  				);
88  			}
89  			
90  			return Boolean.valueOf(leftComparable.compareTo(right) < 0);
91  		}
92  		
93  		return super.evaluateNonComparable(left, right);
94  	}
95      
96  }