001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.wicket.examples.repeater;
018:
019: import java.util.Collection;
020:
021: /**
022: * generates random contacts
023: *
024: * @author igor
025: *
026: */
027: public class ContactGenerator {
028: private static ContactGenerator instance = new ContactGenerator();
029: private static long nextId = 1;
030:
031: /**
032: * @return static instance of generator
033: */
034: public static ContactGenerator getInstance() {
035: return instance;
036: }
037:
038: private String[] firstNames = { "Jacob", "Emily", "Michael",
039: "Sarah", "Matthew", "Brianna", "Nicholas", "Samantha",
040: "Christopher", "Hailey", "Abner", "Abby", "Joshua",
041: "Douglas", "Jack", "Keith", "Gerald", "Samuel", "Willie",
042: "Larry", "Jose", "Timothy", "Sandra", "Kathleen", "Pamela",
043: "Virginia", "Debra", "Maria", "Linda" };
044: private String[] lastNames = { "Smiith", "Johnson", "Williams",
045: "Jones", "Brown", "Donahue", "Bailey", "Rose", "Allen",
046: "Black", "Davis", "Clark", "Hall", "Lee", "Baker",
047: "Gonzalez", "Nelson", "Moore", "Wilson", "Graham",
048: "Fisher", "Cruz", "Ortiz", "Gomez", "Murray" };
049:
050: private ContactGenerator() {
051:
052: }
053:
054: /**
055: * @return unique id
056: */
057: public synchronized long generateId() {
058: return nextId++;
059: }
060:
061: /**
062: * generates a new contact
063: *
064: * @return generated contact
065: */
066: public Contact generate() {
067: Contact contact = new Contact(randomString(firstNames),
068: randomString(lastNames));
069: contact.setId(generateId());
070: contact.setHomePhone(generatePhoneNumber());
071: contact.setCellPhone(generatePhoneNumber());
072: return contact;
073: }
074:
075: /**
076: * generats <code>count</code> number contacts and puts them into
077: * <code>collection</code> collection
078: *
079: * @param collection
080: * @param count
081: */
082: public void generate(Collection collection, int count) {
083: for (int i = 0; i < count; i++) {
084: collection.add(generate());
085: }
086: }
087:
088: private String generatePhoneNumber() {
089: return new StringBuffer().append(rint(2, 9)).append(rint(0, 9))
090: .append(rint(0, 9)).append("-555-").append(rint(1, 9))
091: .append(rint(0, 9)).append(rint(0, 9)).append(
092: rint(0, 9)).toString();
093: }
094:
095: private int rint(int min, int max) {
096: return (int) (Math.random() * (max - min) + min);
097: }
098:
099: private String randomString(String[] choices) {
100: return choices[rint(0, choices.length)];
101: }
102:
103: }
|