1 16 package org.apache.commons.collections.map; 17 18 import java.lang.reflect.Array ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import org.apache.commons.collections.iterators.AbstractIteratorDecorator; 24 import org.apache.commons.collections.keyvalue.AbstractMapEntryDecorator; 25 import org.apache.commons.collections.set.AbstractSetDecorator; 26 27 46 abstract class AbstractInputCheckedMapDecorator 47 extends AbstractMapDecorator { 48 49 52 protected AbstractInputCheckedMapDecorator() { 53 super(); 54 } 55 56 62 protected AbstractInputCheckedMapDecorator(Map map) { 63 super(map); 64 } 65 66 81 protected abstract Object checkSetValue(Object value); 82 83 93 protected boolean isSetValueChecking() { 94 return true; 95 } 96 97 public Set entrySet() { 99 if (isSetValueChecking()) { 100 return new EntrySet(map.entrySet(), this); 101 } else { 102 return map.entrySet(); 103 } 104 } 105 106 110 static class EntrySet extends AbstractSetDecorator { 111 112 113 private final AbstractInputCheckedMapDecorator parent; 114 115 protected EntrySet(Set set, AbstractInputCheckedMapDecorator parent) { 116 super(set); 117 this.parent = parent; 118 } 119 120 public Iterator iterator() { 121 return new EntrySetIterator(collection.iterator(), parent); 122 } 123 124 public Object [] toArray() { 125 Object [] array = collection.toArray(); 126 for (int i = 0; i < array.length; i++) { 127 array[i] = new MapEntry((Map.Entry ) array[i], parent); 128 } 129 return array; 130 } 131 132 public Object [] toArray(Object array[]) { 133 Object [] result = array; 134 if (array.length > 0) { 135 result = (Object []) Array.newInstance(array.getClass().getComponentType(), 0); 138 } 139 result = collection.toArray(result); 140 for (int i = 0; i < result.length; i++) { 141 result[i] = new MapEntry((Map.Entry ) result[i], parent); 142 } 143 144 if (result.length > array.length) { 146 return result; 147 } 148 149 System.arraycopy(result, 0, array, 0, result.length); 151 if (array.length > result.length) { 152 array[result.length] = null; 153 } 154 return array; 155 } 156 } 157 158 161 static class EntrySetIterator extends AbstractIteratorDecorator { 162 163 164 private final AbstractInputCheckedMapDecorator parent; 165 166 protected EntrySetIterator(Iterator iterator, AbstractInputCheckedMapDecorator parent) { 167 super(iterator); 168 this.parent = parent; 169 } 170 171 public Object next() { 172 Map.Entry entry = (Map.Entry ) iterator.next(); 173 return new MapEntry(entry, parent); 174 } 175 } 176 177 180 static class MapEntry extends AbstractMapEntryDecorator { 181 182 183 private final AbstractInputCheckedMapDecorator parent; 184 185 protected MapEntry(Map.Entry entry, AbstractInputCheckedMapDecorator parent) { 186 super(entry); 187 this.parent = parent; 188 } 189 190 public Object setValue(Object value) { 191 value = parent.checkSetValue(value); 192 return entry.setValue(value); 193 } 194 } 195 196 } 197 | Popular Tags |