1 16 17 package org.springframework.beans.factory.support; 18 19 import java.util.Collection ; 20 import java.util.Iterator ; 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 ManagedSet implements Set , Mergeable, BeanMetadataElement { 40 41 private final Set targetSet; 42 43 private Object source; 44 45 private boolean mergeEnabled; 46 47 48 public ManagedSet() { 49 this(16); 50 } 51 52 public ManagedSet(int initialCapacity) { 53 this.targetSet = CollectionFactory.createLinkedSetIfPossible(initialCapacity); 54 } 55 56 public ManagedSet(Set targetSet) { 57 this.targetSet = targetSet; 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 Set )) { 93 throw new IllegalArgumentException ("Cannot merge with object of type [" + parent.getClass() + "]"); 94 } 95 Set merged = new ManagedSet(); 96 merged.addAll((Set ) parent); 97 merged.addAll(this); 98 return merged; 99 } 100 101 102 public int size() { 103 return this.targetSet.size(); 104 } 105 106 public boolean isEmpty() { 107 return this.targetSet.isEmpty(); 108 } 109 110 public boolean contains(Object obj) { 111 return this.targetSet.contains(obj); 112 } 113 114 public Iterator iterator() { 115 return this.targetSet.iterator(); 116 } 117 118 public Object [] toArray() { 119 return this.targetSet.toArray(); 120 } 121 122 public Object [] toArray(Object [] arr) { 123 return this.targetSet.toArray(arr); 124 } 125 126 public boolean add(Object obj) { 127 return this.targetSet.add(obj); 128 } 129 130 public boolean remove(Object obj) { 131 return this.targetSet.remove(obj); 132 } 133 134 public boolean containsAll(Collection coll) { 135 return this.targetSet.containsAll(coll); 136 } 137 138 public boolean addAll(Collection coll) { 139 return this.targetSet.addAll(coll); 140 } 141 142 public boolean retainAll(Collection coll) { 143 return this.targetSet.retainAll(coll); 144 } 145 146 public boolean removeAll(Collection coll) { 147 return this.targetSet.removeAll(coll); 148 } 149 150 public void clear() { 151 this.targetSet.clear(); 152 } 153 154 public int hashCode() { 155 return this.targetSet.hashCode(); 156 } 157 158 public boolean equals(Object obj) { 159 return this.targetSet.equals(obj); 160 } 161 162 public String toString() { 163 return this.targetSet.toString(); 164 } 165 166 } 167 | Popular Tags |