View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt.io.read;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  
22  /**  
23    * <p>Chain implementation that's backed by a list.
24    * This is the default implementation used by Betwixt.
25    * </p><p>
26    * <strong>Note</strong> this implementation is <em>not</em>
27    * intended to allow multiple threads of execution to perform
28    * modification operations concurrently with traversal of the chain.
29    * Users who require this behaviour are advised to create their own implementation.
30    * </p>
31    *
32    * @author Robert Burrell Donkin
33    * @since 0.5
34    */
35  public class BeanCreationList extends BeanCreationChain {
36      
37  //-------------------------------------------------------- Class Methods
38  
39      /** 
40       * Creates the default <code>BeanCreationChain</code> used when reading beans.
41       * @return a <code>BeanCreationList</code> with the default creators loader in order, not null
42       */
43      public static final BeanCreationList createStandardChain() {
44          BeanCreationList chain = new BeanCreationList();
45          chain.addBeanCreator( ChainedBeanCreatorFactory.createIDREFBeanCreator() );
46          chain.addBeanCreator( ChainedBeanCreatorFactory.createDerivedBeanCreator() );
47          chain.addBeanCreator( ChainedBeanCreatorFactory.createElementTypeBeanCreator() );
48          return chain;
49      }
50      
51  
52      
53  //-------------------------------------------------------- Attributes
54      /** The list backing this chain */
55      private ArrayList beanCreators = new ArrayList();
56      
57  //-------------------------------------------------------- Methods
58      
59      /**
60        * Creates an Object based on the given element mapping and read context.
61        * Delegates to chain.
62        *
63        * @param elementMapping the element mapping details
64        * @param readContext create against this context 
65        * @return the created bean, possibly null
66        */
67      public Object create( ElementMapping elementMapping, ReadContext readContext ) {
68          ChainWorker worker = new ChainWorker();
69          return worker.create( elementMapping, readContext );
70      }
71      
72  //-------------------------------------------------------- Properties
73      
74      /**
75        * Gets the number of BeanCreators in the wrapped chain.
76        * @return the number of <code>ChainedBeanCreator</code>'s in the current chain
77        */
78      public int getSize() {
79          return beanCreators.size();
80      }
81      
82      /**
83        * Inserts a <code>BeanCreator</code> at the given position in the chain.
84        * Shifts the object currently in that position - and any subsequent elements -
85        * to the right.
86        *
87        * @param index index at which the creator should be inserted
88        * @param beanCreator the <code>BeanCreator</code> to be inserted, not null
89        * @throws IndexOutOfBoundsException if the index is out of the range 
90        * <code>(index < 0 || index > getSize())
91        */
92      public void insertBeanCreator(
93                                  int index, 
94                                  ChainedBeanCreator beanCreator ) 
95                                      throws IndexOutOfBoundsException {
96          beanCreators.add( index, beanCreator );
97      }
98      
99      /**
100       * Adds a <code>BeanCreator</code> to the end of the chain.
101       * @param beanCreator the <code>BeanCreator</code> to be inserted, not null
102       */
103     public void addBeanCreator( ChainedBeanCreator beanCreator ) {
104         beanCreators.add( beanCreator );
105     }
106     
107     /** 
108       * Clears the creator chain.
109       */
110     public void clearBeanCreators() {
111         beanCreators.clear();
112     }
113      
114     /** Worker class walks a chain */
115     private class ChainWorker extends BeanCreationChain {
116         /** Iterator for the creator list */
117         private Iterator iterator;
118         /** Creates the iterator */
119         ChainWorker() {
120             iterator = beanCreators.iterator();
121         }
122     
123         /**
124           * @see BeanCreationChain#create
125           */
126         public Object create( ElementMapping elementMapping, ReadContext readContext ) {
127             if ( iterator.hasNext() ) {
128                 ChainedBeanCreator beanCreator = (ChainedBeanCreator) iterator.next();
129                 return beanCreator.create( elementMapping, readContext, this );
130             }
131             
132             return null;
133         }
134     }
135 }