1 16 package org.apache.commons.collections.map; 17 18 import java.lang.reflect.Array ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.apache.commons.collections.Unmodifiable; 25 import org.apache.commons.collections.iterators.AbstractIteratorDecorator; 26 import org.apache.commons.collections.keyvalue.AbstractMapEntryDecorator; 27 import org.apache.commons.collections.set.AbstractSetDecorator; 28 29 37 public final class UnmodifiableEntrySet 38 extends AbstractSetDecorator implements Unmodifiable { 39 40 46 public static Set decorate(Set set) { 47 if (set instanceof Unmodifiable) { 48 return set; 49 } 50 return new UnmodifiableEntrySet(set); 51 } 52 53 60 private UnmodifiableEntrySet(Set set) { 61 super(set); 62 } 63 64 public boolean add(Object object) { 66 throw new UnsupportedOperationException (); 67 } 68 69 public boolean addAll(Collection coll) { 70 throw new UnsupportedOperationException (); 71 } 72 73 public void clear() { 74 throw new UnsupportedOperationException (); 75 } 76 77 public boolean remove(Object object) { 78 throw new UnsupportedOperationException (); 79 } 80 81 public boolean removeAll(Collection coll) { 82 throw new UnsupportedOperationException (); 83 } 84 85 public boolean retainAll(Collection coll) { 86 throw new UnsupportedOperationException (); 87 } 88 89 public Iterator iterator() { 91 return new UnmodifiableEntrySetIterator(collection.iterator()); 92 } 93 94 public Object [] toArray() { 95 Object [] array = collection.toArray(); 96 for (int i = 0; i < array.length; i++) { 97 array[i] = new UnmodifiableEntry((Map.Entry ) array[i]); 98 } 99 return array; 100 } 101 102 public Object [] toArray(Object array[]) { 103 Object [] result = array; 104 if (array.length > 0) { 105 result = (Object []) Array.newInstance(array.getClass().getComponentType(), 0); 108 } 109 result = collection.toArray(result); 110 for (int i = 0; i < result.length; i++) { 111 result[i] = new UnmodifiableEntry((Map.Entry ) result[i]); 112 } 113 114 if (result.length > array.length) { 116 return result; 117 } 118 119 System.arraycopy(result, 0, array, 0, result.length); 121 if (array.length > result.length) { 122 array[result.length] = null; 123 } 124 return array; 125 } 126 127 131 final static class UnmodifiableEntrySetIterator extends AbstractIteratorDecorator { 132 133 protected UnmodifiableEntrySetIterator(Iterator iterator) { 134 super(iterator); 135 } 136 137 public Object next() { 138 Map.Entry entry = (Map.Entry ) iterator.next(); 139 return new UnmodifiableEntry(entry); 140 } 141 142 public void remove() { 143 throw new UnsupportedOperationException (); 144 } 145 } 146 147 151 final static class UnmodifiableEntry extends AbstractMapEntryDecorator { 152 153 protected UnmodifiableEntry(Map.Entry entry) { 154 super(entry); 155 } 156 157 public Object setValue(Object obj) { 158 throw new UnsupportedOperationException (); 159 } 160 } 161 162 } 163 | Popular Tags |