home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / incrbag.jav < prev    next >
Encoding:
Text File  |  1995-12-14  |  5.7 KB  |  217 lines

  1. /*
  2.   File: IncrBag.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   24Sep95  dl@cs.oswego.edu   Create from collections.java  working file
  11.   13Oct95  dl                 Changed to use accessOnly
  12.  
  13. */
  14.   
  15. package collections;
  16.  
  17. import java.util.Enumeration;
  18. import java.util.NoSuchElementException;
  19.  
  20. /**
  21.  *
  22.  * 
  23.  * Incremental Implementation of Immutable Bag
  24.  * @author Doug Lea
  25.  * @version 0.93
  26.  *
  27.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  28. **/
  29.  
  30. public final class IncrBag extends IncrImpl implements Immutable, Bag {
  31.  
  32.   private int occurrencesArg_;
  33.  
  34. /**
  35.  * Make a new pure bag using the default underlying Bag implementation
  36. **/
  37.   public IncrBag() { this(DefaultImplementations.bag()); }
  38.  
  39. /**
  40.  * Make a pure bag managing the given updatable bag s.
  41.  * Warning: Do not modify s during the the lifetime of the constructed pure bag!
  42. **/
  43.   public IncrBag(UpdatableBag s) { super(s); occurrencesArg_ = 0; }
  44.  
  45.  
  46. /**
  47.  * Make a copy. Uses lazy update.
  48. **/
  49.   protected Object clone() throws CloneNotSupportedException {
  50.     undelta();
  51.     IncrBag s = new IncrBag((UpdatableBag)(updatable_));
  52.     nextVersion_ = s;
  53.     updatable_ = null;
  54.     op_ = NO_EDIT;
  55.     return s;
  56.   }
  57.  
  58.  
  59. /**
  60.  * Implements collections.Bag.addingIfAbsent
  61.  * @see collections.Bag#addingIfAbsent
  62. **/
  63.   public synchronized /* IncrBag */ Bag addingIfAbsent(Object element) 
  64.   throws IllegalElementException {
  65.     undelta(); 
  66.     UpdatableBag u = (UpdatableBag)updatable_;
  67.     boolean has = u.includes(element);
  68.     if (has) 
  69.       return this;
  70.     else {
  71.       u.add(element);
  72.       IncrBag s = new IncrBag(u);
  73.       nextVersion_ = s;
  74.       updatable_ = null;
  75.       firstObjectArg_ = element;
  76.       occurrencesArg_ = 1;
  77.       op_ = REMOVE_EDIT;
  78.       return s;
  79.     }
  80.   }
  81.  
  82. /**
  83.  * Implements collections.Bag.adding
  84.  * @see collections.Bag#adding
  85. **/
  86.   public synchronized /* IncrBag */ Bag adding(Object element) 
  87.   throws IllegalElementException {
  88.     undelta(); 
  89.     UpdatableBag u = (UpdatableBag)updatable_;
  90.     u.add(element);
  91.     IncrBag s = new IncrBag(u);
  92.     nextVersion_ = s;
  93.     updatable_ = null;
  94.     occurrencesArg_ = 1;
  95.     firstObjectArg_ = element;
  96.     op_ = REMOVE_EDIT;
  97.     return s;
  98.   }
  99.  
  100. /**
  101.  * Implements collections.Collection.excluding.
  102.  * @see collections.Collection#excluding
  103. **/
  104.   public synchronized /* IncrBag */ Collection excluding(Object element) {
  105.     undelta(); 
  106.     UpdatableBag u = (UpdatableBag)updatable_;
  107.     int occ = u.occurrencesOf(element);
  108.     if (occ == 0)
  109.       return this;
  110.     else {
  111.       u.exclude(element);
  112.       IncrBag s = new IncrBag(u);
  113.       nextVersion_ = s;
  114.       updatable_ = null;
  115.       firstObjectArg_ = element;
  116.       occurrencesArg_ = occ;
  117.       op_ = ADD_EDIT;
  118.       return s;
  119.     }
  120.   }
  121.  
  122. /**
  123.  * Implements collections.Collection.removingOneOf
  124.  * @see collections.Collection#removingOneOf
  125. **/
  126.   public synchronized /* IncrBag */ Collection removingOneOf(Object element) {
  127.     undelta(); 
  128.     UpdatableBag u = (UpdatableBag)updatable_;
  129.     int occ = u.occurrencesOf(element);
  130.     if (occ == 0)
  131.       return this;
  132.     else {
  133.       u.removeOneOf(element);
  134.       IncrBag s = new IncrBag(u);
  135.       nextVersion_ = s;
  136.       updatable_ = null;
  137.       firstObjectArg_ = element;
  138.       occurrencesArg_ = 1;
  139.       op_ = ADD_EDIT;
  140.       return s;
  141.     }
  142.   }
  143.  
  144. /**
  145.  * Implements collections.Collection.replacingAllOf
  146.  * @see collections.Collection#replacingAllOf
  147. **/
  148.   public synchronized /* IncrBag */ Collection replacingAllOf(Object oldElement,
  149.                                                         Object newElement) 
  150.   throws IllegalElementException {
  151.     undelta(); 
  152.     UpdatableBag u = (UpdatableBag)updatable_;
  153.     int oldocc = u.occurrencesOf(oldElement);
  154.     if (oldocc == 0)
  155.       return this;
  156.     else {
  157.       u.replaceAllOf(oldElement, newElement);
  158.       IncrBag s = new IncrBag(u);
  159.       nextVersion_ = s;
  160.       updatable_ = null;
  161.       firstObjectArg_ = newElement;
  162.       secondObjectArg_ = oldElement;
  163.       op_ = REPLACE_EDIT;
  164.       occurrencesArg_ = oldocc;
  165.       return s;
  166.     }
  167.   }
  168.  
  169. /**
  170.  * Implements collections.Collection.replacingOneOf
  171.  * @see collections.Collection#replacingOneOf
  172. **/
  173.   public synchronized /* IncrBag */ Collection replacingOneOf(Object oldElement,
  174.                                                     Object newElement) 
  175.   throws IllegalElementException {
  176.     undelta(); 
  177.     UpdatableBag u = (UpdatableBag)updatable_;
  178.     boolean has = u.includes(oldElement);
  179.     if (!has)
  180.       return this;
  181.     else {
  182.       u.replaceOneOf(oldElement, newElement);
  183.       IncrBag s = new IncrBag(u);
  184.       nextVersion_ = s;
  185.       updatable_ = null;
  186.       firstObjectArg_ = newElement;
  187.       secondObjectArg_ = oldElement;
  188.       op_ = REPLACE_EDIT;
  189.       occurrencesArg_ = 1;
  190.       return s;
  191.     }
  192.   }
  193.  
  194.  
  195. /**
  196.  * Perform updates within an edit chain
  197. **/
  198.   protected synchronized UpdatableCollection doEdit(UpdatableCollection c) { 
  199.     UpdatableBag u = (UpdatableBag)c;
  200.     try { 
  201.       for (int i = 0; i < occurrencesArg_; ++i) {
  202.         if (op_ == ADD_EDIT) 
  203.           u.add(firstObjectArg_);
  204.         else if (op_ == REMOVE_EDIT)
  205.           u.removeOneOf(firstObjectArg_);
  206.         else if (op_ == REPLACE_EDIT)
  207.           u.replaceOneOf(firstObjectArg_, secondObjectArg_);
  208.       }
  209.     }
  210.     catch (IllegalElementException ex) {} // we've screened for all possible
  211.     return u;
  212.   }
  213.  
  214.  
  215. }
  216.  
  217.