01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
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[] VALUES = new String[] { "equal",
33: "greater", "less", "ne", "ge", "le", "eq", "gt", "lt",
34: "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 value) {
69: setValue(value);
70: }
71:
72: /**
73: * Return the possible values.
74: * @return String[] of EnumeratedAttribute values.
75: */
76: public String[] 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: }
|