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: }
|