Source Code Cross Referenced for ContactsDatabase.java in  » J2EE » wicket » org » apache » wicket » examples » repeater » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » wicket » org.apache.wicket.examples.repeater 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.