All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gw.util.concurrent.ConcurrentHashSet Maven / Gradle / Ivy

There is a newer version: 1.18.2
Show newest version
/*
 * Copyright 2014 Guidewire Software, Inc.
 */

package gw.util.concurrent;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 */
public class ConcurrentHashSet implements Set {
  private final ConcurrentMap _map;

  public ConcurrentHashSet() {
    _map = new ConcurrentHashMap();
  }

  public ConcurrentHashSet( int initialCapacity ) {
    _map = new ConcurrentHashMap( initialCapacity );
  }

  public ConcurrentHashSet( Set set ) {
    _map = new ConcurrentHashMap( set.size() );
    for( K value: set ) {
      _map.put( value, Boolean.TRUE );
    }
  }

  public int size() {
    return _map.size();
  }

  public boolean isEmpty() {
    return _map.isEmpty();
  }

  public boolean contains( Object o ) {
    return _map.containsKey( o );
  }

  public Iterator iterator() {
    return _map.keySet().iterator();
  }

  public Object[] toArray() {
    return _map.keySet().toArray();
  }

  public  T[] toArray( T[] a ) {
    return _map.keySet().toArray( a );
  }

  public boolean add( K o ) {
    return _map.putIfAbsent( o, Boolean.TRUE ) == null;
  }

  public boolean remove( Object o ) {
    return _map.keySet().remove( o );
  }

  public boolean containsAll( Collection c ) {
    return _map.keySet().containsAll( c );
  }

  public boolean addAll( Collection c ) {
    boolean ret = false;
    for( K value: c ) {
      ret |= add( value );
    }

    return ret;
  }

  public boolean retainAll( Collection c ) {
    return _map.keySet().retainAll( c );
  }

  public boolean removeAll( Collection c ) {
    return _map.keySet().removeAll( c );
  }

  public void clear() {
    _map.clear();
  }

  @Override
  public String toString() {
    return _map.keySet().toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy