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.ArrayList;
20: import java.util.List;
21:
22: import org.springframework.beans.BeanMetadataElement;
23: import org.springframework.beans.Mergeable;
24:
25: /**
26: * Tag collection class used to hold managed List elements, which may
27: * include runtime bean references (to be resolved into bean objects).
28: *
29: * @author Rod Johnson
30: * @author Rob Harrop
31: * @author Juergen Hoeller
32: * @since 27.05.2003
33: */
34: public class ManagedList extends ArrayList implements Mergeable,
35: BeanMetadataElement {
36:
37: private Object source;
38:
39: private boolean mergeEnabled;
40:
41: public ManagedList() {
42: }
43:
44: public ManagedList(int initialCapacity) {
45: super (initialCapacity);
46: }
47:
48: /**
49: * Set the configuration source <code>Object</code> for this metadata element.
50: * <p>The exact type of the object will depend on the configuration mechanism used.
51: */
52: public void setSource(Object source) {
53: this .source = source;
54: }
55:
56: public Object getSource() {
57: return this .source;
58: }
59:
60: /**
61: * Set whether merging should be enabled for this collection,
62: * in case of a 'parent' collection value being present.
63: */
64: public void setMergeEnabled(boolean mergeEnabled) {
65: this .mergeEnabled = mergeEnabled;
66: }
67:
68: public boolean isMergeEnabled() {
69: return this .mergeEnabled;
70: }
71:
72: public Object merge(Object parent) {
73: if (!this .mergeEnabled) {
74: throw new IllegalStateException(
75: "Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
76: }
77: if (parent == null) {
78: return this ;
79: }
80: if (!(parent instanceof List)) {
81: throw new IllegalArgumentException(
82: "Cannot merge with object of type ["
83: + parent.getClass() + "]");
84: }
85: List merged = new ManagedList();
86: merged.addAll((List) parent);
87: merged.addAll(this);
88: return merged;
89: }
90:
91: }
|