001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.getahead.dwrdemo.people;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.List;
021: import java.util.regex.Pattern;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.getahead.dwrdemo.util.RandomData;
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: init(5);
037: }
038:
039: /**
040: *
041: */
042: public void init(int count) {
043: for (int i = 0; i < count; i++) {
044: people.add(getRandomPerson());
045: }
046: }
047:
048: /**
049: * Accessor for the current list of people
050: * @return the current list of people
051: */
052: public List<Person> getAllPeople() {
053: return people;
054: }
055:
056: /**
057: * Accessor for a subset of the current list of people
058: * @return the current list of people
059: */
060: public List<Person> getMatchingPeople(String filter) {
061: List<Person> reply = new ArrayList<Person>();
062: Pattern regex = Pattern.compile(filter,
063: Pattern.CASE_INSENSITIVE);
064: for (Person person : people) {
065: if (regex.matcher(person.getName()).find()) {
066: reply.add(person);
067: log.debug("Adding " + person + " to reply");
068: }
069: }
070: return reply;
071: }
072:
073: /**
074: * Insert a person into the set of people
075: * @param person The person to add or update
076: */
077: public void setPerson(Person person) {
078: log.debug("Adding person: " + person);
079: if (person.getId() == -1) {
080: person.setId(getNextId());
081: }
082:
083: people.remove(person);
084: people.add(person);
085: }
086:
087: /**
088: * Delete a person from the set of people
089: * @param person The person to delete
090: */
091: public void deletePerson(Person person) {
092: log.debug("Removing person: " + person);
093: people.remove(person);
094: debug();
095: }
096:
097: /**
098: * Create a random person
099: * @return a random person
100: */
101: public static Person getRandomPerson() {
102: Person person = new Person();
103: person.setId(getNextId());
104: person.setName(RandomData.getFullName());
105: String[] addressAndNumber = RandomData.getAddressAndNumber();
106: person.setAddress(addressAndNumber[0]);
107: person.setPhoneNumber(addressAndNumber[1]);
108: person.setSalary(RandomData.getSalary());
109: return person;
110: }
111:
112: /**
113: * List the current people so we know what is going on
114: */
115: protected void debug() {
116: for (Person person : people) {
117: log.debug(person.toString());
118: }
119: }
120:
121: /**
122: * Get the next unique ID in a thread safe way
123: * @return a unique id
124: */
125: public static synchronized int getNextId() {
126: return nextId++;
127: }
128:
129: /**
130: * The next ID, to get around serialization issues
131: */
132: private static int nextId = 1;
133:
134: /**
135: * the current list of people
136: */
137: private List<Person> people = Collections
138: .synchronizedList(new ArrayList<Person>());
139:
140: /**
141: * The log stream
142: */
143: private static final Log log = LogFactory.getLog(People.class);
144: }
|