KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > test > Person


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.pojo.test;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15
16 /**
17  * Test class for PojoCache.
18  * Person is a POJO that will be instrumented with CacheFieldInterceptor
19  *
20  * @version $Revision: 1.4 $
21  * <p>Below is the annotation that signifies this class is "prepared" under JBossAop. This is used in
22  * conjunction with a special jboss-aop.xml (supplied by PojoCache). In addition, this is JDK1.4 style,
23  * so a annoc Ant build target is needed to pre-compile it.</p>
24  * <p>To use this approach, just apply this line to your pojo and run annoc (and possibly aopc).</p>
25  */

26 // We are using JDK1.5 annotation.
27
@org.jboss.cache.pojo.annotation.Replicable
28 public class Person
29 {
30    String JavaDoc name = null;
31    int age = 0;
32    Map JavaDoc<String JavaDoc, String JavaDoc> hobbies = null;
33    Address address = null;
34    Set JavaDoc<String JavaDoc> skills;
35    List JavaDoc<String JavaDoc> languages;
36    // Test for transient field non-replication
37
transient String JavaDoc currentStatus = "Active";
38    // Test swapping out the Collection ref with proxy one
39
// medication will be different when age limit is exceeded.
40
List JavaDoc<String JavaDoc> medication = null;
41    static final int AGE1 = 50;
42    static final int AGE2 = 60;
43
44    public Person()
45    {
46
47    }
48
49    public String JavaDoc getName()
50    {
51       return name;
52    }
53
54    public void setName(String JavaDoc name)
55    {
56       this.name = name;
57    }
58
59    public void setCurrentStatus(String JavaDoc status)
60    {
61       currentStatus = status;
62    }
63
64    public String JavaDoc getCurrentStatus()
65    {
66       return currentStatus;
67    }
68
69    public void setName(Object JavaDoc obj)
70    {
71       this.name = (String JavaDoc) obj;
72    }
73
74    public int getAge()
75    {
76       return age;
77    }
78
79    public void setAge(int age)
80    {
81
82       this.age = age;
83
84       // This will swap out the reference dynamically
85
if (age < AGE1)
86       {
87          if (medication != null)
88          {
89             medication.clear();
90             medication = null;
91          }
92       } else
93       {
94          if (age >= AGE1)
95          {
96             addMedication("Lipitor");
97          }
98
99          if (age >= AGE2)
100          {
101             addMedication("Vioxx");
102          }
103       }
104
105
106    }
107
108    void addMedication(String JavaDoc name)
109    {
110       if (medication == null)
111          medication = new ArrayList JavaDoc();
112       if (!medication.contains(name))
113          medication.add(name);
114    }
115
116    public Map JavaDoc<String JavaDoc, String JavaDoc> getHobbies()
117    {
118       return hobbies;
119    }
120
121    public void setHobbies(Map JavaDoc hobbies)
122    {
123       this.hobbies = hobbies;
124    }
125
126    public Address getAddress()
127    {
128       return address;
129    }
130
131    public void setAddress(Address address)
132    {
133       this.address = address;
134    }
135
136    public Set JavaDoc<String JavaDoc> getSkills()
137    {
138       return skills;
139    }
140
141    public void setSkills(Set JavaDoc skills)
142    {
143       this.skills = skills;
144    }
145
146    public List JavaDoc<String JavaDoc> getMedication()
147    {
148       return medication;
149    }
150
151    public void setMedication(List JavaDoc medication)
152    {
153       this.medication = medication;
154    }
155
156    public List JavaDoc<String JavaDoc> getLanguages()
157    {
158       return languages;
159    }
160
161    public void setLanguages(List JavaDoc languages)
162    {
163       this.languages = languages;
164    }
165
166    public String JavaDoc toString()
167    {
168       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
169       sb.append("name=").append(getName()).append(", age=").append(getAge()).append(", hobbies=")
170               .append(print(getHobbies())).append(", address=").append(getAddress()).append(", skills=")
171               .append(skills).append(", languages=").append(languages).toString();
172       if (medication != null)
173          sb.append(", medication=" + medication);
174       return sb.toString();
175    }
176
177    public String JavaDoc print(Map JavaDoc m)
178    {
179       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
180       Map.Entry JavaDoc entry;
181       if (m != null)
182       {
183          for (Iterator JavaDoc it = m.entrySet().iterator(); it.hasNext();)
184          {
185             entry = (Map.Entry JavaDoc) it.next();
186             sb.append(entry.getKey()).append(": ").append(entry.getValue());
187             sb.append("\n");
188          }
189       }
190       return sb.toString();
191    }
192 }
193
Popular Tags