| 1 | /* |
| 2 | * Copyright (c) 2001-2004, 2007 Ant-Contrib project. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package net.sf.antcontrib.logic.condition; |
| 17 | |
| 18 | import org.apache.tools.ant.BuildException; |
| 19 | import org.apache.tools.ant.taskdefs.condition.Condition; |
| 20 | |
| 21 | /** |
| 22 | * Condition to test if the first argument is greater than the |
| 23 | * second argument. Will deal with base 10 integer and decimal numbers, otherwise, |
| 24 | * treats arguments as Strings. |
| 25 | * <p>Developed for use with Antelope, migrated to ant-contrib Oct 2003. |
| 26 | * |
| 27 | * @author Dale Anson, [email protected] |
| 28 | * @version $Revision: 1.4 $ |
| 29 | */ |
| 30 | public final class IsGreaterThan implements Condition { |
| 31 | |
| 32 | private String arg1, arg2; |
| 33 | private boolean trim = false; |
| 34 | private boolean caseSensitive = true; |
| 35 | |
| 36 | public void setArg1(String a1) { |
| 37 | arg1 = a1; |
| 38 | } |
| 39 | |
| 40 | public void setArg2(String a2) { |
| 41 | arg2 = a2; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Should we want to trim the arguments before comparing them? |
| 46 | * |
| 47 | * @since Revision: 1.3, Ant 1.5 |
| 48 | */ |
| 49 | public void setTrim(boolean b) { |
| 50 | trim = b; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Should the comparison be case sensitive? |
| 55 | * |
| 56 | * @since Revision: 1.3, Ant 1.5 |
| 57 | */ |
| 58 | public void setCasesensitive(boolean b) { |
| 59 | caseSensitive = b; |
| 60 | } |
| 61 | |
| 62 | public boolean eval() throws BuildException { |
| 63 | if (arg1 == null || arg2 == null) { |
| 64 | throw new BuildException("both arg1 and arg2 are required in " |
| 65 | + "greater than"); |
| 66 | } |
| 67 | |
| 68 | if (trim) { |
| 69 | arg1 = arg1.trim(); |
| 70 | arg2 = arg2.trim(); |
| 71 | } |
| 72 | |
| 73 | // check if args are numbers |
| 74 | try { |
| 75 | double num1 = Double.parseDouble(arg1); |
| 76 | double num2 = Double.parseDouble(arg2); |
| 77 | return num1 > num2; |
| 78 | } |
| 79 | catch(NumberFormatException nfe) { |
| 80 | // ignored, fall thru to string comparision |
| 81 | } |
| 82 | |
| 83 | return caseSensitive ? arg1.compareTo(arg2) > 0 : arg1.compareToIgnoreCase(arg2) > 0; |
| 84 | } |
| 85 | |
| 86 | } |