1 package JSX; 2 3 import java.io.*; 4 import java.util.*; 5 6 14 15 16 17 class ValidationList { 18 public static void main(String [] arg) throws Exception { 19 ValidationList v = new ValidationList(); 20 v.register(null, 1); 21 v.register(null, 2); 22 v.register(null, 2); 23 v.register(null, -1); 24 v.register(null, 1); 25 v.register(null, -1); 26 v.register(null, 99); 27 v.register(null, 1); 28 v.register(null, -1); 29 30 v.doCallbacks(); 31 } 32 33 private static class Callback implements Comparable { 34 final ObjectInputValidation obj; 35 final int priority; 36 private Callback(ObjectInputValidation obj, int priority) { 37 this.obj = obj; 38 this.priority = priority; 39 } 40 public int compareTo(Object obj) { int cmp = -(this.priority-((Callback)obj).priority); 42 return cmp; 45 } 46 } 50 51 private List list = new LinkedList(); 53 54 void register(ObjectInputValidation obj, int priority) throws InvalidObjectException { 55 if (obj==null) 56 throw new InvalidObjectException("null callback"); 57 list.add(new Callback(obj, priority)); 58 } 59 60 void doCallbacks() throws InvalidObjectException { 61 Collections.sort(list); 62 Iterator it = list.iterator(); while (it.hasNext()) { 65 ((Callback)it.next()).obj.validateObject(); 67 } 68 } 72 73 void clear() { 74 list.clear(); 75 } 76 } 77 | Popular Tags |