1 18 package org.apache.tools.ant.types; 19 20 import org.apache.tools.ant.BuildException; 21 22 38 public class Quantifier extends EnumeratedAttribute { 39 private static final String [] VALUES 40 = new String [] {"all", "each", "every", "any", "some", "one", 41 "majority", "most", "none"}; 42 43 44 public static final Quantifier ALL = new Quantifier("all"); 45 46 public static final Quantifier ANY = new Quantifier("any"); 47 48 public static final Quantifier ONE = new Quantifier("one"); 49 50 public static final Quantifier MAJORITY = new Quantifier("majority"); 51 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 95 public Quantifier() { 96 } 97 98 102 public Quantifier(String value) { 103 setValue(value); 104 } 105 106 110 public String [] getValues() { 111 return VALUES; 112 } 113 114 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 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 |