001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.util;
030:
031: import java.util.ArrayList;
032: import java.util.Collection;
033: import java.util.HashSet;
034: import java.util.Iterator;
035: import java.util.Set;
036:
037: /**
038: * Set with values iterated in insertion order. This is similar to the Java 1.4
039: * java.util.LinkedHashSet class, but compatible with earlier JVM versions. This
040: * implementation is for insert-only sets.
041: */
042: public class InsertionOrderedSet implements Set {
043: private final Set m_baseMap;
044: private final ArrayList m_insertList;
045:
046: public InsertionOrderedSet() {
047: m_baseMap = new HashSet();
048: m_insertList = new ArrayList();
049: }
050:
051: /* (non-Javadoc)
052: * @see java.util.Map#clear()
053: */
054: public void clear() {
055: m_baseMap.clear();
056: m_insertList.clear();
057: }
058:
059: /* (non-Javadoc)
060: * @see java.util.Set#isEmpty()
061: */
062: public boolean isEmpty() {
063: return m_baseMap.isEmpty();
064: }
065:
066: /* (non-Javadoc)
067: * @see java.util.Set#size()
068: */
069: public int size() {
070: return m_baseMap.size();
071: }
072:
073: /* (non-Javadoc)
074: * @see java.util.Set#add(java.lang.Object)
075: */
076: public boolean add(Object o) {
077: if (m_baseMap.contains(o)) {
078: return false;
079: } else {
080: m_baseMap.add(o);
081: m_insertList.add(o);
082: return true;
083: }
084: }
085:
086: /* (non-Javadoc)
087: * @see java.util.Set#addAll(java.util.Collection)
088: */
089: public boolean addAll(Collection c) {
090: boolean changed = false;
091: for (Iterator iter = c.iterator(); iter.hasNext();) {
092: Object item = (Object) iter.next();
093: if (add(item)) {
094: changed = true;
095: }
096: }
097: return changed;
098: }
099:
100: /* (non-Javadoc)
101: * @see java.util.Set#contains(java.lang.Object)
102: */
103: public boolean contains(Object o) {
104: return m_baseMap.contains(o);
105: }
106:
107: /* (non-Javadoc)
108: * @see java.util.Set#containsAll(java.util.Collection)
109: */
110: public boolean containsAll(Collection c) {
111: return m_baseMap.containsAll(c);
112: }
113:
114: /* (non-Javadoc)
115: * @see java.util.Set#iterator()
116: */
117: public Iterator iterator() {
118: return m_insertList.iterator();
119: }
120:
121: /* (non-Javadoc)
122: * @see java.util.Set#remove(java.lang.Object)
123: */
124: public boolean remove(Object o) {
125: throw new UnsupportedOperationException("add-only set");
126: }
127:
128: /* (non-Javadoc)
129: * @see java.util.Set#removeAll(java.util.Collection)
130: */
131: public boolean removeAll(Collection c) {
132: throw new UnsupportedOperationException("add-only set");
133: }
134:
135: /* (non-Javadoc)
136: * @see java.util.Set#retainAll(java.util.Collection)
137: */
138: public boolean retainAll(Collection c) {
139: throw new UnsupportedOperationException("add-only set");
140: }
141:
142: /* (non-Javadoc)
143: * @see java.util.Set#toArray()
144: */
145: public Object[] toArray() {
146: return m_insertList.toArray();
147: }
148:
149: /* (non-Javadoc)
150: * @see java.util.Set#toArray(T[])
151: */
152: public Object[] toArray(Object[] a) {
153: return m_insertList.toArray(a);
154: }
155:
156: /**
157: * Get list of values in order added. The returned list is live, and will
158: * grow as new items are added to the set.
159: *
160: * @return list
161: */
162: public ArrayList asList() {
163: return m_insertList;
164: }
165: }
|