KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > example > Main


1 package dynaop.example;
2
3 import java.util.Date JavaDoc;
4
5 import dynaop.ProxyFactory;
6 import dynaop.observer.Observer;
7 import dynaop.observer.Subject;
8
9 /**
10  * Example application. Creates and exersizes proxied book and person objects.
11  * Renders sequence diagram.
12  *
13  * @author Bob Lee (crazybob@crazybob.org)
14  */

15 public class Main {
16     
17     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
18         // initialize sequence diagram window.
19
Sequence.setFrame(new com.zanthan.sequence.SequenceFrame());
20         
21         // get domain object factory.
22
Factory factory = Factory.getInstance();
23
24         // create a date and change the time.
25
Date JavaDoc date = factory.createDate();
26         System.out.println("=> Time: " + date.getTime());
27         date.setTime(1000);
28         
29         // create a book and add an observer to it.
30
Book book = factory.createBook();
31         Observer observer = BookTitleObserver.getInstance();
32         ((Subject) book).addObserver(observer);
33         book.setTitle("Bitter EJB");
34         book.setPublicationDate(date);
35         
36         // create a person, set the ID, add a book, and set the e-mail.
37
// note: we cast the person to the helper interface, PersonProxy.
38
PersonProxy bob = (PersonProxy) factory.createPerson();
39         bob.setId(100);
40         bob.getId();
41         bob.getBooks().add(book);
42         bob.setEmail("crazybob@crazybob.org");
43         
44         // create another person and compare it to the first.
45
PersonProxy krista = (PersonProxy) factory.createPerson();
46         krista.setId(200);
47         System.out.println("=> Equals: " + bob.equals(krista));
48
49         // compare book to a person.
50
((Identifiable) book).setId(10);
51         System.out.println("=> Equals: " + book.equals(krista));
52         
53         /*
54         for (int i = 0; i < 10; i++)
55             testPerformance();
56         System.out.println();
57         for (int i = 0; i < 10; i++)
58             testCglibPerformance();
59         */

60     }
61
62     static void testPerformance() {
63         PersonProxy p = (PersonProxy) Factory.getInstance().createPerson();
64         p.setId(50);
65         p.getId();
66         p.getEmail();
67         p.hashCode();
68         
69         long start = System.currentTimeMillis();
70         for (int i = 0; i < 10000; i++) {
71             p.setId(50);
72             p.getId();
73             p.getEmail();
74             p.hashCode();
75         }
76         System.out.println("*** Time: " +
77             (System.currentTimeMillis() - start) + "ms");
78     }
79
80     static void testCglibPerformance() {
81         BookImpl b = (BookImpl)
82             ProxyFactory.getInstance().extend(BookImpl.class);
83         ((Identifiable) b).setId(50);
84         ((Identifiable) b).getId();
85         b.hashCode();
86         
87         long start = System.currentTimeMillis();
88         for (int i = 0; i < 10000; i++) {
89             ((Identifiable) b).setId(50);
90             ((Identifiable) b).getId();
91             b.hashCode();
92         }
93         System.out.println("*** Time: " +
94             (System.currentTimeMillis() - start) + "ms");
95     }
96 }
97
Popular Tags