home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / CollDemo.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  5.5 KB  |  192 lines

  1. package COM.odi.demo.collections;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * This is the source code of the basic example used in the
  7.  * documentation of ObjectStore Java Pro.
  8.  */
  9.  
  10. // import the persistent collections package which we created
  11. import pcollections.*;
  12.  
  13. // import the ObjectStore API
  14. import COM.odi.*;
  15.  
  16. import java.util.NoSuchElementException;
  17. import java.util.Enumeration;
  18.  
  19. /**
  20.  * CollDemo is a simple test of the persistence-capability of the
  21.  * HashedSet, HashedMap, Dynarray, RBTree classes from the
  22.  * persistent version of the collections library.
  23.  */
  24.  
  25. class CollDemo {
  26.  
  27.   /* This sets the maximum size of the HashedMap, HashedSet, Dynarray
  28.    * classes.  RBTree may be larger.
  29.    */
  30.   static final int maxCollectionSize = 20;
  31.  
  32.   public static void main(String[] argv) {
  33.     if (argv.length != 1) {
  34.       System.out.println("usage: CollDemo <dbname>");
  35.       return;
  36.     }
  37.  
  38.     ObjectStore.initialize(null, null);
  39.     Database db = null;
  40.     try {
  41.       db = Database.open(argv[0], ObjectStore.OPEN_UPDATE);
  42.     } catch (DatabaseNotFoundException ex1) {
  43.       db = Database.create(argv[0], 0777);
  44.     }
  45.  
  46.     Transaction tx = Transaction.begin(ObjectStore.UPDATE);
  47.  
  48.     try {
  49.       /* Check to see if the roots exist yet.  If not, 
  50.      DatabaseRootNotFound will be thrown */
  51.       db.getRoot("Dynarray");
  52.  
  53.       displayCollections(db);
  54.  
  55.       /* start a new transaction for the update phase */
  56.       tx.commit();
  57.       tx = Transaction.begin(ObjectStore.UPDATE);
  58.  
  59.       maintainCollections(db);
  60.  
  61.     } catch (DatabaseRootNotFoundException dbrex) {
  62.  
  63.       System.out.println("Populating the database...");
  64.  
  65.       /* Create collections objects */
  66.       HashedMap hashedMap = new HashedMap();
  67.       HashedSet hashedSet = new HashedSet();
  68.       Dynarray dynarray = new Dynarray();
  69.       RBTree rbtree = new RBTree();
  70.  
  71.       /* Create roots so that we can find them in later transactions */
  72.       db.createRoot("HashedMap", hashedMap);
  73.       db.createRoot("HashedSet", hashedSet);
  74.       db.createRoot("Dynarray", dynarray);
  75.       db.createRoot("RBTree", rbtree);
  76.  
  77.       /* Populate the collections */
  78.       for (int i = 0; i<maxCollectionSize; i++)
  79.     maintainCollections(db);
  80.     }
  81.  
  82.     tx.commit();
  83.  
  84.     db.close();
  85.   }
  86.   
  87.   /**
  88.    * Add or subtract elements to/from the collections.  This method
  89.    * selects a random number to decide whether to add/subtract.
  90.    */
  91.   static void maintainCollections(Database db) {
  92.  
  93.     /* lookup the collection roots */
  94.     HashedMap hashedMap = (HashedMap) db.getRoot("HashedMap");
  95.     HashedSet hashedSet = (HashedSet) db.getRoot("HashedSet");
  96.     Dynarray dynarray = (Dynarray) db.getRoot("Dynarray");
  97.     RBTree rbtree = (RBTree) db.getRoot("RBTree");
  98.     
  99.     /* Select a random integer */
  100.     Integer integer = 
  101.       new Integer((int) (Math.random() * maxCollectionSize));
  102.  
  103.     try {
  104.       String string = (String) hashedMap.at(integer);
  105.  
  106.       /* Remove the integer from the collections */
  107.       System.out.println ("removing " + integer);
  108.  
  109.       hashedMap.removeAt(integer);
  110.       hashedSet.exclude(integer);
  111.       dynarray.removeAt(dynarray.firstIndexOf(string));
  112.  
  113.       for (int j=0; j<string.length(); j++) 
  114.        rbtree.removeOneOf(new Character(string.charAt(j)));
  115.  
  116.     } catch (NoSuchElementException e) {
  117.       /* add the integer to the collections */
  118.       String string  = integer.toString();
  119.  
  120.       System.out.println ("adding " + integer);
  121.  
  122.       /* HashedMap will contain a mapping of Integer to String */
  123.       hashedMap.putAt(integer, string);
  124.  
  125.       /* HashedSet will contain Integers */
  126.       hashedSet.include(integer);
  127.  
  128.       /* Dynarray will include the string */
  129.       dynarray.insertAt(0, string);
  130.  
  131.       for (int j=0; j<string.length(); j++) 
  132.     rbtree.add(new Character(string.charAt(j)));
  133.     }
  134.   }
  135.  
  136.   /**
  137.    * Display the contents of the current collections.
  138.    */
  139.   static void displayCollections(Database db) {
  140.  
  141.     /* lookup the collection roots */
  142.     HashedMap hashedMap = (HashedMap) db.getRoot("HashedMap");
  143.     HashedSet hashedSet = (HashedSet) db.getRoot("HashedSet");
  144.     Dynarray dynarray = (Dynarray) db.getRoot("Dynarray");
  145.     RBTree rbtree = (RBTree) db.getRoot("RBTree");
  146.     
  147.     /* Display the HashedSet */
  148.  
  149.     System.out.println("HashedSet contains " +
  150.                hashedSet.size() + " elements:");
  151.     for (Enumeration hse = hashedSet.elements(); hse.hasMoreElements();) {
  152.       Integer integer = (Integer) hse.nextElement();
  153.       System.out.print("  " + integer);
  154.     }
  155.     System.out.println();
  156.  
  157.     /* Display the HashedMap */
  158.  
  159.     System.out.println("HashedMap contains " +
  160.                hashedMap.size() + " elements:");
  161.  
  162.     boolean first = true;
  163.     for (Enumeration hse = hashedSet.elements(); hse.hasMoreElements();) {
  164.       Integer integer = (Integer) hse.nextElement();
  165.       String string = (String) hashedMap.at(integer);
  166.       System.out.print("  " + integer + "->\"" + string + "\"");
  167.     }
  168.     System.out.println();
  169.  
  170.     /* Display the Dynarray */
  171.  
  172.     System.out.println("Dynarray contains " +
  173.                dynarray.size() + " elements:");
  174.     for (Enumeration dae = dynarray.elements(); dae.hasMoreElements();) {
  175.       String string = (String) dae.nextElement();
  176.       System.out.print("  \"" + string + "\"");
  177.     }
  178.     System.out.println();
  179.  
  180.     /* Display the RBTree */
  181.  
  182.     System.out.println("RBTree contains " +
  183.                rbtree.size() + " elements:");
  184.     for (Enumeration rbe = rbtree.elements(); rbe.hasMoreElements();) {
  185.       Character character = (Character) rbe.nextElement();
  186.       System.out.print("  \'" + character + "\'");
  187.     }
  188.     System.out.println();
  189.   }
  190. }
  191.  
  192.