001: /*
002: * $Id: IntSet.java,v 1.1 2005/12/20 18:32:46 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.rowcollection;
042:
043: import org.apache.commons.collections.primitives.IntCollection;
044: import org.apache.commons.collections.primitives.IntIterator;
045: import org.apache.commons.collections.primitives.IntListIterator;
046:
047: /**
048: * An int set that uses IntHashMap to keep ids.
049: *
050: * @author Ahimanikya Satapathy
051: */
052: public class IntSet implements IntCollection {
053:
054: // Dummy value to associate with an Object in the backing Map
055: private static final Object PRESENT = new Object();
056:
057: private transient IntHashMap map;
058:
059: /**
060: * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has default
061: * initial capacity (16) and load factor (0.75).
062: */
063: public IntSet() {
064: map = new IntHashMap(16);
065: }
066:
067: /**
068: * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has the
069: * specified initial capacity and default load factor, which is <tt>0.75</tt>.
070: *
071: * @param initialCapacity the initial capacity of the hash table.
072: * @throws IllegalArgumentException if the initial capacity is less than zero.
073: */
074: public IntSet(int initialCapacity) {
075: map = new IntHashMap(initialCapacity);
076: }
077:
078: /**
079: * Adds the specified element to this set if it is not already present.
080: *
081: * @param id element to be added to this set.
082: * @return <tt>true</tt> if the set did not already contain the specified element.
083: */
084: public boolean add(int id) {
085: return map.put(id, PRESENT) == null;
086: }
087:
088: /**
089: * Removes all of the elements from this set.
090: */
091: public void clear() {
092: map.clear();
093: }
094:
095: /**
096: * Returns <tt>true</tt> if this set contains the specified element.
097: *
098: * @param id element whose presence in this set is to be tested.
099: * @return <tt>true</tt> if this set contains the specified element.
100: */
101: public boolean contains(int id) {
102: return map.containsKey(id);
103: }
104:
105: /**
106: * Returns <tt>true</tt> if this set contains no elements.
107: *
108: * @return <tt>true</tt> if this set contains no elements.
109: */
110: public boolean isEmpty() {
111: return map.isEmpty();
112: }
113:
114: public IntIterator iterator() {
115: return map.keyIterator();
116: }
117:
118: public IntListIterator listIterator() {
119: return map.keyIterator();
120: }
121:
122: /**
123: * Removes the specified element from this set if it is present.
124: *
125: * @param id object to be removed from this set, if present.
126: * @return <tt>true</tt> if the set contained the specified element.
127: */
128: public boolean remove(int id) {
129: return map.remove(id) == PRESENT;
130: }
131:
132: /**
133: * Returns the number of elements in this set (its cardinality).
134: *
135: * @return the number of elements in this set (its cardinality).
136: */
137: public int size() {
138: return map.size();
139: }
140:
141: public boolean addAll(IntCollection c) {
142: throw new UnsupportedOperationException();
143: }
144:
145: public boolean containsAll(IntCollection c) {
146: throw new UnsupportedOperationException();
147: }
148:
149: public boolean removeAll(IntCollection c) {
150: throw new UnsupportedOperationException();
151: }
152:
153: public boolean removeElement(int element) {
154: return map.remove(element) == PRESENT;
155: }
156:
157: public boolean retainAll(IntCollection c) {
158: throw new UnsupportedOperationException();
159: }
160:
161: public int[] toArray() {
162: throw new UnsupportedOperationException();
163: }
164:
165: public int[] toArray(int[] a) {
166: throw new UnsupportedOperationException();
167: }
168: }
|