Source Code Cross Referenced for PhoneBook.java in  » Database-JDBC-Connection-Pool » Space4J » org » space4j » demos » phonebook » 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 » Database JDBC Connection Pool » Space4J » org.space4j.demos.phonebook 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Space4J(TM) - Object Persistence in RAM
002:        // Copyright (C) 2003 Sergio Oliveira Junior
003:        // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
004:
005:        package org.space4j.demos.phonebook;
006:
007:        import org.space4j.implementation.*;
008:        import org.space4j.commands.*;
009:        import org.space4j.*;
010:
011:        import java.util.*;
012:        import java.io.*;
013:        import java.net.*;
014:
015:        /** 
016:         * This is a simple application demonstrating how Space4J can be easily used as a data storage system.<br>
017:         * Is is as simple as it can be.
018:         * java org.space4j.demos.phonebook.PhoneBook
019:         */
020:        public class PhoneBook {
021:
022:            protected Space4J space4j = null;
023:            protected Space space = null;
024:
025:            public PhoneBook() throws LoggerException, CommandException,
026:                    UnknownHostException, IOException, ClassNotFoundException {
027:                // Initializes the system, passing the dir where the logs and snapshots are stored.
028:                // NOTE: The system will load everything in memory here. Snapshot and last commands.
029:                space4j = new SimpleSpace4J("PhoneBook");
030:
031:                // Start the system...
032:                space4j.start();
033:
034:                // Grab the space where all data resides...
035:                space = space4j.getSpace();
036:
037:                // If this is the first time, create our main hashmap...
038:                if (space.getObject("phonenumbers") == null) {
039:                    space4j.executeCommand(new MapCreateCmd("phonenumbers",
040:                            new HashMap()));
041:                }
042:            }
043:
044:            public void executeSnapshot() throws LoggerException {
045:                space4j.executeSnapshot();
046:            }
047:
048:            public void addNumber(String user, String number)
049:                    throws CommandException, LoggerException {
050:                space4j.executeCommand(new MapPutCmd("phonenumbers", user,
051:                        number));
052:            }
053:
054:            public String getNumber(String user) {
055:                Map phonenumbers = (Map) space.getObject("phonenumbers");
056:                if (phonenumbers != null) { // sanity
057:                    return (String) phonenumbers.get(user);
058:                }
059:                return null;
060:            }
061:
062:            public boolean delNumber(String user) throws CommandException,
063:                    LoggerException {
064:                int x = space4j.executeCommand(new MapRemoveCmd("phonenumbers",
065:                        user));
066:                if (x > 0)
067:                    return true;
068:                return false;
069:            }
070:
071:            public ArrayList getNames() {
072:                Iterator iter = space.getSafeKeyIterator("phonenumbers");
073:                if (iter == null)
074:                    return null;
075:                ArrayList list = new ArrayList();
076:                while (iter.hasNext()) {
077:                    String name = (String) iter.next();
078:                    list.add(name);
079:                }
080:                return list;
081:            }
082:
083:            // This has nothing to do with Space4J.
084:            // Just a simple logic for a minimal PhoneBook application.
085:            // ToDo: Make it a Swing application. (Anyone?)
086:            public static void main(String[] args) throws Exception {
087:                PhoneBook book = new PhoneBook();
088:                BufferedReader input = new BufferedReader(
089:                        new InputStreamReader(System.in));
090:                System.out
091:                        .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
092:                while (true) {
093:                    String cmd = input.readLine();
094:                    System.out.println();
095:                    if (cmd.equalsIgnoreCase("l")) {
096:                        ArrayList list = book.getNames();
097:                        if (list == null || list.size() == 0)
098:                            System.out.println("No entries in phone book!\n");
099:                        else {
100:                            Iterator iter = list.iterator();
101:                            while (iter.hasNext()) {
102:                                String key = (String) iter.next();
103:                                String value = book.getNumber(key);
104:                                System.out.println(key + ": " + value);
105:                            }
106:                            System.out
107:                                    .println("\nTotal: " + list.size() + "\n");
108:                        }
109:                    } else if (cmd.equalsIgnoreCase("a")) {
110:                        System.out.print("Name: ");
111:                        String name = input.readLine();
112:                        System.out.print("Tel: ");
113:                        String tel = input.readLine();
114:                        if (!name.trim().equals("") && !tel.trim().equals("")) {
115:                            book.addNumber(name, tel);
116:                            System.out.println(name + ": " + tel + " added!\n");
117:                        }
118:                    } else if (cmd.equalsIgnoreCase("f")) {
119:                        System.out.print("Name: ");
120:                        String name = input.readLine();
121:                        String tel = book.getNumber(name);
122:                        if (tel != null) {
123:                            System.out.println("Number: " + tel + "\n");
124:                        } else {
125:                            System.out.println("Nothing found!\n");
126:                        }
127:                    } else if (cmd.equalsIgnoreCase("r")) {
128:                        System.out.print("Name: ");
129:                        String name = input.readLine();
130:                        if (book.delNumber(name)) {
131:                            System.out.println("Item removed!\n");
132:                        } else {
133:                            System.out.println("Not found!\n");
134:                        }
135:                    } else if (cmd.equalsIgnoreCase("s")) {
136:                        book.executeSnapshot();
137:                        System.out.println("Snapshot taken!\n");
138:                    } else if (cmd.equalsIgnoreCase("q")) {
139:                        break;
140:                    }
141:                    System.out
142:                            .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
143:                }
144:            }
145:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.