root/trunk/ProjectFortress/src/com/sun/fortress/interpreter/evaluator/transactions/ReadSet.java @ 2738

Revision 2738, 3.3 KB (checked in by chf, 15 months ago)

Some cleanups and refactorings. More to come

Line 
1/*******************************************************************************
2    Copyright 2008 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.interpreter.evaluator.transactions;
19import com.sun.fortress.interpreter.evaluator.tasks.FortressTaskRunner;
20import java.util.AbstractSet;
21import java.util.concurrent.CopyOnWriteArrayList;
22
23public class ReadSet extends AbstractSet<Transaction> {
24  private CopyOnWriteArrayList<Transaction> elements;
25  private Boolean sealed;
26
27  public ReadSet() {
28      sealed = false;
29      elements = new CopyOnWriteArrayList<Transaction>();
30  }
31
32
33  public ReadSet(ReadSet r) {
34      sealed = false;
35      elements = new CopyOnWriteArrayList<Transaction>();
36      // There is a race here.  If I'm trying to copy from r and r is modified
37      // out from underneath me, I get a NoSuchElementException, so I'm writing
38      // my own add function.
39      if (r != null)
40          myAdd(r);
41  }
42
43  private void myAdd(ReadSet r) {
44      for (Transaction t : elements)
45          if (t.isActive())
46              elements.add(t);
47  }
48
49  public String toString() {
50          return "sealed = " + sealed + " elements = " + elements;
51  }
52     
53
54  /**
55   * Initialize one object from another.
56   * @param aSet Initialize from this other object.
57   */
58  public void copyFrom(ReadSet aSet) {
59      elements.addAll(aSet.elements);
60  }
61
62  public void seal() {
63      sealed = true;
64  }
65
66  /**
67   * Add a new transaction to the set.
68   * @param t Transaction to add.
69   * @return Whether this transaction was already present.
70   */
71  public boolean add(Transaction t) {
72      cleanup();
73      if (sealed) {
74                  FortressTaskRunner.debugPrintln("add of " + t + " to readset " + toString() + " failed because readset was sealed");
75          Thread.dumpStack();
76                  return false;
77      } else {
78                  elements.addIfAbsent(t);
79                  return true;
80      }
81  }
82
83  public void cleanup() {
84      for (Transaction t : elements)
85          if (!t.isActive())
86              remove(t);
87  }
88
89  /**
90   * remove transaction from the set.
91   * @param t Transaction to remove.
92   * @return Whether this transaction was already present.
93   */
94  public boolean remove(Transaction t) {
95      boolean res = elements.contains(t);
96      if (res) elements.remove(t);
97      return res;
98  }
99
100  /**
101   * Discard all elements of this set.
102   */
103  public void clear() {
104      elements.clear();
105  }
106
107  /**
108   * How many transactions in the set?
109   * @return Number of transactions in the set.
110   */
111  public int size() {
112      return elements.size();
113  }
114
115  /**
116   * Iterate over transaction in the set.
117   * @return Iterator over transactions in the set.
118   */
119  public java.util.Iterator<Transaction> iterator() {
120    return elements.iterator();
121  }
122}
Note: See TracBrowser for help on using the browser.