KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSX > ValidationList


1 package JSX;
2
3 import java.io.*;
4 import java.util.*;
5
6 /** Correct order is higher priorities first? Large->small, "decreasing order"
7     * Note: Can't use "SortedSet", because can have more than one of same "value".
8     * Question: this works to spec, but how to test it in situ?
9     *
10     * The messy commenting out below will remain, until at least one release
11     * has passed - and same for the remanents of native approach in ObjIn.
12     * But this new approach is definitely the right way to go!
13     **/

14
15
16 /** Not public: only used from ObjIn **/
17 class ValidationList {
18     public static void main(String JavaDoc[] arg) throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc obj) { // >0 if this>obj; -ve for decreasing
41
int cmp = -(this.priority-((Callback)obj).priority);
42         // if (cmp==0) return 1; // never == (ugly potentially dangerous hack)
43
// else
44
return cmp;
45         }
46 // public boolean equals(Object obj) { // >0 if this>obj; want reverse, so -ve
47
// return this.priority==((Callback)obj).priority; // javanut says needed
48
// }
49
}
50
51     //private SortedSet list = new TreeSet();
52
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 // try {
63
Iterator it = list.iterator(); // style from JPL
64
while (it.hasNext()) {
65                 //System.err.println(((Callback)it.next()).priority);
66
((Callback)it.next()).obj.validateObject();
67             }
68 // } finally {
69
// clear(); // free the objects -> actually, we can rely on readObject
70
// }
71
}
72
73     void clear() {
74         list.clear();
75     }
76 }
77
Popular Tags