001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.support;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.Set;
022:
023: import org.springframework.beans.BeanMetadataElement;
024: import org.springframework.beans.Mergeable;
025: import org.springframework.core.CollectionFactory;
026:
027: /**
028: * Tag collection class used to hold managed Set values, which may
029: * include runtime bean references (to be resolved into bean objects).
030: *
031: * <p>Wraps a target Set, which will be a linked set if possible
032: * (that is, if running on JDK 1.4 or if Commons Collections 3.x is available).
033: *
034: * @author Juergen Hoeller
035: * @author Rob Harrop
036: * @since 21.01.2004
037: * @see org.springframework.core.CollectionFactory#createLinkedSetIfPossible
038: */
039: public class ManagedSet implements Set, Mergeable, BeanMetadataElement {
040:
041: private final Set targetSet;
042:
043: private Object source;
044:
045: private boolean mergeEnabled;
046:
047: public ManagedSet() {
048: this (16);
049: }
050:
051: public ManagedSet(int initialCapacity) {
052: this .targetSet = CollectionFactory
053: .createLinkedSetIfPossible(initialCapacity);
054: }
055:
056: public ManagedSet(Set targetSet) {
057: this .targetSet = targetSet;
058: }
059:
060: /**
061: * Set the configuration source <code>Object</code> for this metadata element.
062: * <p>The exact type of the object will depend on the configuration mechanism used.
063: */
064: public void setSource(Object source) {
065: this .source = source;
066: }
067:
068: public Object getSource() {
069: return this .source;
070: }
071:
072: /**
073: * Set whether merging should be enabled for this collection,
074: * in case of a 'parent' collection value being present.
075: */
076: public void setMergeEnabled(boolean mergeEnabled) {
077: this .mergeEnabled = mergeEnabled;
078: }
079:
080: public boolean isMergeEnabled() {
081: return this .mergeEnabled;
082: }
083:
084: public Object merge(Object parent) {
085: if (!this .mergeEnabled) {
086: throw new IllegalStateException(
087: "Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
088: }
089: if (parent == null) {
090: return this ;
091: }
092: if (!(parent instanceof Set)) {
093: throw new IllegalArgumentException(
094: "Cannot merge with object of type ["
095: + parent.getClass() + "]");
096: }
097: Set merged = new ManagedSet();
098: merged.addAll((Set) parent);
099: merged.addAll(this );
100: return merged;
101: }
102:
103: public int size() {
104: return this .targetSet.size();
105: }
106:
107: public boolean isEmpty() {
108: return this .targetSet.isEmpty();
109: }
110:
111: public boolean contains(Object obj) {
112: return this .targetSet.contains(obj);
113: }
114:
115: public Iterator iterator() {
116: return this .targetSet.iterator();
117: }
118:
119: public Object[] toArray() {
120: return this .targetSet.toArray();
121: }
122:
123: public Object[] toArray(Object[] arr) {
124: return this .targetSet.toArray(arr);
125: }
126:
127: public boolean add(Object obj) {
128: return this .targetSet.add(obj);
129: }
130:
131: public boolean remove(Object obj) {
132: return this .targetSet.remove(obj);
133: }
134:
135: public boolean containsAll(Collection coll) {
136: return this .targetSet.containsAll(coll);
137: }
138:
139: public boolean addAll(Collection coll) {
140: return this .targetSet.addAll(coll);
141: }
142:
143: public boolean retainAll(Collection coll) {
144: return this .targetSet.retainAll(coll);
145: }
146:
147: public boolean removeAll(Collection coll) {
148: return this .targetSet.removeAll(coll);
149: }
150:
151: public void clear() {
152: this .targetSet.clear();
153: }
154:
155: public int hashCode() {
156: return this .targetSet.hashCode();
157: }
158:
159: public boolean equals(Object obj) {
160: return this .targetSet.equals(obj);
161: }
162:
163: public String toString() {
164: return this.targetSet.toString();
165: }
166:
167: }
|