001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TinyHashSet.java,v 1.6.2.2 2008/01/07 15:14:18 cwl Exp $
007: */
008:
009: package com.sleepycat.je.utilint;
010:
011: import java.util.HashSet;
012: import java.util.Iterator;
013: import java.util.NoSuchElementException;
014: import java.util.Set;
015:
016: /**
017: * TinyHashSet is used to optimize (for speed, not space) the case where a
018: * HashSet generally holds a single element. This saves us the cost of
019: * creating the HashSet and related elements as well as call Object.hashCode().
020: *
021: * If single != null, it's the only element in the TinyHashSet. If set != null
022: * then there are multiple elements in the TinyHashSet. It should never be
023: * true that (single != null) && (set != null).
024: */
025: public class TinyHashSet {
026:
027: private Set set;
028: private Object single;
029:
030: /*
031: * Will return a fuzzy value if the not under synchronized control.
032: */
033: public int size() {
034: if (single != null) {
035: return 1;
036: } else if (set != null) {
037: return set.size();
038: } else {
039: return 0;
040: }
041: }
042:
043: public boolean remove(Object o) {
044: assert (single == null) || (set == null);
045: if (single != null) {
046: if (single == o || single.equals(o)) {
047: single = null;
048: return true;
049: } else {
050: return false;
051: }
052: } else if (set != null) {
053: return set.remove(o);
054: } else {
055: return false;
056: }
057: }
058:
059: public boolean add(Object o) {
060: assert (single == null) || (set == null);
061: if (set != null) {
062: return set.add(o);
063: } else if (single == null) {
064: single = o;
065: return true;
066: } else {
067: set = new HashSet();
068: set.add(single);
069: single = null;
070: return set.add(o);
071: }
072: }
073:
074: public Set copy() {
075: assert (single == null) || (set == null);
076: if (set != null) {
077: return new HashSet(set);
078: } else {
079: Set ret = new HashSet();
080: if (single != null) {
081: ret.add(single);
082: }
083: return ret;
084: }
085: }
086:
087: public Iterator iterator() {
088: assert (single == null) || (set == null);
089: if (set != null) {
090: return set.iterator();
091: } else {
092: return new SingleElementIterator(single, this );
093: }
094: }
095:
096: /*
097: * Iterator that is used to just return one element.
098: */
099: public static class SingleElementIterator implements Iterator {
100: Object theObject;
101: TinyHashSet theSet;
102: boolean returnedTheObject = false;
103:
104: SingleElementIterator(Object o, TinyHashSet theSet) {
105: theObject = o;
106: this .theSet = theSet;
107: returnedTheObject = (o == null);
108: }
109:
110: public boolean hasNext() {
111: return !returnedTheObject;
112: }
113:
114: public Object next() {
115: if (returnedTheObject) {
116: throw new NoSuchElementException();
117: }
118:
119: returnedTheObject = true;
120: return theObject;
121: }
122:
123: public void remove() {
124: if (theObject == null || !returnedTheObject) {
125: throw new IllegalStateException();
126: }
127: theSet.remove(theObject);
128: }
129: }
130: }
|