package org.jboss.cache.util.concurrent;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A simple Set implementation backed by a {@link java.util.concurrent.ConcurrentHashMap} to deal with the fact that the
 * JDK does not have a proper concurrent Set implementation that uses efficient lock striping.
 * <p/>
 * Note that values are stored as keys in the underlying Map, with a static dummy object as value.
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
 * @since 2.0.0
 */
public class ConcurrentHashSet<E> extends AbstractSet<E>
{
   protected ConcurrentHashMap<E, Object> map;
   private static final Object DUMMY = new Object();

   public ConcurrentHashSet()
   {
      map = new ConcurrentHashMap<E, Object>();
   }

   /**
    * @param concurrency concurrency level passed in to the CHM constructor.  See {@link ConcurrentHashMap#ConcurrentHashMap(int, float, int)} javadocs for details.
    */
   public ConcurrentHashSet(int concurrency)
   {
      map = new ConcurrentHashMap<E, Object>(16, 0.75f, concurrency);
   }

   /**
    * Params passed in to the CHM constructor.  See {@link ConcurrentHashMap#ConcurrentHashMap(int, float, int)} javadocs for details.
    */
   public ConcurrentHashSet(int initSize, float loadFactor, int concurrency)
   {
      map = new ConcurrentHashMap<E, Object>(initSize, loadFactor, concurrency);
   }

   @Override
   public int size()
   {
      return map.size();
   }

   @Override
   public boolean isEmpty()
   {
      return map.isEmpty();
   }

   @Override
   public boolean contains(Object o)
   {
      return map.containsKey(o);
   }

   @Override
   public Iterator<E> iterator()
   {
      return map.keySet().iterator();
   }

   @Override
   public Object[] toArray()
   {
      return map.keySet().toArray();
   }

   @Override
   public <T> T[] toArray(T[] a)
   {
      return map.keySet().toArray(a);
   }

   @Override
   public boolean add(E o)
   {
      Object v = map.put(o, DUMMY);
      return v == null;
   }

   @Override
   public boolean remove(Object o)
   {
      Object v = map.remove(o);
      return v != null;
   }

   @Override
   public boolean containsAll(Collection<?> c)
   {
      return map.keySet().containsAll(c);
   }

   @Override
   public boolean addAll(Collection<? extends E> c)
   {
      throw new UnsupportedOperationException("Not supported in this implementation since additional locking is required and cannot directly be delegated to multiple calls to ConcurrentHashMap");
   }

   @Override
   public boolean retainAll(Collection<?> c)
   {
      throw new UnsupportedOperationException("Not supported in this implementation since additional locking is required and cannot directly be delegated to multiple calls to ConcurrentHashMap");
   }

   @Override
   public boolean removeAll(Collection<?> c)
   {
      throw new UnsupportedOperationException("Not supported in this implementation since additional locking is required and cannot directly be delegated to multiple calls to ConcurrentHashMap");
   }

   @Override
   public void clear()
   {
      map.clear();
   }
}
