KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSX > Util


1 package JSX;
2 import java.io.StringWriter JavaDoc;
3 import java.io.IOException JavaDoc;
4
5
6 public class Util {
7 /** If you really wanted to test this out more extensively, you could
8     * get the initial objects from XML, by reading in from two separate files.
9     * Then you could edit the files by hand, to check to see if it
10     * picked up the differences. Clearly it should work, but (eg) you would
11     * find that non-significant aspects (eg indentation) made no difference.
12     **/

13     public static void main(String JavaDoc[] args) throws IOException JavaDoc { //a test case
14
Object JavaDoc[] leaf = {"left-leaf", "right-leaf"};
15         Object JavaDoc[] branch = {leaf, "middle-leaf", leaf};
16         Object JavaDoc[] root = {null, "another one", branch, leaf};
17         root[0] = root;
18
19         Object JavaDoc[] leaf1 = {"left-leaf", "right-leaf"};
20         Object JavaDoc[] branch1 = {leaf1, "middle-leaf", leaf1};
21         Object JavaDoc[] root1 = {null, "another one", branch1, leaf1};
22         root1[0] = root1;
23
24         new ObjOut().writeObject(root); //just to have a look
25
new ObjOut().writeObject(root1); //just to have a look
26

27         System.out.print("Are they equal? "+equals(root, root1));
28     }
29
30     public static boolean equals(Object JavaDoc obj1, Object JavaDoc obj2) {
31         String JavaDoc a = getString(obj1);
32         String JavaDoc b = getString(obj2);
33         return a.equals(b);
34     }
35     private static String JavaDoc getString(Object JavaDoc obj) {
36         try {
37             StringWriter JavaDoc sout = new StringWriter JavaDoc();
38             ObjOut out = new ObjOut(sout);
39             out.writeObject(obj);
40             return sout.toString();
41         } catch (IOException JavaDoc stringProblem) {
42             stringProblem.printStackTrace(); //could ever happen? Memory insuff?
43
return null;
44         }
45     }
46     private Util() {} //so can't be instantiated
47
}
48
Popular Tags