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.ArrayList;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: /**
027: * simple database for contacts
028: *
029: * @author igor
030: *
031: */
032: public class ContactsDatabase {
033: private Map map = Collections.synchronizedMap(new HashMap());
034: private List fnameIdx = Collections
035: .synchronizedList(new ArrayList());
036: private List lnameIdx = Collections
037: .synchronizedList(new ArrayList());
038: private List fnameDescIdx = Collections
039: .synchronizedList(new ArrayList());
040: private List lnameDescIdx = Collections
041: .synchronizedList(new ArrayList());
042:
043: /**
044: * Constructor
045: *
046: * @param count
047: * number of contacts to generate at startup
048: */
049: public ContactsDatabase(int count) {
050: for (int i = 0; i < count; i++) {
051: add(ContactGenerator.getInstance().generate());
052: }
053: updateIndecies();
054: }
055:
056: /**
057: * find contact by id
058: *
059: * @param id
060: * @return contact
061: */
062: public Contact get(long id) {
063: Contact c = (Contact) map.get(new Long(id));
064: if (c == null) {
065: throw new RuntimeException("contact with id [" + id
066: + "] not found in the database");
067: }
068: return c;
069: }
070:
071: protected void add(final Contact contact) {
072: map.put(new Long(contact.getId()), contact);
073: fnameIdx.add(contact);
074: lnameIdx.add(contact);
075: fnameDescIdx.add(contact);
076: lnameDescIdx.add(contact);
077: }
078:
079: /**
080: * select contacts and apply sort
081: *
082: * @param first
083: * @param count
084: * @param sortProperty
085: * @param sortAsc
086: * @return list of contacts
087: */
088: public List find(int first, int count, String sortProperty,
089: boolean sortAsc) {
090: List sublist = getIndex(sortProperty, sortAsc).subList(first,
091: first + count);
092: return sublist;
093: }
094:
095: protected List getIndex(String prop, boolean asc) {
096: if (prop == null) {
097: return fnameIdx;
098: }
099: if (prop.equals("firstName")) {
100: return (asc) ? fnameIdx : fnameDescIdx;
101: } else if (prop.equals("lastName")) {
102: return (asc) ? lnameIdx : lnameDescIdx;
103: }
104: throw new RuntimeException("uknown sort option [" + prop
105: + "]. valid options: [firstName] , [lastName]");
106: }
107:
108: /**
109: * @return number of contacts in the database
110: */
111: public int getCount() {
112: return fnameIdx.size();
113: }
114:
115: /**
116: * add contact to the database
117: *
118: * @param contact
119: */
120: public void save(final Contact contact) {
121: if (contact.getId() == 0) {
122: contact.setId(ContactGenerator.getInstance().generateId());
123: add(contact);
124: updateIndecies();
125: } else {
126: throw new IllegalArgumentException("contact ["
127: + contact.getFirstName()
128: + "] is already persistent");
129: }
130: }
131:
132: /**
133: * delete contact from the database
134: *
135: * @param contact
136: */
137: public void delete(final Contact contact) {
138: Contact c = (Contact) map.remove(new Long(contact.getId()));
139:
140: fnameIdx.remove(contact);
141: lnameIdx.remove(contact);
142: fnameDescIdx.remove(contact);
143: lnameDescIdx.remove(contact);
144:
145: contact.setId(0);
146: }
147:
148: private void updateIndecies() {
149: Collections.sort(fnameIdx, new Comparator() {
150: public int compare(Object arg0, Object arg1) {
151: return ((Contact) arg0).getFirstName().compareTo(
152: ((Contact) arg1).getFirstName());
153: }
154: });
155:
156: Collections.sort(lnameIdx, new Comparator() {
157: public int compare(Object arg0, Object arg1) {
158: return ((Contact) arg0).getLastName().compareTo(
159: ((Contact) arg1).getLastName());
160: }
161: });
162:
163: Collections.sort(fnameDescIdx, new Comparator() {
164: public int compare(Object arg0, Object arg1) {
165: return ((Contact) arg1).getFirstName().compareTo(
166: ((Contact) arg0).getFirstName());
167: }
168: });
169:
170: Collections.sort(lnameDescIdx, new Comparator() {
171: public int compare(Object arg0, Object arg1) {
172: return ((Contact) arg1).getLastName().compareTo(
173: ((Contact) arg0).getLastName());
174: }
175: });
176:
177: }
178:
179: }
|