01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.beans.factory.support;
18:
19: import java.util.Collection;
20: import java.util.Iterator;
21: import java.util.LinkedHashSet;
22: import java.util.Set;
23:
24: import org.springframework.beans.BeanMetadataElement;
25: import org.springframework.beans.Mergeable;
26:
27: /**
28: * Tag collection class used to hold managed Set values, which may
29: * include runtime bean references (to be resolved into bean objects).
30: *
31: * @author Juergen Hoeller
32: * @author Rob Harrop
33: * @since 21.01.2004
34: */
35: public class ManagedSet extends LinkedHashSet implements Mergeable,
36: BeanMetadataElement {
37:
38: private Object source;
39:
40: private boolean mergeEnabled;
41:
42: public ManagedSet() {
43: }
44:
45: public ManagedSet(int initialCapacity) {
46: super (initialCapacity);
47: }
48:
49: /**
50: * Set the configuration source <code>Object</code> for this metadata element.
51: * <p>The exact type of the object will depend on the configuration mechanism used.
52: */
53: public void setSource(Object source) {
54: this .source = source;
55: }
56:
57: public Object getSource() {
58: return this .source;
59: }
60:
61: /**
62: * Set whether merging should be enabled for this collection,
63: * in case of a 'parent' collection value being present.
64: */
65: public void setMergeEnabled(boolean mergeEnabled) {
66: this .mergeEnabled = mergeEnabled;
67: }
68:
69: public boolean isMergeEnabled() {
70: return this .mergeEnabled;
71: }
72:
73: public Object merge(Object parent) {
74: if (!this .mergeEnabled) {
75: throw new IllegalStateException(
76: "Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
77: }
78: if (parent == null) {
79: return this ;
80: }
81: if (!(parent instanceof Set)) {
82: throw new IllegalArgumentException(
83: "Cannot merge with object of type ["
84: + parent.getClass() + "]");
85: }
86: Set merged = new ManagedSet();
87: merged.addAll((Set) parent);
88: merged.addAll(this);
89: return merged;
90: }
91:
92: }
|