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

org.infinispan.util.ReadOnlyDataContainerBackedKeySet Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import org.infinispan.container.DataContainer;

/**
 * A Set view of keys in a data container, which is read-only and has efficient contains(), unlike some data container
 * ley sets.
 *
 * @author Manik Surtani
 * @since 4.1
 * @deprecated DataContainer keySet method will be removed in the future. See {@link DataContainer#keySet()}
 */
@Deprecated
public class ReadOnlyDataContainerBackedKeySet implements Set {

   final DataContainer container;
   Set keySet;

   public ReadOnlyDataContainerBackedKeySet(DataContainer container) {
      this.container = container;
   }

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

   @Override
   public boolean isEmpty() {
      // We don't check size since it isn't constant and most likely allocates an iterator anyways.
      return !container.iterator().hasNext();
   }

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

   @Override
   public Iterator iterator() {
      if (keySet == null) keySet = container.keySet();
      return keySet.iterator();
   }

   @Override
   public Object[] toArray() {
      if (keySet == null) keySet = container.keySet();
      return keySet.toArray();
   }

   @Override
   public  T[] toArray(T[] a) {
      if (keySet == null) keySet = container.keySet();
      return keySet.toArray(a);
   }

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

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

   @Override
   public boolean containsAll(Collection c) {
      boolean ca = true;
      for (Object o: c) {
         ca = ca && contains(o);
         if (!ca) return false;
      }
      return ca;
   }

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

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

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

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