View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.map;
18  
19  import java.lang.reflect.Array;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.collections.Unmodifiable;
26  import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
27  import org.apache.commons.collections.keyvalue.AbstractMapEntryDecorator;
28  import org.apache.commons.collections.set.AbstractSetDecorator;
29  
30  /**
31   * Decorates a map entry <code>Set</code> to ensure it can't be altered.
32   * <p>
33   * Attempts to modify it will result in an UnsupportedOperationException. 
34   *
35   * @since 3.0
36   * @version $Id: UnmodifiableEntrySet.java 1429905 2013-01-07 17:15:14Z ggregory $
37   */
38  public final class UnmodifiableEntrySet<K, V>
39          extends AbstractSetDecorator<Map.Entry<K, V>> implements Unmodifiable {
40  
41      /** Serialization version */
42      private static final long serialVersionUID = 1678353579659253473L;
43  
44      /**
45       * Factory method to create an unmodifiable set of Map Entry objects.
46       * 
47       * @param <K>  the key type
48       * @param <V>  the value type
49       * @param set  the set to decorate, must not be null
50       * @return a new unmodifiable entry set
51       * @throws IllegalArgumentException if set is null
52       */
53      public static <K, V> Set<Map.Entry<K, V>> unmodifiableEntrySet(final Set<Map.Entry<K, V>> set) {
54          if (set instanceof Unmodifiable) {
55              return set;
56          }
57          return new UnmodifiableEntrySet<K, V>(set);
58      }
59  
60      //-----------------------------------------------------------------------
61      /**
62       * Constructor that wraps (not copies).
63       * 
64       * @param set  the set to decorate, must not be null
65       * @throws IllegalArgumentException if set is null
66       */
67      private UnmodifiableEntrySet(final Set<Map.Entry<K, V>> set) {
68          super(set);
69      }
70  
71      //-----------------------------------------------------------------------
72      @Override
73      public boolean add(final Map.Entry<K, V> object) {
74          throw new UnsupportedOperationException();
75      }
76  
77      @Override
78      public boolean addAll(final Collection<? extends Map.Entry<K, V>> coll) {
79          throw new UnsupportedOperationException();
80      }
81  
82      @Override
83      public void clear() {
84          throw new UnsupportedOperationException();
85      }
86  
87      @Override
88      public boolean remove(final Object object) {
89          throw new UnsupportedOperationException();
90      }
91  
92      @Override
93      public boolean removeAll(final Collection<?> coll) {
94          throw new UnsupportedOperationException();
95      }
96  
97      @Override
98      public boolean retainAll(final Collection<?> coll) {
99          throw new UnsupportedOperationException();
100     }
101 
102     //-----------------------------------------------------------------------
103     @Override
104     public Iterator<Map.Entry<K, V>> iterator() {
105         return new UnmodifiableEntrySetIterator(collection.iterator());
106     }
107     
108     @Override
109     @SuppressWarnings("unchecked")
110     public Object[] toArray() {
111         final Object[] array = collection.toArray();
112         for (int i = 0; i < array.length; i++) {
113             array[i] = new UnmodifiableEntry((Map.Entry<K, V>) array[i]);
114         }
115         return array;
116     }
117     
118     @Override
119     @SuppressWarnings("unchecked")
120     public <T> T[] toArray(final T[] array) {
121         Object[] result = array;
122         if (array.length > 0) {
123             // we must create a new array to handle multi-threaded situations
124             // where another thread could access data before we decorate it
125             result = (Object[]) Array.newInstance(array.getClass().getComponentType(), 0);
126         }
127         result = collection.toArray(result);
128         for (int i = 0; i < result.length; i++) {
129             result[i] = new UnmodifiableEntry((Map.Entry<K, V>) result[i]);
130         }
131 
132         // check to see if result should be returned straight
133         if (result.length > array.length) {
134             return (T[]) result;
135         }
136 
137         // copy back into input array to fulfill the method contract
138         System.arraycopy(result, 0, array, 0, result.length);
139         if (array.length > result.length) {
140             array[result.length] = null;
141         }
142         return array;
143     }
144     
145     //-----------------------------------------------------------------------
146     /**
147      * Implementation of an entry set iterator.
148      */
149     private class UnmodifiableEntrySetIterator extends AbstractIteratorDecorator<Map.Entry<K, V>> {
150 
151         protected UnmodifiableEntrySetIterator(final Iterator<Map.Entry<K, V>> iterator) {
152             super(iterator);
153         }
154 
155         @Override
156         public Map.Entry<K, V> next() {
157             return new UnmodifiableEntry(iterator.next());
158         }
159 
160         @Override
161         public void remove() {
162             throw new UnsupportedOperationException();
163         }
164     }
165 
166     //-----------------------------------------------------------------------
167     /**
168      * Implementation of a map entry that is unmodifiable.
169      */
170     private class UnmodifiableEntry extends AbstractMapEntryDecorator<K, V> {
171 
172         protected UnmodifiableEntry(final Map.Entry<K, V> entry) {
173             super(entry);
174         }
175 
176         @Override
177         public V setValue(final V obj) {
178             throw new UnsupportedOperationException();
179         }
180     }
181 
182 }