Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.util;
  
  
  import java.util.*;
  
 import static java.util.Collections.singletonMap;
 import static java.util.Collections.unmodifiableMap;

Static helpers for Infinispan-specific collections

Author(s):
Manik Surtani
Since:
4.0
 
 public class InfinispanCollections {
 
    private static final Set EMPTY_SET = new EmptySet();
    private static final Map EMPTY_MAP = new EmptyMap();
    private static final List EMPTY_LIST = new EmptyList();
 
    public static final class EmptySet extends AbstractSet<Object> {
 
       private static final Iterator<ObjectEMPTY_ITERATOR =
             new Iterator<Object>() {
          @Override public boolean hasNext() { return false; }
          @Override public Object next() { throw new NoSuchElementException(); }
          @Override public void remove() { throw new UnsupportedOperationException(); }
       };
 
       @Override
       public Iterator<Objectiterator() { return ; }
       @Override
       public int size() { return 0; }
       @Override
       public boolean contains(Object obj) { return false; }
 
       public static final class EmptySetExternalizer
             extends AbstractExternalizer<Set> {
 
          @Override public Integer getId() { return .; }
          @Override public void writeObject(ObjectOutput outputSet object) {}
          @Override public Set readObject(ObjectInput input) { return ; }
 
          @Override
          @SuppressWarnings("unchecked")
          public Set<Class<? extends Set>> getTypeClasses() {
             return Util.<Class<? extends Set>>asSet(EmptySet.class);
          }
       }
    }
 
    public static final class EmptyMap extends java.util.AbstractMap<Object,Object> {
       @Override public int size() { return 0; }
       @Override public boolean isEmpty() { return true; }
       @Override public boolean containsKey(Object key) { return false; }
       @Override public boolean containsValue(Object value) { return false; }
       @Override public Object get(Object key) { return null; }
       @Override public Set<ObjectkeySet() { return emptySet(); }
       @Override public Collection<Objectvalues() { return emptySet(); }
       @Override public Set<Entry<ObjectObject>> entrySet() { return emptySet(); }
       @Override public int hashCode() { return 0; }
 
       @Override
       public boolean equals(Object o) {
          return (o instanceof Map) && ((Mapo).size() == 0;
       }
 
       public static final class EmptyMapExternalizer
             extends AbstractExternalizer<Map> {
 
          @Override public Integer getId() { return .; }
          @Override public void writeObject(ObjectOutput outputMap object) {}
          @Override public Map readObject(ObjectInput input) { return ; }
 
          @Override
          @SuppressWarnings("unchecked")
          public Set<Class<? extends Map>> getTypeClasses() {
             return Util.<Class<? extends Map>>asSet(EmptyMap.class);
          }
       }
    }
 
    public static final class EmptyList
          extends AbstractList<Objectimplements RandomAccess {
 
       private static final Iterator<ObjectEMPTY_ITERATOR =
             new Iterator<Object>() {
                @Override public boolean hasNext() { return false; }
                @Override public Object next() { throw new NoSuchElementException(); }
                @Override public void remove() { throw new UnsupportedOperationException(); }
             };
 
       @Override public int size() { return 0; }
       @Override public boolean contains(Object obj) { return false; }
       @Override public Iterator<Objectiterator() { return ; }
      @Override public Object get(int index) {
         throw new IndexOutOfBoundsException("Index: " + index);
      }
      public static final class EmptyListExternalizer
            extends AbstractExternalizer<List> {
         @Override public Integer getId() { return .; }
         @Override public void writeObject(ObjectOutput outputList object) {}
         @Override public List readObject(ObjectInput input) { return ; }
         @Override
         @SuppressWarnings("unchecked")
         public Set<Class<? extends List>> getTypeClasses() {
            return Util.<Class<? extends List>>asSet(EmptyList.class);
         }
      }
   }
   private static final ReversibleOrderedSet<ObjectEMPTY_ROS = new EmptyReversibleOrderedSet<Object>();
   @SuppressWarnings("unchecked")
   private static final class EmptyReversibleOrderedSet<E> extends AbstractSet<E> implements ReversibleOrderedSet<E> {
      Iterator<E> it = new Iterator() {
         @Override
         public boolean hasNext() {
            return false;
         }
         @Override
         public E next() {
            throw new NoSuchElementException();
         }
         @Override
         public void remove() {
            throw new UnsupportedOperationException();
         }
      };
      @Override
      public Iterator<E> iterator() {
         return ;
      }
      @Override
      public int size() {
         return 0;
      }
      @Override
      public Iterator<E> reverseIterator() {
         return ;
      }
   }

   
A function that converts a type into another one.

Parameters:
<E> Input type.
<T> Output type.
   public static interface Function<E, T> {

      
Transforms an instance of the given input into an instace of the type to be returned.

Parameters:
input Instance of the input type.
Returns:
Instance of the output type.
      T transform(E input);
   }

   
A function that converts an entry into a key/value pair for use in a map.

Parameters:
<K> generated key
<V> generated value
<E> entry input
   public static interface MapMakerFunction<K, V, E> {
      
Transforms the given input into a key/value pair for use in a map

Parameters:
input instance of the input type
Returns:
a Map.Entry parameterized with K and V
      Map.Entry<K, V> transform(E input);
   }

   
Given a map of well known key/value types, it makes a shallow copy of it while at the same time transforming it's value type to a desired output type. The transformation of the value type is done using a given a function.

Parameters:
input contains the input key/value pair map
f function instance to use to transform the value part of the map
<K> input map's key type
<V> desired output type of the map's value
<E> input map's value type
Returns:
a shallow copy of the input Map with all its values transformed.
   public static <K, V, E> Map<K, V> transformMapValue(Map<K, E> inputFunction<E, V> f) {
      // This screams for a map function! Gimme functional programming pleasee...
      if (input.isEmpty()) return InfinispanCollections.emptyMap();
      if (input.size() == 1) {
         Map.Entry<K, E> single = input.entrySet().iterator().next();
         return singletonMap(single.getKey(), f.transform(single.getValue()));
      } else {
         Map<K, V> copy = new HashMap<K, V>(input.size());
         for (Map.Entry<K, E> entry : input.entrySet())
            copy.put(entry.getKey(), f.transform(entry.getValue()));
         return unmodifiableMap(copy);
      }
   }

   
Given a collection, transforms the collection to a map given a InfinispanCollections.MapMakerFunction

Parameters:
input contains a collection of type E
f MapMakerFunction instance to use to transform the collection to a key/value pair
<K> output map's key type
<V> output type of the map's value
<E> input collection's entry type
Returns:
a Map with keys and values generated from the input collection
   public static <K, V, E> Map<K, V> transformCollectionToMap(Collection<E> inputMapMakerFunction<K, V, E> f) {
      // This screams for a map function! Gimme functional programming pleasee...
      if (input.isEmpty()) return InfinispanCollections.emptyMap();
      if (input.size() == 1) {
         E single = input.iterator().next();
         Map.Entry<K, V> entry = f.transform(single);
         return singletonMap(entry.getKey(), entry.getValue());
      } else {
         Map<K, V> map = new HashMap<K, V>(input.size());
         for (E e : input) {
            Map.Entry<K, V> entry = f.transform(e);
            map.put(entry.getKey(), entry.getValue());
         }
         return unmodifiableMap(map);
      }
   }

   
Returns the elements that are present in s1 but which are not present in s2, without changing the contents of neither s1, nor s2.

Parameters:
s1 first set
s2 second set
<E> type of objects in Set
Returns:
the elements in s1 that are not in s2
   public static <E> Set<E> difference(Set<E> s1Set<E> s2) {
      Set<E> copy1 = new HashSet<E>(s1);
      copy1.removeAll(new HashSet<E>(s2));
      return copy1;
   }

   
Returns the empty set (immutable). Contrary to java.util.Collections.emptySet(), the set returned returns a constant Iterator, rather than create a brand new one in each iterator call. This set is marshallable using Infinispan's org.jboss.marshalling.Externalizer framework.

See also:
EMPTY_SET
   @SuppressWarnings("unchecked")
   public static final <T> Set<T> emptySet() {
      return ;
   }

   
Returns the empty map (immutable). Contrary to java.util.Collections.emptyMap(), the map returned returns a constant Iterator, rather than create a brand new one in each iterator call. This set is marshallable using Infinispan's org.jboss.marshalling.Externalizer framework.

See also:
EMPTY_MAP
   @SuppressWarnings("unchecked")
   public static final <K,V> Map<K,V> emptyMap() {
      return ;
   }

   
Returns the empty list (immutable). Contrary to java.util.Collections.emptyList()}, the list returned returns a constant Iterator, rather than create a brand new one in each iterator call. This set is marshallable using Infinispan's org.jboss.marshalling.Externalizer framework.

See also:
EMPTY_LIST
   @SuppressWarnings("unchecked")
   public static final <T> List<T> emptyList() {
      return ;
   }
New to GrepCode? Check out our FAQ X