home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 5.5 KB | 192 lines |
- package COM.odi.demo.collections;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * This is the source code of the basic example used in the
- * documentation of ObjectStore Java Pro.
- */
-
- // import the persistent collections package which we created
- import pcollections.*;
-
- // import the ObjectStore API
- import COM.odi.*;
-
- import java.util.NoSuchElementException;
- import java.util.Enumeration;
-
- /**
- * CollDemo is a simple test of the persistence-capability of the
- * HashedSet, HashedMap, Dynarray, RBTree classes from the
- * persistent version of the collections library.
- */
-
- class CollDemo {
-
- /* This sets the maximum size of the HashedMap, HashedSet, Dynarray
- * classes. RBTree may be larger.
- */
- static final int maxCollectionSize = 20;
-
- public static void main(String[] argv) {
- if (argv.length != 1) {
- System.out.println("usage: CollDemo <dbname>");
- return;
- }
-
- ObjectStore.initialize(null, null);
- Database db = null;
- try {
- db = Database.open(argv[0], ObjectStore.OPEN_UPDATE);
- } catch (DatabaseNotFoundException ex1) {
- db = Database.create(argv[0], 0777);
- }
-
- Transaction tx = Transaction.begin(ObjectStore.UPDATE);
-
- try {
- /* Check to see if the roots exist yet. If not,
- DatabaseRootNotFound will be thrown */
- db.getRoot("Dynarray");
-
- displayCollections(db);
-
- /* start a new transaction for the update phase */
- tx.commit();
- tx = Transaction.begin(ObjectStore.UPDATE);
-
- maintainCollections(db);
-
- } catch (DatabaseRootNotFoundException dbrex) {
-
- System.out.println("Populating the database...");
-
- /* Create collections objects */
- HashedMap hashedMap = new HashedMap();
- HashedSet hashedSet = new HashedSet();
- Dynarray dynarray = new Dynarray();
- RBTree rbtree = new RBTree();
-
- /* Create roots so that we can find them in later transactions */
- db.createRoot("HashedMap", hashedMap);
- db.createRoot("HashedSet", hashedSet);
- db.createRoot("Dynarray", dynarray);
- db.createRoot("RBTree", rbtree);
-
- /* Populate the collections */
- for (int i = 0; i<maxCollectionSize; i++)
- maintainCollections(db);
- }
-
- tx.commit();
-
- db.close();
- }
-
- /**
- * Add or subtract elements to/from the collections. This method
- * selects a random number to decide whether to add/subtract.
- */
- static void maintainCollections(Database db) {
-
- /* lookup the collection roots */
- HashedMap hashedMap = (HashedMap) db.getRoot("HashedMap");
- HashedSet hashedSet = (HashedSet) db.getRoot("HashedSet");
- Dynarray dynarray = (Dynarray) db.getRoot("Dynarray");
- RBTree rbtree = (RBTree) db.getRoot("RBTree");
-
- /* Select a random integer */
- Integer integer =
- new Integer((int) (Math.random() * maxCollectionSize));
-
- try {
- String string = (String) hashedMap.at(integer);
-
- /* Remove the integer from the collections */
- System.out.println ("removing " + integer);
-
- hashedMap.removeAt(integer);
- hashedSet.exclude(integer);
- dynarray.removeAt(dynarray.firstIndexOf(string));
-
- for (int j=0; j<string.length(); j++)
- rbtree.removeOneOf(new Character(string.charAt(j)));
-
- } catch (NoSuchElementException e) {
- /* add the integer to the collections */
- String string = integer.toString();
-
- System.out.println ("adding " + integer);
-
- /* HashedMap will contain a mapping of Integer to String */
- hashedMap.putAt(integer, string);
-
- /* HashedSet will contain Integers */
- hashedSet.include(integer);
-
- /* Dynarray will include the string */
- dynarray.insertAt(0, string);
-
- for (int j=0; j<string.length(); j++)
- rbtree.add(new Character(string.charAt(j)));
- }
- }
-
- /**
- * Display the contents of the current collections.
- */
- static void displayCollections(Database db) {
-
- /* lookup the collection roots */
- HashedMap hashedMap = (HashedMap) db.getRoot("HashedMap");
- HashedSet hashedSet = (HashedSet) db.getRoot("HashedSet");
- Dynarray dynarray = (Dynarray) db.getRoot("Dynarray");
- RBTree rbtree = (RBTree) db.getRoot("RBTree");
-
- /* Display the HashedSet */
-
- System.out.println("HashedSet contains " +
- hashedSet.size() + " elements:");
- for (Enumeration hse = hashedSet.elements(); hse.hasMoreElements();) {
- Integer integer = (Integer) hse.nextElement();
- System.out.print(" " + integer);
- }
- System.out.println();
-
- /* Display the HashedMap */
-
- System.out.println("HashedMap contains " +
- hashedMap.size() + " elements:");
-
- boolean first = true;
- for (Enumeration hse = hashedSet.elements(); hse.hasMoreElements();) {
- Integer integer = (Integer) hse.nextElement();
- String string = (String) hashedMap.at(integer);
- System.out.print(" " + integer + "->\"" + string + "\"");
- }
- System.out.println();
-
- /* Display the Dynarray */
-
- System.out.println("Dynarray contains " +
- dynarray.size() + " elements:");
- for (Enumeration dae = dynarray.elements(); dae.hasMoreElements();) {
- String string = (String) dae.nextElement();
- System.out.print(" \"" + string + "\"");
- }
- System.out.println();
-
- /* Display the RBTree */
-
- System.out.println("RBTree contains " +
- rbtree.size() + " elements:");
- for (Enumeration rbe = rbtree.elements(); rbe.hasMoreElements();) {
- Character character = (Character) rbe.nextElement();
- System.out.print(" \'" + character + "\'");
- }
- System.out.println();
- }
- }
-
-