KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > getahead > dwrdemo > people > People


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.getahead.dwrdemo.people;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Random JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.directwebremoting.util.Logger;
24
25 /**
26  * A container for a set of people
27  * @author Joe Walker [joe at getahead dot ltd dot uk]
28  */

29 public class People
30 {
31     /**
32      * Pre-populate with random people
33      */

34     public People()
35     {
36         log.debug("Generating a new set of random people");
37         for (int i = 0; i < 5; i++)
38         {
39             people.add(getRandomPerson());
40         }
41     }
42
43     /**
44      * Accessor for the current list of people
45      * @return the current list of people
46      */

47     public Set JavaDoc getAllPeople()
48     {
49         return people;
50     }
51
52     /**
53      * Insert a person into the set of people
54      * @param person The person to add or update
55      */

56     public void setPerson(Person person)
57     {
58         log.debug("Adding person: " + person);
59         if (person.getId() == -1)
60         {
61             person.setId(getNextId());
62         }
63
64         people.remove(person);
65         people.add(person);
66     }
67
68     /**
69      * Delete a person from the set of people
70      * @param person The person to delete
71      */

72     public void deletePerson(Person person)
73     {
74         log.debug("Removing person: " + person);
75         people.remove(person);
76         debug();
77     }
78
79     /**
80      * the current list of people
81      */

82     private Set JavaDoc people = new HashSet JavaDoc();
83
84     /**
85      * Create a random person
86      * @return a random person
87      */

88     private Person getRandomPerson()
89     {
90         Person person = new Person();
91         person.setId(getNextId());
92
93         String JavaDoc firstname = FIRSTNAMES[random.nextInt(FIRSTNAMES.length)];
94         String JavaDoc surname = SURNAMES[random.nextInt(SURNAMES.length)];
95         person.setName(firstname + " " + surname);
96
97         String JavaDoc housenum = (random.nextInt(99) + 1) + " ";
98         String JavaDoc road1 = ROADS1[random.nextInt(ROADS1.length)];
99         String JavaDoc road2 = ROADS2[random.nextInt(ROADS2.length)];
100         String JavaDoc town = TOWNS[random.nextInt(TOWNS.length)];
101         String JavaDoc address = housenum + road1 + " " + road2 + ", " + town;
102         person.setAddress(address);
103
104         float salary = Math.round(10 + 90 * random.nextFloat()) * 1000;
105         person.setSalary(salary);
106
107         return person;
108     }
109
110     /**
111      * List the current people so we know what is going on
112      */

113     protected void debug()
114     {
115         for (Iterator JavaDoc it = people.iterator(); it.hasNext();)
116         {
117             Person person = (Person) it.next();
118             log.debug(person.toString());
119         }
120     }
121
122     /**
123      * Get the next unique ID in a thread safe way
124      * @return a unique id
125      */

126     private static synchronized int getNextId()
127     {
128         return nextId++;
129     }
130
131     /**
132      * The next ID, to get around serialization issues
133      */

134     private static int nextId = 1;
135
136     private Random JavaDoc random = new Random JavaDoc();
137
138     private static final String JavaDoc[] FIRSTNAMES =
139     {
140         "Fred", "Jim", "Shiela", "Jack", "Betty", "Jacob", "Martha", "Kelly",
141         "Luke", "Matt", "Gemma", "Joe", "Ben", "Jessie", "Leanne", "Becky",
142     };
143
144     private static final String JavaDoc[] SURNAMES =
145     {
146         "Sutcliffe", "MacDonald", "Duckworth", "Smith", "Wisner", "Iversen",
147         "Nield", "Turton", "Trelfer", "Wilson", "Johnson", "Cowan", "Daniels",
148     };
149
150     private static final String JavaDoc[] ROADS1 =
151     {
152         "Green", "Red", "Yellow", "Brown", "Blue", "Black", "White",
153     };
154
155     private static final String JavaDoc[] ROADS2 =
156     {
157         "Close", "Drive", "Street", "Avenue", "Crescent", "Road", "Place",
158     };
159
160     private static final String JavaDoc[] TOWNS =
161     {
162         "Birmingham", "Kettering", "Paris", "San Francisco", "New York",
163         "San Mateo", "Barcelona",
164     };
165
166     /**
167      * The log stream
168      */

169     private static final Logger log = Logger.getLogger(People.class);
170 }
171
Popular Tags