KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.BuildException;
21
22 /**
23  * EnumeratedAttribute for quantifier comparisons. Evaluates a
24  * <code>boolean[]</code> or raw <code>true</code> and <code>false</code>
25  * counts. Accepts the following values:<ul>
26  * <li>"all"</li> - none <code>false</code>
27  * <li>"each"</li> - none <code>false</code>
28  * <li>"every"</li> - none <code>false</code>
29  * <li>"any"</li> - at least one <code>true</code>
30  * <li>"some"</li> - at least one <code>true</code>
31  * <li>"one"</li> - exactly one <code>true</code>
32  * <li>"majority"</li> - more <code>true</code> than <code>false</code>
33  * <li>"most"</li> - more <code>true</code> than <code>false</code>
34  * <li>"none"</li> - none <code>true</code>
35  * </ul>
36  * @since Ant 1.7
37  */

38 public class Quantifier extends EnumeratedAttribute {
39     private static final String JavaDoc[] VALUES
40         = new String JavaDoc[] {"all", "each", "every", "any", "some", "one",
41                         "majority", "most", "none"};
42
43     /** ALL instance */
44     public static final Quantifier ALL = new Quantifier("all");
45     /** ANY instance */
46     public static final Quantifier ANY = new Quantifier("any");
47     /** ONE instance */
48     public static final Quantifier ONE = new Quantifier("one");
49     /** MAJORITY instance */
50     public static final Quantifier MAJORITY = new Quantifier("majority");
51     /** NONE instance */
52     public static final Quantifier NONE = new Quantifier("none");
53
54     private abstract static class Predicate {
55         abstract boolean eval(int t, int f);
56     }
57
58     private static final Predicate ALL_PRED = new Predicate() {
59         boolean eval(int t, int f) { return f == 0; }
60     };
61
62     private static final Predicate ANY_PRED = new Predicate() {
63         boolean eval(int t, int f) { return t > 0; }
64     };
65
66     private static final Predicate ONE_PRED = new Predicate() {
67         boolean eval(int t, int f) { return t == 1; }
68     };
69
70     private static final Predicate MAJORITY_PRED = new Predicate() {
71         boolean eval(int t, int f) { return t > f; }
72     };
73
74     private static final Predicate NONE_PRED = new Predicate() {
75         boolean eval(int t, int f) { return t == 0; }
76     };
77
78     private static final Predicate[] PREDS = new Predicate[VALUES.length];
79
80     static {
81         PREDS[0] = ALL_PRED;
82         PREDS[1] = ALL_PRED;
83         PREDS[2] = ALL_PRED;
84         PREDS[3] = ANY_PRED;
85         PREDS[4] = ANY_PRED;
86         PREDS[5] = ONE_PRED;
87         PREDS[6] = MAJORITY_PRED;
88         PREDS[7] = MAJORITY_PRED;
89         PREDS[8] = NONE_PRED;
90     }
91
92     /**
93      * Default constructor.
94      */

95     public Quantifier() {
96     }
97
98     /**
99      * Construct a new Quantifier with the specified value.
100      * @param value the EnumeratedAttribute value.
101      */

102     public Quantifier(String JavaDoc value) {
103         setValue(value);
104     }
105
106     /**
107      * Return the possible values.
108      * @return String[] of EnumeratedAttribute values.
109      */

110     public String JavaDoc[] getValues() {
111         return VALUES;
112     }
113
114     /**
115      * Evaluate a <code>boolean<code> array.
116      * @param b the <code>boolean[]</code> to evaluate.
117      * @return true if the argument fell within the parameters of this Quantifier.
118      */

119     public boolean evaluate(boolean[] b) {
120         int t = 0;
121         for (int i = 0; i < b.length; i++) {
122             if (b[i]) {
123                 t++;
124             }
125         }
126         return evaluate(t, b.length - t);
127     }
128
129     /**
130      * Evaluate integer <code>true</code> vs. <code>false</code> counts.
131      * @param t the number of <code>true</code> values.
132      * @param f the number of <code>false</code> values.
133      * @return true if the arguments fell within the parameters of this Quantifier.
134      */

135     public boolean evaluate(int t, int f) {
136         int index = getIndex();
137         if (index == -1) {
138             throw new BuildException("Quantifier value not set.");
139         }
140         return PREDS[index].eval(t, f);
141     }
142
143 }
144
145
Popular Tags