Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.util;
  
  import java.io.Reader;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
Factory for generating immutable type wrappers.

Author(s):
Jason T. Greene
Galder ZamarreƱo
Tristan Tarrant
Since:
4.0
 
 public class Immutables {
   
Whether or not this collection type is immutable

Parameters:
o a Collection, Set, List, or Map
Returns:
true if immutable, false if not
 
    public static boolean isImmutable(Object o) {
       return o instanceof Immutable;
    }

   
Converts a Collection to an immutable List by copying it.

Parameters:
source the collection to convert
Returns:
a copied/converted immutable list
 
    public static <T> List<T> immutableListConvert(Collection<? extends T> source) {
       return new ImmutableListCopy<T>(source);
    }

   
Creates an immutable copy of the list.

Parameters:
list the list to copy
Returns:
the immutable copy
 
    public static <T> List<T> immutableListCopy(List<T> list) {
       if (list == nullreturn null;
       if (list.isEmpty()) return InfinispanCollections.emptyList();
       if (list.size() == 1) return Collections.singletonList(list.get(0));
       return new ImmutableListCopy<T>(list);
    }

   
Creates an immutable copy of the properties.

Parameters:
properties the TypedProperties to copy
Returns:
the immutable copy
 
    public static TypedProperties immutableTypedProperties(TypedProperties properties) {
       if (properties == nullreturn null;
       return new ImmutableTypedProperties(properties);
    }

   
Wraps an array with an immutable list. There is no copying involved.

Parameters:
<T>
array the array to wrap
Returns:
a list containing the array
 
    public static <T> List<T> immutableListWrap(T... array) {
       return new ImmutableListCopy<T>(array);
    }

   
Creates a new immutable list containing the union (combined entries) of both lists.

Parameters:
list1 contains the first elements of the new list
list2 contains the successor elements of the new list
Returns:
a new immutable merged copy of list1 and list2
 
    public static <T> List<T> immutableListMerge(List<? extends T> list1List<? extends T> list2) {
      return new ImmutableListCopy<T>(list1list2);
   }

   
Converts a Collections into an immutable Set by copying it.

Parameters:
collection the collection to convert/copy
Returns:
a new immutable set containing the elements in collection
   public static <T> Set<T> immutableSetConvert(Collection<? extends T> collection) {
      return immutableSetWrap(new HashSet<T>(collection));
   }

   
Wraps a set with an immutable set. There is no copying involved.

Parameters:
set the set to wrap
Returns:
an immutable set wrapper that delegates to the original set
   public static <T> Set<T> immutableSetWrap(Set<? extends T> set) {
      return new ImmutableSetWrapper<T>(set);
   }

   
Creates an immutable copy of the specified set.

Parameters:
set the set to copy from
Returns:
an immutable set copy
   public static <T> Set<T> immutableSetCopy(Set<T> set) {
      if (set == nullreturn null;
      if (set.isEmpty()) return InfinispanCollections.emptySet();
      if (set.size() == 1) return Collections.singleton(set.iterator().next());
      Set<? extends T> copy = ObjectDuplicator.duplicateSet(set);
      if (copy == null)
         // Set uses Collection copy-ctor
         copy = attemptCopyConstructor(setCollection.class);
      if (copy == null)
         copy = new HashSet<T>(set);
      return new ImmutableSetWrapper<T>(copy);
   }


   
Wraps a map with an immutable map. There is no copying involved.

Parameters:
map the map to wrap
Returns:
an immutable map wrapper that delegates to the original map
   public static <K, V> Map<K, V> immutableMapWrap(Map<? extends K, ? extends V> map) {
      return new ImmutableMapWrapper<K, V>(map);
   }

   
Creates an immutable copy of the specified map.

Parameters:
map the map to copy from
Returns:
an immutable map copy
   public static <K, V> Map<K, V> immutableMapCopy(Map<K, V> map) {
      if (map == nullreturn null;
      if (map.isEmpty()) return InfinispanCollections.emptyMap();
      if (map.size() == 1) {
         Map.Entry<K, V> me = map.entrySet().iterator().next();
         return Collections.singletonMap(me.getKey(), me.getValue());
      }
      Map<? extends K, ? extends V> copy = ObjectDuplicator.duplicateMap(map);
      if (copy == null)
         copy = attemptCopyConstructor(mapMap.class);
      if (copy == null)
         copy = new HashMap<K, V>(map);
      return new ImmutableMapWrapper<K, V>(copy);
   }

   
Creates a new immutable copy of the specified Collection.

Parameters:
collection the collection to copy
Returns:
an immutable copy
   public static <T> Collection<T> immutableCollectionCopy(Collection<T> collection) {
      if (collection == nullreturn null;
      if (collection.isEmpty()) return InfinispanCollections.emptySet();
      if (collection.size() == 1) return Collections.singleton(collection.iterator().next());
      Collection<? extends T> copy = ObjectDuplicator.duplicateCollection(collection);
      if (copy == null)
         copy = attemptCopyConstructor(collectionCollection.class);
      if (copy == null)
         copy = new ArrayList<T>(collection);
      return new ImmutableCollectionWrapper<T>(copy);
   }

   
Wraps a collection with an immutable collection. There is no copying involved.

Parameters:
collection the collection to wrap
Returns:
an immutable collection wrapper that delegates to the original collection
   public static <T> Collection<T> immutableCollectionWrap(Collection<? extends T> collection) {
      return new ImmutableCollectionWrapper<T>(collection);
   }
   @SuppressWarnings("unchecked")
   private static <T> T attemptCopyConstructor(T sourceClass<? super T> clazz) {
      try {
         return (T) source.getClass().getConstructor(clazz).newInstance(source);
      }
      catch (Exception e) {
      }
      return null;
   }

   
Wraps a java.util.Map.Entry} with an immutable java.util.Map.Entry}. There is no copying involved.

Parameters:
entry the mapping to wrap.
Returns:
an immutable java.util.Map.Entry} wrapper that delegates to the original mapping.
   public static <K, V> Map.Entry<K, V> immutableEntry(Map.Entry<K, V> entry) {
      return new ImmutableEntry<K, V>(entry);
   }
   public interface  Immutable {
   }
   /*
    * Immutable wrapper types.
    *
    * We have to re-implement Collections.unmodifiableXXX, since it is not
    * simple to detect them (the class names are JDK dependent).
    */
   private static class ImmutableIteratorWrapper<E> implements Iterator<E> {
      private Iterator<? extends E> iterator;
      public ImmutableIteratorWrapper(Iterator<? extends E> iterator) {
         this. = iterator;
      }
      @Override
      public boolean hasNext() {
         return .hasNext();
      }
      @Override
      public E next() {
         return .next();
      }
      @Override
      public void remove() {
         throw new UnsupportedOperationException();
      }
   }
   private static class ImmutableCollectionWrapper<E> implements Collection<E>, SerializableImmutable {
      private static final long serialVersionUID = 6777564328198393535L;
      Collection<? extends E> collection;
      public ImmutableCollectionWrapper(Collection<? extends E> collection) {
         this. = collection;
      }
      @Override
      public boolean add(E o) {
         throw new UnsupportedOperationException();
      }
      @Override
      public boolean addAll(Collection<? extends E> c) {
         throw new UnsupportedOperationException();
      }
      @Override
      public void clear() {
         throw new UnsupportedOperationException();
      }
      @Override
      public boolean contains(Object o) {
         return .contains(o);
      }
      @Override
      public boolean containsAll(Collection<?> c) {
         return .containsAll(c);
      }
      @Override
      public boolean equals(Object o) {
         return .equals(o);
      }
      @Override
      public int hashCode() {
         return .hashCode();
      }
      @Override
      public boolean isEmpty() {
         return .isEmpty();
      }
      @Override
      public Iterator<E> iterator() {
         return new ImmutableIteratorWrapper<E>(.iterator());
      }
      @Override
      public boolean remove(Object o) {
         throw new UnsupportedOperationException();
      }
      @Override
      public boolean removeAll(Collection<?> c) {
         throw new UnsupportedOperationException();
      }
      @Override
      public boolean retainAll(Collection<?> c) {
         throw new UnsupportedOperationException();
      }
      @Override
      public int size() {
         return .size();
      }
      @Override
      public Object[] toArray() {
         return .toArray();
      }
      @Override
      public <T> T[] toArray(T[] a) {
         return .toArray(a);
      }
      @Override
      public String toString() {
         return .toString();
      }
   }

   
Immutable version of Map.Entry for traversing immutable collections.
   private static class ImmutableEntry<K, V> implements Entry<K, V>, Immutable {
      private K key;
      private V value;
      private int hash;
      ImmutableEntry(Entry<? extends K, ? extends V> entry) {
         this. = entry.getKey();
         this. = entry.getValue();
         this. = entry.hashCode();
      }
      @Override
      public K getKey() {
         return ;
      }
      @Override
      public V getValue() {
         return ;
      }
      @Override
      public V setValue(V value) {
         throw new UnsupportedOperationException();
      }
      private static boolean eq(Object o1Object o2) {
         return o1 == o2 || (o1 != null && o1.equals(o2));
      }
      @Override
      @SuppressWarnings("unchecked")
      public boolean equals(Object o) {
         if (!(o instanceof Entry))
            return false;
         Entry<K, V> entry = (Entry<K, V>) o;
         return eq(entry.getKey(), ) && eq(entry.getValue(), );
      }
      @Override
      public int hashCode() {
         return ;
      }
      @Override
      public String toString() {
         return getKey() + "=" + getValue();
      }
   }
   private static class ImmutableSetWrapper<E> extends ImmutableCollectionWrapper<E> implements Set<E>, SerializableImmutable {
      private static final long serialVersionUID = 7991492805176142615L;
      public ImmutableSetWrapper(Set<? extends E> set) {
         super(set);
      }
   }
   private static class ImmutableReversibleOrderedSetWrapper<E> extends ImmutableCollectionWrapper<E> implements ReversibleOrderedSet<E>, SerializableImmutable {
      private static final long serialVersionUID = 7991492805176142615L;
      public ImmutableReversibleOrderedSetWrapper(Set<? extends E> set) {
         super(set);
      }
      @Override
      public Iterator<E> reverseIterator() {
         return new ImmutableIteratorWrapper<E>(((ReversibleOrderedSet<? extends E>) ).reverseIterator());
      }
   }
   private static class ImmutableEntrySetWrapper<K, V> extends ImmutableSetWrapper<Entry<K, V>> {
      private static final long serialVersionUID = 6378667653889667692L;
      @SuppressWarnings("unchecked")
      public ImmutableEntrySetWrapper(Set<? extends Entry<? extends K, ? extends V>> set) {
         super((Set<Entry<K, V>>) set);
      }
      @Override
      public Object[] toArray() {
         Object[] array = new Object[.size()];
         int i = 0;
         for (Entry<K, V> entry : this)
            array[i++] = entry;
         return array;
      }
      @Override
      @SuppressWarnings("unchecked")
      public <T> T[] toArray(T[] array) {
         int size = .size();
         if (array.length < size)
            array = (T[]) Array.newInstance(array.getClass().getComponentType(), size);
         int i = 0;
         Object[] result = array;
         for (Entry<K, V> entry : this)
            result[i++] = entry;
         return array;
      }
      @Override
      public Iterator<Entry<K, V>> iterator() {
         return new ImmutableIteratorWrapper<Entry<K, V>>(.iterator()) {
            @Override
            public Entry<K, V> next() {
               return new ImmutableEntry<K, V>(super.next());
            }
         };
      }
   }
   private static class ImmutableMapWrapper<K, V> implements Map<K, V>, SerializableImmutable {
      private static final long serialVersionUID = 708144227046742221L;
      private Map<? extends K, ? extends V> map;
      public ImmutableMapWrapper(Map<? extends K, ? extends V> map) {
         this. = map;
      }
      @Override
      public void clear() {
         throw new UnsupportedOperationException();
      }
      @Override
      public boolean containsKey(Object key) {
         return .containsKey(key);
      }
      @Override
      public boolean containsValue(Object value) {
         return .containsValue(value);
      }
      @Override
      public Set<Entry<K, V>> entrySet() {
         return new ImmutableEntrySetWrapper<K, V>(.entrySet());
      }
      @Override
      public boolean equals(Object o) {
         return .equals(o);
      }
      @Override
      public V get(Object key) {
         return .get(key);
      }
      @Override
      public int hashCode() {
         return .hashCode();
      }
      @Override
      public boolean isEmpty() {
         return .isEmpty();
      }
      @Override
      public Set<K> keySet() {
         return new ImmutableSetWrapper<K>(.keySet());
      }
      @Override
      public V put(K key, V value) {
         throw new UnsupportedOperationException();
      }
      @Override
      public void putAll(Map<? extends K, ? extends V> t) {
         throw new UnsupportedOperationException();
      }
      @Override
      public V remove(Object key) {
         throw new UnsupportedOperationException();
      }
      @Override
      public int size() {
         return .size();
      }
      @Override
      public Collection<V> values() {
         return new ImmutableCollectionWrapper<V>(.values());
      }
      @Override
      public String toString() {
         return .toString();
      }
   }
   public static class ImmutableMapWrapperExternalizer extends AbstractExternalizer<Map> {
      @Override
      public void writeObject(ObjectOutput outputMap mapthrows IOException {
         MarshallUtil.marshallMap(mapoutput);
      }
      @Override
      public Map readObject(ObjectInput inputthrows IOExceptionClassNotFoundException {
         Map<ObjectObjectmap = new HashMap<ObjectObject>();
         MarshallUtil.unmarshallMap(mapinput);
         return Immutables.immutableMapWrap(map);
      }
      @Override
      public Integer getId() {
         return .;
      }
      @Override
      @SuppressWarnings("unchecked")
      public Set<Class<? extends Map>> getTypeClasses() {
         return Util.<Class<? extends Map>>asSet(ImmutableMapWrapper.class);
      }
   }
   private static class ImmutableTypedProperties extends TypedProperties {
      ImmutableTypedProperties(TypedProperties properties) {
         super();
         if (properties != null && !properties.isEmpty()) {
            for (Map.Entry<ObjectObjecteproperties.entrySet()) super.put(e.getKey(), e.getValue());
         }
      }
      @Override
      public synchronized void clear() {
         throw new UnsupportedOperationException();
      }
      @Override
      public Set<java.util.Map.Entry<ObjectObject>> entrySet() {
         return new ImmutableEntrySetWrapper<ObjectObject>(super.entrySet());
      }
      @Override
      public Set<ObjectkeySet() {
         return new ImmutableSetWrapper<Object>(super.keySet());
      }
      @Override
      public synchronized void load(InputStream inStreamthrows IOException {
         throw new UnsupportedOperationException();
      }
      @Override
      public synchronized void load(Reader readerthrows IOException {
         throw new UnsupportedOperationException();
      }
      @Override
      public synchronized void loadFromXML(InputStream inthrows IOExceptionInvalidPropertiesFormatException {
         throw new UnsupportedOperationException();
      }
      @Override
      public synchronized Object put(Object keyObject value) {
         throw new UnsupportedOperationException();
      }
      @Override
      public synchronized void putAll(Map<?, ?> t) {
         throw new UnsupportedOperationException();
      }
      @Override
      public synchronized Object remove(Object key) {
         throw new UnsupportedOperationException();
      }
      @Override
      public synchronized TypedProperties setProperty(String keyString value) {
         throw new UnsupportedOperationException();
      }
      @Override
      public Set<StringstringPropertyNames() {
         return new ImmutableSetWrapper<String>(super.stringPropertyNames());
      }
      @Override
      public Collection<Objectvalues() {
         return new ImmutableCollectionWrapper<Object>(super.values());
      }
   }
New to GrepCode? Check out our FAQ X