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