001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.io.read;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021:
022: /**
023: * <p>Chain implementation that's backed by a list.
024: * This is the default implementation used by Betwixt.
025: * </p><p>
026: * <strong>Note</strong> this implementation is <em>not</em>
027: * intended to allow multiple threads of execution to perform
028: * modification operations concurrently with traversal of the chain.
029: * Users who require this behaviour are advised to create their own implementation.
030: * </p>
031: *
032: * @author Robert Burrell Donkin
033: * @since 0.5
034: */
035: public class BeanCreationList extends BeanCreationChain {
036:
037: //-------------------------------------------------------- Class Methods
038:
039: /**
040: * Creates the default <code>BeanCreationChain</code> used when reading beans.
041: * @return a <code>BeanCreationList</code> with the default creators loader in order, not null
042: */
043: public static final BeanCreationList createStandardChain() {
044: BeanCreationList chain = new BeanCreationList();
045: chain.addBeanCreator(ChainedBeanCreatorFactory
046: .createIDREFBeanCreator());
047: chain.addBeanCreator(ChainedBeanCreatorFactory
048: .createDerivedBeanCreator());
049: chain.addBeanCreator(ChainedBeanCreatorFactory
050: .createElementTypeBeanCreator());
051: return chain;
052: }
053:
054: //-------------------------------------------------------- Attributes
055: /** The list backing this chain */
056: private ArrayList beanCreators = new ArrayList();
057:
058: //-------------------------------------------------------- Methods
059:
060: /**
061: * Creates an Object based on the given element mapping and read context.
062: * Delegates to chain.
063: *
064: * @param elementMapping the element mapping details
065: * @param readContext create against this context
066: * @return the created bean, possibly null
067: */
068: public Object create(ElementMapping elementMapping,
069: ReadContext readContext) {
070: ChainWorker worker = new ChainWorker();
071: return worker.create(elementMapping, readContext);
072: }
073:
074: //-------------------------------------------------------- Properties
075:
076: /**
077: * Gets the number of BeanCreators in the wrapped chain.
078: * @return the number of <code>ChainedBeanCreator</code>'s in the current chain
079: */
080: public int getSize() {
081: return beanCreators.size();
082: }
083:
084: /**
085: * Inserts a <code>BeanCreator</code> at the given position in the chain.
086: * Shifts the object currently in that position - and any subsequent elements -
087: * to the right.
088: *
089: * @param index index at which the creator should be inserted
090: * @param beanCreator the <code>BeanCreator</code> to be inserted, not null
091: * @throws IndexOutOfBoundsException if the index is out of the range
092: * <code>(index < 0 || index > getSize())
093: */
094: public void insertBeanCreator(int index,
095: ChainedBeanCreator beanCreator)
096: throws IndexOutOfBoundsException {
097: beanCreators.add(index, beanCreator);
098: }
099:
100: /**
101: * Adds a <code>BeanCreator</code> to the end of the chain.
102: * @param beanCreator the <code>BeanCreator</code> to be inserted, not null
103: */
104: public void addBeanCreator(ChainedBeanCreator beanCreator) {
105: beanCreators.add(beanCreator);
106: }
107:
108: /**
109: * Clears the creator chain.
110: */
111: public void clearBeanCreators() {
112: beanCreators.clear();
113: }
114:
115: /** Worker class walks a chain */
116: private class ChainWorker extends BeanCreationChain {
117: /** Iterator for the creator list */
118: private Iterator iterator;
119:
120: /** Creates the iterator */
121: ChainWorker() {
122: iterator = beanCreators.iterator();
123: }
124:
125: /**
126: * @see BeanCreationChain#create
127: */
128: public Object create(ElementMapping elementMapping,
129: ReadContext readContext) {
130: if (iterator.hasNext()) {
131: ChainedBeanCreator beanCreator = (ChainedBeanCreator) iterator
132: .next();
133: return beanCreator.create(elementMapping, readContext,
134: this);
135: }
136:
137: return null;
138: }
139: }
140: }
|