001: package prefuse.data.tuple;
002:
003: import java.util.Iterator;
004: import java.util.LinkedHashSet;
005:
006: import prefuse.data.Tuple;
007: import prefuse.data.event.EventConstants;
008:
009: /**
010: * <p>TupleSet implementation that maintains a set of heterogeneous Tuples
011: * -- tuples that can come from any backing data source. This class supports
012: * {@link #addTuple(Tuple)} and {@link #removeTuple(Tuple)} but does not
013: * support adding new columns to contained tuples.</p>
014: *
015: * <p>This TupleSet uses a {@link java.util.LinkedHashSet} to support fast
016: * lookup of contained tuples while mainting Tuples in the order in which
017: * they are added to the set.</p>
018: *
019: * @author <a href="http://jheer.org">jeffrey heer</a>
020: */
021: public class DefaultTupleSet extends AbstractTupleSet implements
022: EventConstants {
023: protected LinkedHashSet m_tuples;
024:
025: /**
026: * Create a new, empty DefaultTupleSet.
027: */
028: public DefaultTupleSet() {
029: m_tuples = new LinkedHashSet();
030: }
031:
032: /**
033: * @see prefuse.data.tuple.TupleSet#getTupleCount()
034: */
035: public int getTupleCount() {
036: return m_tuples.size();
037: }
038:
039: /**
040: * @see prefuse.data.tuple.TupleSet#addTuple(prefuse.data.Tuple)
041: */
042: public Tuple addTuple(Tuple t) {
043: t = addInternal(t);
044: if (t != null)
045: fireTupleEvent(t, INSERT);
046: return t;
047: }
048:
049: /**
050: * @see prefuse.data.tuple.TupleSet#setTuple(prefuse.data.Tuple)
051: */
052: public Tuple setTuple(Tuple t) {
053: Tuple[] rem = clearInternal();
054: t = addInternal(t);
055: Tuple[] add = t == null ? null : new Tuple[] { t };
056: fireTupleEvent(add, rem);
057: return t;
058: }
059:
060: /**
061: * Adds a tuple without firing a notification.
062: * @param t the Tuple to add
063: * @return the added Tuple
064: */
065: protected final Tuple addInternal(Tuple t) {
066: if (m_tuples.add(t)) {
067: return t;
068: } else {
069: return null;
070: }
071: }
072:
073: /**
074: * @see prefuse.data.tuple.TupleSet#containsTuple(prefuse.data.Tuple)
075: */
076: public boolean containsTuple(Tuple t) {
077: return m_tuples.contains(t);
078: }
079:
080: /**
081: * @see prefuse.data.tuple.TupleSet#removeTuple(prefuse.data.Tuple)
082: */
083: public boolean removeTuple(Tuple t) {
084: boolean b = removeInternal(t);
085: if (b)
086: fireTupleEvent(t, DELETE);
087: return b;
088: }
089:
090: /**
091: * Removes a tuple without firing a notification.
092: * @param t the tuple to remove
093: * @return true if the tuple is removed successfully, false otherwise
094: */
095: protected final boolean removeInternal(Tuple t) {
096: return (m_tuples.remove(t));
097: }
098:
099: /**
100: * @see prefuse.data.tuple.TupleSet#clear()
101: */
102: public void clear() {
103: if (getTupleCount() > 0) {
104: Tuple[] t = clearInternal();
105: fireTupleEvent(null, t);
106: }
107: }
108:
109: /**
110: * Clear the internal state without firing a notification.
111: * @return an array of removed tuples
112: */
113: public Tuple[] clearInternal() {
114: Tuple[] t = new Tuple[getTupleCount()];
115: Iterator iter = tuples();
116: for (int i = 0; iter.hasNext(); ++i) {
117: t[i] = (Tuple) iter.next();
118: }
119: m_tuples.clear();
120: return t;
121: }
122:
123: /**
124: * @see prefuse.data.tuple.TupleSet#tuples()
125: */
126: public Iterator tuples() {
127: return m_tuples.iterator();
128: }
129:
130: /**
131: * Get the contents of this TupleSet as an array.
132: * @return the contents of this TupleSet as an array
133: */
134: public Tuple[] toArray() {
135: Tuple[] t = new Tuple[getTupleCount()];
136: m_tuples.toArray(t);
137: return t;
138: }
139:
140: } // end of class DefaultTupleSet
|