| 1 28 29 package com.idaremedia.antx.parameters; 30 31 import com.idaremedia.antx.helpers.Strings; 32 import com.idaremedia.antx.helpers.Tk; 33 34 55 56 public final class Handling extends EnumSkeleton 57 { 58 59 public static final int ACCEPT_INDEX = 0; 60 61 public static final int IGNORE_INDEX = ACCEPT_INDEX+1; 62 63 public static final int REJECT_INDEX = IGNORE_INDEX+1; 64 65 public static final int BALK_INDEX = REJECT_INDEX+1; 66 67 public static final int INHERIT_INDEX = BALK_INDEX+1; 68 69 70 71 public static final Handling ACCEPT= 72 new Handling("accept",ACCEPT_INDEX); 73 74 75 public static final Handling REJECT= 76 new Handling("reject",REJECT_INDEX); 77 78 79 public static final Handling IGNORE= 80 new Handling("ignore",IGNORE_INDEX); 81 82 83 public static final Handling BALK= 84 new Handling("balk",BALK_INDEX); 85 86 87 public static final Handling INHERIT= 88 new Handling("inherit",INHERIT_INDEX); 89 90 91 94 public Handling() 95 { 96 super(); 97 } 98 99 100 105 private Handling(String v, int i) 106 { 107 super(v); 108 } 109 110 111 116 public String [] getValues() 117 { 118 return new String [] {"accept", "ignore", 119 "reject", "balk", "inherit"}; 120 }; 121 122 123 124 130 public static Handling from(int i) 131 { 132 if (i==IGNORE.index) { return IGNORE; } 133 if (i==BALK.index) { return BALK; } 134 if (i==ACCEPT.index) { return ACCEPT; } 135 if (i==INHERIT.index) { return INHERIT; } 136 if (i==REJECT.index) { return REJECT; } 137 return null; 138 } 139 140 141 148 public static Handling from(int i, Handling dflt) 149 { 150 Handling choice= from(i); 151 return (choice==null) ? dflt : choice; 152 } 153 154 155 160 public static Handling from(String s) 161 { 162 if (s!=null && s.length()>1) { 163 s = Tk.lowercaseFrom(s); 164 if (Character.isDigit(s.charAt(0))) { 165 try { return from(Integer.parseInt(s)); } 166 catch(Exception nfx) {} 167 } else { 168 if (IGNORE.value.equals(s)) { return IGNORE; } 169 if (INHERIT.value.equals(s)) { return INHERIT; } 170 if (BALK.value.equals(s)) { return BALK; } 171 if (ACCEPT.value.equals(s)) { return ACCEPT; } 172 if (REJECT.value.equals(s)) { return REJECT; } 173 if (Strings.DEFAULT.equals(s)){ return BALK; } 174 if (Strings.ENCLOSING.equals(s)) { return INHERIT; } 175 } 176 } 177 return null; 178 } 179 180 181 188 public static Handling from(String s, Handling dflt) 189 { 190 Handling choice= from(s); 191 return (choice==null) ? dflt : choice; 192 } 193 194 195 199 209 public static Handling simplify(Handling choice, 210 Handling inheritProxy, 211 Handling nullProxy) 212 { 213 if (choice==null) { 214 return nullProxy; 215 } 216 switch (choice.getIndex()) { 217 case ACCEPT_INDEX: 218 case IGNORE_INDEX: { 219 return ACCEPT; 220 } 221 case REJECT_INDEX: 222 case BALK_INDEX: { 223 return REJECT; 224 } 225 } 226 return inheritProxy; 227 } 228 229 230 238 public static Handling simplify(Handling choice, 239 Handling inheritProxy) 240 { 241 if (choice==null) { 242 throw new IllegalArgumentException (); 243 } 244 return simplify(choice,inheritProxy,null); 245 } 246 247 248 257 public static boolean isYes(Handling choice, 258 Handling inheritProxy) 259 { 260 if (choice==null) { 261 throw new IllegalArgumentException (); 262 } 263 choice = simplify(choice,inheritProxy,null); 264 return (choice==Handling.ACCEPT); 265 } 266 267 268 276 public static Handling simplifyIgnoreOrNot(Handling choice, 277 Handling inheritProxy) 278 { 279 if (choice==null) { 280 throw new IllegalArgumentException (); 281 } 282 switch (choice.getIndex()) { 283 case REJECT_INDEX: 284 case BALK_INDEX: 285 case IGNORE_INDEX: { 286 return IGNORE; 287 } 288 case ACCEPT_INDEX: { 289 return ACCEPT; 290 } 291 } 292 return inheritProxy; 293 } 294 } 295 296 297 | Popular Tags |