001: /*
002: *
003: * Copyright 2005 Joe Walker
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package uk.ltd.getahead.dwrdemo.people;
019:
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Random;
023: import java.util.Set;
024:
025: import org.directwebremoting.util.Logger;
026:
027: /**
028: * A container for a set of people
029: * @author Joe Walker [joe at getahead dot ltd dot uk]
030: */
031: public class People {
032: /**
033: * Pre-populate with random people
034: */
035: public People() {
036: log.debug("Generating a new set of random people");
037: for (int i = 0; i < 5; i++) {
038: people.add(getRandomPerson());
039: }
040: }
041:
042: /**
043: * Accessor for the current list of people
044: * @return the current list of people
045: */
046: public Set getAllPeople() {
047: return people;
048: }
049:
050: /**
051: * Insert a person into the set of people
052: * @param person The person to add or update
053: */
054: public void setPerson(Person person) {
055: log.debug("Adding person: " + person);
056: if (person.getId() == -1) {
057: person.setId(getNextId());
058: }
059:
060: people.remove(person);
061: people.add(person);
062: }
063:
064: /**
065: * Delete a person from the set of people
066: * @param person The person to delete
067: */
068: public void deletePerson(Person person) {
069: log.debug("Removing person: " + person);
070: people.remove(person);
071: debug();
072: }
073:
074: /**
075: * the current list of people
076: */
077: private Set people = new HashSet();
078:
079: /**
080: * Create a random person
081: * @return a random person
082: */
083: private Person getRandomPerson() {
084: Person person = new Person();
085: person.setId(getNextId());
086:
087: String firstname = FIRSTNAMES[random.nextInt(FIRSTNAMES.length)];
088: String surname = SURNAMES[random.nextInt(SURNAMES.length)];
089: person.setName(firstname + " " + surname);
090:
091: String housenum = (random.nextInt(99) + 1) + " ";
092: String road1 = ROADS1[random.nextInt(ROADS1.length)];
093: String road2 = ROADS2[random.nextInt(ROADS2.length)];
094: String town = TOWNS[random.nextInt(TOWNS.length)];
095: String address = housenum + road1 + " " + road2 + ", " + town;
096: person.setAddress(address);
097:
098: float salary = Math.round(10 + 90 * random.nextFloat()) * 1000;
099: person.setSalary(salary);
100:
101: return person;
102: }
103:
104: /**
105: * List the current people so we know what is going on
106: */
107: protected void debug() {
108: for (Iterator it = people.iterator(); it.hasNext();) {
109: Person person = (Person) it.next();
110: log.debug(person.toString());
111: }
112: }
113:
114: /**
115: * Get the next unique ID in a thread safe way
116: * @return a unique id
117: */
118: private static synchronized int getNextId() {
119: return nextId++;
120: }
121:
122: /**
123: * The next ID, to get around serialization issues
124: */
125: private static int nextId = 1;
126:
127: private Random random = new Random();
128:
129: private static final String[] FIRSTNAMES = { "Fred", "Jim",
130: "Shiela", "Jack", "Betty", "Jacob", "Martha", "Kelly",
131: "Luke", "Matt", "Gemma", "Joe", "Ben", "Jessie", "Leanne",
132: "Becky", };
133:
134: private static final String[] SURNAMES = { "Sutcliffe",
135: "MacDonald", "Duckworth", "Smith", "Wisner", "Iversen",
136: "Nield", "Turton", "Trelfer", "Wilson", "Johnson", "Cowan",
137: "Daniels", };
138:
139: private static final String[] ROADS1 = { "Green", "Red", "Yellow",
140: "Brown", "Blue", "Black", "White", };
141:
142: private static final String[] ROADS2 = { "Close", "Drive",
143: "Street", "Avenue", "Crescent", "Road", "Place", };
144:
145: private static final String[] TOWNS = { "Birmingham", "Kettering",
146: "Paris", "San Francisco", "New York", "San Mateo",
147: "Barcelona", };
148:
149: /**
150: * The log stream
151: */
152: private static final Logger log = Logger.getLogger(People.class);
153: }
|