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
13
14 public class CollectionJ2MemberAndAccessorFactory extends CollectionMemberAndAccessorFactory {
15
16
17
18
19
20 public CollectionJ2MemberAndAccessorFactory(final JavaNaming naming) {
21 super(naming);
22 }
23
24
25
26
27
28
29 protected final void createCollectionIterationMethods(final CollectionInfo fieldInfo,
30 final JClass jClass,
31 final boolean useJava50) {
32 super.createCollectionIterationMethods(fieldInfo, jClass, useJava50);
33 this.createIteratorMethod(fieldInfo, jClass, useJava50);
34 }
35
36
37
38
39 protected final void createEnumerateMethod(final CollectionInfo fieldInfo,
40 final JClass jClass, final boolean useJava50) {
41 JMethod method = new JMethod("enumerate" + fieldInfo.getMethodSuffix(),
42 SGTypes.createEnumeration(fieldInfo.getContentType().getJType(), useJava50, true),
43 "an Enumeration over all possible elements of this collection");
44
45 JSourceCode sourceCode = method.getSourceCode();
46 sourceCode.add("return java.util.Collections.enumeration(this.");
47 sourceCode.append(fieldInfo.getName());
48 sourceCode.append(");");
49
50 jClass.addMethod(method);
51 }
52
53
54
55
56 protected final void createAddMethod(final CollectionInfo fieldInfo,
57 final JClass jClass) {
58 JMethod method = new JMethod(fieldInfo.getWriteMethodName());
59 method.addException(SGTypes.INDEX_OUT_OF_BOUNDS_EXCEPTION,
60 "if the index given is outside the bounds of the collection");
61 final JParameter parameter = new JParameter(fieldInfo.getContentType().getJType(),
62 fieldInfo.getContentName());
63 method.addParameter(parameter);
64
65 JSourceCode sourceCode = method.getSourceCode();
66 this.addMaxSizeCheck(fieldInfo, method.getName(), sourceCode);
67
68 sourceCode.add("this.");
69 sourceCode.append(fieldInfo.getName());
70 sourceCode.append(".add(");
71 sourceCode.append(fieldInfo.getContentType().createToJavaObjectCode(parameter.getName()));
72 sourceCode.append(");");
73
74 if (fieldInfo.isBound()) {
75 this.createBoundPropertyCode(fieldInfo, sourceCode);
76 }
77
78 jClass.addMethod(method);
79 }
80
81 }