KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > Equals


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.condition;
20
21 import org.apache.tools.ant.BuildException;
22
23 /**
24  * Simple String comparison condition.
25  *
26  * @since Ant 1.4
27  */

28 public class Equals implements Condition {
29
30     private String JavaDoc arg1, arg2;
31     private boolean trim = false;
32     private boolean caseSensitive = true;
33
34     /**
35      * Set the first string
36      *
37      * @param a1 the first string
38      */

39     public void setArg1(String JavaDoc a1) {
40         arg1 = a1;
41     }
42
43     /**
44      * Set the second string
45      *
46      * @param a2 the second string
47      */

48     public void setArg2(String JavaDoc a2) {
49         arg2 = a2;
50     }
51
52     /**
53      * Should we want to trim the arguments before comparing them?
54      * @param b if true trim the arguments
55      * @since Ant 1.5
56      */

57     public void setTrim(boolean b) {
58         trim = b;
59     }
60
61     /**
62      * Should the comparison be case sensitive?
63      * @param b if true use a case sensitive comparison (this is the
64      * default)
65      * @since Ant 1.5
66      */

67     public void setCasesensitive(boolean b) {
68         caseSensitive = b;
69     }
70
71     /**
72      * @return true if the two strings are equal
73      * @exception BuildException if the attributes are not set correctly
74      */

75     public boolean eval() throws BuildException {
76         if (arg1 == null || arg2 == null) {
77             throw new BuildException("both arg1 and arg2 are required in "
78                                      + "equals");
79         }
80
81         if (trim) {
82             arg1 = arg1.trim();
83             arg2 = arg2.trim();
84         }
85
86         return caseSensitive ? arg1.equals(arg2) : arg1.equalsIgnoreCase(arg2);
87     }
88 }
89
Popular Tags