001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types;
019:
020: import org.apache.tools.ant.BuildException;
021:
022: /**
023: * EnumeratedAttribute for quantifier comparisons. Evaluates a
024: * <code>boolean[]</code> or raw <code>true</code> and <code>false</code>
025: * counts. Accepts the following values:<ul>
026: * <li>"all"</li> - none <code>false</code>
027: * <li>"each"</li> - none <code>false</code>
028: * <li>"every"</li> - none <code>false</code>
029: * <li>"any"</li> - at least one <code>true</code>
030: * <li>"some"</li> - at least one <code>true</code>
031: * <li>"one"</li> - exactly one <code>true</code>
032: * <li>"majority"</li> - more <code>true</code> than <code>false</code>
033: * <li>"most"</li> - more <code>true</code> than <code>false</code>
034: * <li>"none"</li> - none <code>true</code>
035: * </ul>
036: * @since Ant 1.7
037: */
038: public class Quantifier extends EnumeratedAttribute {
039: private static final String[] VALUES = new String[] { "all",
040: "each", "every", "any", "some", "one", "majority", "most",
041: "none" };
042:
043: /** ALL instance */
044: public static final Quantifier ALL = new Quantifier("all");
045: /** ANY instance */
046: public static final Quantifier ANY = new Quantifier("any");
047: /** ONE instance */
048: public static final Quantifier ONE = new Quantifier("one");
049: /** MAJORITY instance */
050: public static final Quantifier MAJORITY = new Quantifier("majority");
051: /** NONE instance */
052: public static final Quantifier NONE = new Quantifier("none");
053:
054: private abstract static class Predicate {
055: abstract boolean eval(int t, int f);
056: }
057:
058: private static final Predicate ALL_PRED = new Predicate() {
059: boolean eval(int t, int f) {
060: return f == 0;
061: }
062: };
063:
064: private static final Predicate ANY_PRED = new Predicate() {
065: boolean eval(int t, int f) {
066: return t > 0;
067: }
068: };
069:
070: private static final Predicate ONE_PRED = new Predicate() {
071: boolean eval(int t, int f) {
072: return t == 1;
073: }
074: };
075:
076: private static final Predicate MAJORITY_PRED = new Predicate() {
077: boolean eval(int t, int f) {
078: return t > f;
079: }
080: };
081:
082: private static final Predicate NONE_PRED = new Predicate() {
083: boolean eval(int t, int f) {
084: return t == 0;
085: }
086: };
087:
088: private static final Predicate[] PREDS = new Predicate[VALUES.length];
089:
090: static {
091: PREDS[0] = ALL_PRED;
092: PREDS[1] = ALL_PRED;
093: PREDS[2] = ALL_PRED;
094: PREDS[3] = ANY_PRED;
095: PREDS[4] = ANY_PRED;
096: PREDS[5] = ONE_PRED;
097: PREDS[6] = MAJORITY_PRED;
098: PREDS[7] = MAJORITY_PRED;
099: PREDS[8] = NONE_PRED;
100: }
101:
102: /**
103: * Default constructor.
104: */
105: public Quantifier() {
106: }
107:
108: /**
109: * Construct a new Quantifier with the specified value.
110: * @param value the EnumeratedAttribute value.
111: */
112: public Quantifier(String value) {
113: setValue(value);
114: }
115:
116: /**
117: * Return the possible values.
118: * @return String[] of EnumeratedAttribute values.
119: */
120: public String[] getValues() {
121: return VALUES;
122: }
123:
124: /**
125: * Evaluate a <code>boolean<code> array.
126: * @param b the <code>boolean[]</code> to evaluate.
127: * @return true if the argument fell within the parameters of this Quantifier.
128: */
129: public boolean evaluate(boolean[] b) {
130: int t = 0;
131: for (int i = 0; i < b.length; i++) {
132: if (b[i]) {
133: t++;
134: }
135: }
136: return evaluate(t, b.length - t);
137: }
138:
139: /**
140: * Evaluate integer <code>true</code> vs. <code>false</code> counts.
141: * @param t the number of <code>true</code> values.
142: * @param f the number of <code>false</code> values.
143: * @return true if the arguments fell within the parameters of this Quantifier.
144: */
145: public boolean evaluate(int t, int f) {
146: int index = getIndex();
147: if (index == -1) {
148: throw new BuildException("Quantifier value not set.");
149: }
150: return PREDS[index].eval(t, f);
151: }
152:
153: }
|