Debian Codegraph @

jdbm1/src/examples/FruitBasket.java

raw
1
2 import jdbm.RecordManager;
3 import jdbm.RecordManagerFactory;
4 import jdbm.helper.FastIterator;
5 import jdbm.htree.HTree;
6
7 import java.io.IOException;
8 import java.util.Properties;
9
10 /**
11 * Sample JDBM application to demonstrate the use of basic JDBM operations.
12 *
13 * @author <a href="mailto:[email protected]">Alex Boisvert</a>
14 */
15 public class FruitBasket
16 {
17 RecordManager recman;
18 HTree hashtable;
19 FastIterator iter;
20 String fruit;
21 String color;
22
23
24 public FruitBasket()
25 throws IOException
26 {
27 // create or open fruits record manager
28 Properties props = new Properties();
29 recman = RecordManagerFactory.createRecordManager( "fruits", props );
30
31 // create or load fruit basket (hashtable of fruits)
32 long recid = recman.getNamedObject( "basket" );
33 if ( recid != 0 ) {
34 System.out.println( "Reloading existing fruit basket..." );
35 hashtable = HTree.load( recman, recid );
36 showBasket();
37 } else {
38 System.out.println( "Creating new fruit basket..." );
39 hashtable = HTree.createInstance( recman );
40 recman.setNamedObject( "basket", hashtable.getRecid() );
41 }
42 }
43
44
45 public void runDemo()
46 throws IOException
47 {
48 // insert keys and values
49 System.out.println();
50 System.out.println( "Adding fruits to the basket..." );
51 hashtable.put( "bananas", "yellow" );
52 hashtable.put( "strawberries", "red" );
53 hashtable.put( "kiwis", "green" );
54
55 showBasket();
56
57
58 // display color of a specific fruit
59 System.out.println();
60 System.out.println( "Get the color of bananas..." );
61 String bananasColor = (String) hashtable.get( "bananas" );
62 System.out.println( "bananas are " + bananasColor );
63
64 recman.commit();
65
66 try {
67 // Thread.sleep( 10 * 1000 );
68 } catch ( Exception except ) {
69 // ignore
70 }
71
72 // remove a specific fruit from hashtable
73 System.out.println();
74 System.out.print( "Removing bananas from the basket..." );
75 hashtable.remove( "bananas" );
76 recman.commit();
77 System.out.println( " done." );
78
79 // iterate over remaining objects
80 System.out.println();
81 System.out.println( "Remaining fruit colors:" );
82 iter = hashtable.keys();
83 fruit = (String) iter.next();
84 while ( fruit != null ) {
85 color = (String) hashtable.get( fruit );
86 System.out.println( fruit + " are " + color );
87 fruit = (String) iter.next();
88 }
89
90 // cleanup
91 recman.close();
92 }
93
94
95 public void showBasket()
96 throws IOException
97 {
98 // Display content of fruit basket
99 System.out.println();
100 System.out.print( "Fruit basket contains: " );
101 iter = hashtable.keys();
102 fruit = (String) iter.next();
103 while ( fruit != null ) {
104 System.out.print( " " + fruit );
105 fruit = (String) iter.next();
106 }
107 System.out.println();
108 }
109
110
111 public static void main( String[] args )
112 {
113 try {
114 FruitBasket basket = new FruitBasket();
115 basket.runDemo();
116 } catch ( IOException ioe ) {
117 ioe.printStackTrace();
118 }
119 }
120
121 }