1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.util; 20 21 import java.util.AbstractSet; 22 import java.util.Collection; 23 import java.util.Iterator; 24 import java.util.Map; 25 import java.util.Set; 26 import java.util.concurrent.ConcurrentHashMap; 27 28 public class ConcurrentHashSet<E> extends AbstractSet<E> implements Set<E> 29 { 30 private final Map<E, Boolean> _map = new ConcurrentHashMap<E, Boolean>(); 31 private transient Set<E> _keys = _map.keySet(); 32 33 public ConcurrentHashSet() 34 { 35 } 36 37 @Override 38 public boolean add(E e) 39 { 40 return _map.put(e,Boolean.TRUE) == null; 41 } 42 43 @Override 44 public void clear() 45 { 46 _map.clear(); 47 } 48 49 @Override 50 public boolean contains(Object o) 51 { 52 return _map.containsKey(o); 53 } 54 55 @Override 56 public boolean containsAll(Collection<?> c) 57 { 58 return _keys.containsAll(c); 59 } 60 61 @Override 62 public boolean equals(Object o) 63 { 64 return o == this || _keys.equals(o); 65 } 66 67 @Override 68 public int hashCode() 69 { 70 return _keys.hashCode(); 71 } 72 73 @Override 74 public boolean isEmpty() 75 { 76 return _map.isEmpty(); 77 } 78 79 @Override 80 public Iterator<E> iterator() 81 { 82 return _keys.iterator(); 83 } 84 85 @Override 86 public boolean remove(Object o) 87 { 88 return _map.remove(o) != null; 89 } 90 91 @Override 92 public boolean removeAll(Collection<?> c) 93 { 94 return _keys.removeAll(c); 95 } 96 97 @Override 98 public boolean retainAll(Collection<?> c) 99 { 100 return _keys.retainAll(c); 101 } 102 103 @Override 104 public int size() 105 { 106 return _map.size(); 107 } 108 109 @Override 110 public Object[] toArray() 111 { 112 return _keys.toArray(); 113 } 114 115 @Override 116 public <T> T[] toArray(T[] a) 117 { 118 return _keys.toArray(a); 119 } 120 121 @Override 122 public String toString() 123 { 124 return _keys.toString(); 125 } 126 } 127