Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.utilint;
  
 import java.util.Set;
 
TinyHashSet is used to optimize (for speed, not space) the case where a HashSet generally holds a single element. This saves us the cost of creating the HashSet and related elements as well as call Object.hashCode(). If single != null, it's the only element in the TinyHashSet. If set != null then there are multiple elements in the TinyHashSet. It should never be true that (single != null) && (set != null).
 
 public class TinyHashSet<T> implements Iterable<T> {
 
     private Set<T> set;
     private T single;

    
Creates an empty set.
 
     public TinyHashSet() {
     }

    
Creates a set with one element.
 
     public TinyHashSet(T o) {
          = o;
     }
 
     /*
      * Will return a fuzzy value if not under synchronized control.
      */
     public int size() {
         if ( != null) {
             return 1;
         } else if ( != null) {
             return .size();
         } else {
             return 0;
         }
     }
 
     public boolean contains(T o) {
         assert ( == null) || ( == null);
         if ( != null) {
             if ( == o ||
                 .equals(o)) {
                 return true;
             } else {
                 return false;
             }
         } else if ( != null) {
             return .contains(o);
         } else {
             return false;
         }
     }
 
     public boolean remove(T o) {
         assert ( == null) || ( == null);
         if ( != null) {
             if ( == o ||
                 .equals(o)) {
                  = null;
                 return true;
             } else {
                 return false;
             }
         } else if ( != null) {
             return .remove(o);
         } else {
             return false;
         }
     }
 
     public boolean add(T o) {
         assert ( == null) || ( == null);
         if ( != null) {
             return .add(o);
         } else if ( == null) {
              = o;
             return true;
         } else {
              = new HashSet<T>();
             .add();
             = null;
            return .add(o);
        }
    }
    public Set<T> copy() {
        assert ( == null) || ( == null);
        if ( != null) {
            return new HashSet<T>();
        } else {
            Set<T> ret = new HashSet<T>();
            if ( != null) {
                ret.add();
            }
            return ret;
        }
    }
    public Iterator<T> iterator() {
        assert ( == null) || ( == null);
        if ( != null) {
            return .iterator();
        } else {
            return new SingleElementIterator<T>(this);
        }
    }
    /*
     * Iterator that is used to just return one element.
     */
    public static class SingleElementIterator<T> implements Iterator<T> {
        T theObject;
        TinyHashSet<T> theSet;
        boolean returnedTheObject = false;
        SingleElementIterator(T oTinyHashSet<T> theSet) {
             = o;
            this. = theSet;
             = (o == null);
        }
        public boolean hasNext() {
            return !;
        }
        public T next() {
            if () {
                throw new NoSuchElementException();
            }
             = true;
            return ;
        }
        public void remove() {
            if ( == null ||
                !) {
                throw EnvironmentFailureException.unexpectedState();
            }
            .remove();
        }
    }
New to GrepCode? Check out our FAQ X