1 16 17 package org.springframework.beans.factory.support; 18 19 import java.util.Collection ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import org.springframework.beans.BeanMetadataElement; 24 import org.springframework.beans.Mergeable; 25 import org.springframework.core.CollectionFactory; 26 27 39 public class ManagedMap implements Map , Mergeable, BeanMetadataElement { 40 41 private final Map targetMap; 42 43 private Object source; 44 45 private boolean mergeEnabled; 46 47 48 public ManagedMap() { 49 this(16); 50 } 51 52 public ManagedMap(int initialCapacity) { 53 this.targetMap = CollectionFactory.createLinkedMapIfPossible(initialCapacity); 54 } 55 56 public ManagedMap(Map targetMap) { 57 this.targetMap = targetMap; 58 } 59 60 61 65 public void setSource(Object source) { 66 this.source = source; 67 } 68 69 public Object getSource() { 70 return this.source; 71 } 72 73 77 public void setMergeEnabled(boolean mergeEnabled) { 78 this.mergeEnabled = mergeEnabled; 79 } 80 81 public boolean isMergeEnabled() { 82 return this.mergeEnabled; 83 } 84 85 public Object merge(Object parent) { 86 if (!this.mergeEnabled) { 87 throw new IllegalStateException ("Not allowed to merge when the 'mergeEnabled' property is set to 'false'"); 88 } 89 if (parent == null) { 90 return this; 91 } 92 if (!(parent instanceof Map )) { 93 throw new IllegalArgumentException ("Cannot merge with object of type [" + parent.getClass() + "]"); 94 } 95 Map merged = new ManagedMap(); 96 merged.putAll((Map ) parent); 97 merged.putAll(this); 98 return merged; 99 } 100 101 102 public int size() { 103 return this.targetMap.size(); 104 } 105 106 public boolean isEmpty() { 107 return this.targetMap.isEmpty(); 108 } 109 110 public boolean containsKey(Object key) { 111 return this.targetMap.containsKey(key); 112 } 113 114 public boolean containsValue(Object value) { 115 return this.targetMap.containsValue(value); 116 } 117 118 public Object get(Object key) { 119 return this.targetMap.get(key); 120 } 121 122 public Object put(Object key, Object value) { 123 return this.targetMap.put(key, value); 124 } 125 126 public Object remove(Object key) { 127 return this.targetMap.remove(key); 128 } 129 130 public void putAll(Map t) { 131 this.targetMap.putAll(t); 132 } 133 134 public void clear() { 135 this.targetMap.clear(); 136 } 137 138 public Set keySet() { 139 return this.targetMap.keySet(); 140 } 141 142 public Collection values() { 143 return this.targetMap.values(); 144 } 145 146 public Set entrySet() { 147 return this.targetMap.entrySet(); 148 } 149 150 public int hashCode() { 151 return this.targetMap.hashCode(); 152 } 153 154 public boolean equals(Object obj) { 155 return this.targetMap.equals(obj); 156 } 157 158 public String toString() { 159 return this.targetMap.toString(); 160 } 161 162 } 163 | Popular Tags |