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

org.infinispan.commons.util.ImmutableHopscotchHashSet Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * Wrap a {@link HopscotchHashMap} and allow using it as a {@link Set}.
 *
 * @param  The element type
 * @since 9.3
 * @author Dan Berindei
 */
public class ImmutableHopscotchHashSet implements Set {
   private static final Object PRESENT = new Object();

   private final HopscotchHashMap map;

   public ImmutableHopscotchHashSet(Collection collection) {
      map = new HopscotchHashMap<>(collection.size());
      for (E e : collection) {
         map.put(e, PRESENT);
      }
   }

   @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 iterator() {
      return new Immutables.ImmutableIteratorWrapper<>(map.keySet().iterator());
   }

   @Override
   public void forEach(Consumer action) {
      map.keySet().forEach(action);
   }

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

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

   @Override
   public boolean add(E e) {
      throw new UnsupportedOperationException("add");
   }

   @Override
   public boolean remove(Object o) {
      throw new UnsupportedOperationException("remove");
   }

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

   @Override
   public boolean addAll(Collection c) {
      throw new UnsupportedOperationException("addAll");
   }

   @Override
   public boolean retainAll(Collection c) {
      throw new UnsupportedOperationException("retainAll");
   }

   @Override
   public boolean removeAll(Collection c) {
      throw new UnsupportedOperationException("removeAll");
   }

   @Override
   public boolean removeIf(Predicate filter) {
      throw new UnsupportedOperationException("removeIf");
   }

   @Override
   public void clear() {
      throw new UnsupportedOperationException("clear");
   }

   // Use the default implementation of spliterator(), stream(), and parallelStream()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy