KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > Comparison


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 package org.apache.tools.ant.types;
19
20 import java.util.Arrays JavaDoc;
21
22 import org.apache.tools.ant.BuildException;
23
24 /**
25  * EnumeratedAttribute for generic comparisons. Accepts values
26  * "equal", "greater", "more", "less", "ne" (not equal),
27  * "ge" (greater or equal), "le" (less or equal), "eq" (equal),
28  * "gt" (greater), "lt" (less).
29  * @since Ant 1.7
30  */

31 public class Comparison extends EnumeratedAttribute {
32     private static final String JavaDoc[] VALUES
33         = new String JavaDoc[] {"equal", "greater", "less",
34                         "ne", "ge", "le", "eq", "gt", "lt", "more"};
35
36     /** Equal Comparison. */
37     public static final Comparison EQUAL = new Comparison("equal");
38
39     /** Not-Equal Comparison. */
40     public static final Comparison NOT_EQUAL = new Comparison("ne");
41
42     /** Greater Comparison. */
43     public static final Comparison GREATER = new Comparison("greater");
44
45     /** Less Comparison. */
46     public static final Comparison LESS = new Comparison("less");
47
48     /** Greater-or-Equal Comparison. */
49     public static final Comparison GREATER_EQUAL = new Comparison("ge");
50
51     /** Less-or-Equal Comparison. */
52     public static final Comparison LESS_EQUAL = new Comparison("le");
53
54     private static final int[] EQUAL_INDEX = {0, 4, 5, 6};
55     private static final int[] LESS_INDEX = {2, 3, 5, 8};
56     private static final int[] GREATER_INDEX = {1, 3, 4, 7, 9};
57
58     /**
59      * Default constructor.
60      */

61     public Comparison() {
62     }
63
64     /**
65      * Construct a new Comparison with the specified value.
66      * @param value the EnumeratedAttribute value.
67      */

68     public Comparison(String JavaDoc value) {
69         setValue(value);
70     }
71
72     /**
73      * Return the possible values.
74      * @return String[] of EnumeratedAttribute values.
75      */

76     public String JavaDoc[] getValues() {
77         return VALUES;
78     }
79
80     /**
81      * Evaluate a comparison result as from Comparator.compare() or Comparable.compareTo().
82      * @param comparisonResult the result to evaluate.
83      * @return true if the comparison result fell within the parameters of this Comparison.
84      */

85     public boolean evaluate(int comparisonResult) {
86         if (getIndex() == -1) {
87             throw new BuildException("Comparison value not set.");
88         }
89         int[] i = comparisonResult < 0 ? LESS_INDEX
90             : comparisonResult > 0 ? GREATER_INDEX : EQUAL_INDEX;
91         return Arrays.binarySearch(i, getIndex()) >= 0;
92     }
93
94 }
95
96
Popular Tags