View Javadoc
1   package org.exolab.castor.builder.factory;
2   
3   import org.castor.xml.JavaNaming;
4   import org.exolab.castor.builder.SGTypes;
5   import org.exolab.castor.builder.info.CollectionInfo;
6   import org.exolab.javasource.JClass;
7   import org.exolab.javasource.JMethod;
8   import org.exolab.javasource.JParameter;
9   import org.exolab.javasource.JSourceCode;
10  
11  /**
12   * The Factory for Java2 Collections.
13   */
14  public class CollectionJ2MemberAndAccessorFactory extends CollectionMemberAndAccessorFactory {
15  
16    /**
17     * Creates a new CollectionJ2MemberAndAccessorFactory.
18     * 
19     * @param naming the javaNaming to use
20     */
21    public CollectionJ2MemberAndAccessorFactory(final JavaNaming naming) {
22      super(naming);
23    }
24  
25    /**
26     * To the Java-1 collection iterators, we add the Java-2 Iterator.
27     */
28    @Override
29    protected final void createCollectionIterationMethods(final CollectionInfo fieldInfo,
30        final JClass jClass, final boolean useJava50) {
31      super.createCollectionIterationMethods(fieldInfo, jClass, useJava50);
32      this.createIteratorMethod(fieldInfo, jClass, useJava50);
33    }
34  
35    @Override
36    protected final void createEnumerateMethod(final CollectionInfo fieldInfo, final JClass jClass,
37        final boolean useJava50) {
38      JMethod method = new JMethod("enumerate" + fieldInfo.getMethodSuffix(),
39          SGTypes.createEnumeration(fieldInfo.getContentType().getJType(), useJava50, true),
40          "an Enumeration over all possible elements of this collection");
41  
42      JSourceCode sourceCode = method.getSourceCode();
43      sourceCode.add("return java.util.Collections.enumeration(this.");
44      sourceCode.append(fieldInfo.getName());
45      sourceCode.append(");");
46  
47      jClass.addMethod(method);
48    }
49  
50    @Override
51    protected final void createAddMethod(final CollectionInfo fieldInfo, final JClass jClass) {
52      JMethod method = new JMethod(fieldInfo.getWriteMethodName());
53      method.addException(SGTypes.INDEX_OUT_OF_BOUNDS_EXCEPTION,
54          "if the index given is outside the bounds of the collection");
55      final JParameter parameter =
56          new JParameter(fieldInfo.getContentType().getJType(), fieldInfo.getContentName());
57      method.addParameter(parameter);
58  
59      JSourceCode sourceCode = method.getSourceCode();
60      this.addMaxSizeCheck(fieldInfo, method.getName(), sourceCode);
61  
62      sourceCode.add("this.");
63      sourceCode.append(fieldInfo.getName());
64      sourceCode.append(".add(");
65      sourceCode.append(fieldInfo.getContentType().createToJavaObjectCode(parameter.getName()));
66      sourceCode.append(");");
67  
68      if (fieldInfo.isBound()) {
69        this.createBoundPropertyCode(fieldInfo, sourceCode);
70      }
71  
72      jClass.addMethod(method);
73    }
74  
75  }