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

Revision 2649, 3.2 KB (checked in by mspiegel, 16 months ago)

[warnings] Removed all unused imports in com.sun.fortress.interpreter.* and children packages.

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      myAdd(r);
40  }
41
42  private void myAdd(ReadSet r) {
43      for (Transaction t : elements)
44          if (t.isActive())
45              elements.add(t);
46  }
47
48  public String toString() {
49          return "sealed = " + sealed + " elements = " + elements;
50  }
51     
52
53  /**
54   * Initialize one object from another.
55   * @param aSet Initialize from this other object.
56   */
57  public void copyFrom(ReadSet aSet) {
58      elements.addAll(aSet.elements);
59  }
60
61  public void seal() {
62      sealed = true;
63  }
64
65  /**
66   * Add a new transaction to the set.
67   * @param t Transaction to add.
68   * @return Whether this transaction was already present.
69   */
70  public boolean add(Transaction t) {
71      cleanup();
72      if (sealed) {
73                  FortressTaskRunner.debugPrintln("add of " + t + " to readset " + toString() + " failed because readset was sealed");
74                  return false;
75      } else {
76                  elements.addIfAbsent(t);
77                  return true;
78      }
79  }
80
81  public void cleanup() {
82      for (Transaction t : elements)
83          if (!t.isActive())
84              remove(t);
85  }
86
87  /**
88   * remove transaction from the set.
89   * @param t Transaction to remove.
90   * @return Whether this transaction was already present.
91   */
92  public boolean remove(Transaction t) {
93      boolean res = elements.contains(t);
94      if (res) elements.remove(t);
95      return res;
96  }
97
98  /**
99   * Discard all elements of this set.
100   */
101  public void clear() {
102      elements.clear();
103  }
104
105  /**
106   * How many transactions in the set?
107   * @return Number of transactions in the set.
108   */
109  public int size() {
110      return elements.size();
111  }
112
113  /**
114   * Iterate over transaction in the set.
115   * @return Iterator over transactions in the set.
116   */
117  public java.util.Iterator<Transaction> iterator() {
118    return elements.iterator();
119  }
120}
Note: See TracBrowser for help on using the browser.