View Javadoc
1   /*
2    * Copyright 2007 Ralf Joachim
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.builder.types;
15  
16  import org.exolab.castor.builder.SourceGeneratorConstants;
17  import org.exolab.castor.xml.schema.Facet;
18  import org.exolab.javasource.JClass;
19  import org.exolab.javasource.JCollectionType;
20  import org.exolab.javasource.JType;
21  
22  /**
23   * A base class for all list types.
24   * 
25   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
26   * @version $Revision: 6678 $ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
27   */
28  public abstract class XSListType extends XSType {
29    // --------------------------------------------------------------------------
30  
31    /** Content type of the collection. */
32    private final XSType _contentType;
33  
34    /** The JType represented by this XSType. */
35    private final JType _jType;
36  
37    /** Maximum size of this list. If set to -1 the maximum size is undefined. */
38    private int _maxSize = -1;
39  
40    /** Minimum size of this list. */
41    private int _minSize = 0;
42  
43    // --------------------------------------------------------------------------
44  
45    /**
46     * Creates an instance of this (abstract base) collection type.
47     * 
48     * @param colType Type of collection to use.
49     * @param contentType Type of the collection members.
50     * @param useJava50 If true, the collection will be generated using Java 5
51     */
52    public XSListType(final String colType, final XSType contentType, final boolean useJava50) {
53      super();
54  
55      _contentType = contentType;
56  
57      if (colType.equalsIgnoreCase(SourceGeneratorConstants.FIELD_INFO_ARRAY_LIST)) {
58        _jType = new JCollectionType("java.util.List", "java.util.ArrayList", contentType.getJType(),
59            useJava50);
60      } else if (colType.equalsIgnoreCase(SourceGeneratorConstants.FIELD_INFO_COLLECTION)) {
61        _jType = new JCollectionType("java.util.Collection", "java.util.LinkedList",
62            contentType.getJType(), useJava50);
63      } else if (colType.equalsIgnoreCase(SourceGeneratorConstants.FIELD_INFO_SET)) {
64        _jType = new JCollectionType("java.util.Set", "java.util.HashSet", contentType.getJType(),
65            useJava50);
66      } else if (colType.equalsIgnoreCase(SourceGeneratorConstants.FIELD_INFO_SORTED_SET)) {
67        _jType = new JCollectionType("java.util.SortedSet", "java.util.TreeSet",
68            contentType.getJType(), useJava50);
69      } else if (colType.equalsIgnoreCase(SourceGeneratorConstants.FIELD_INFO_VECTOR)) {
70        _jType = new JCollectionType("java.util.Vector", contentType.getJType(), useJava50);
71      } else if (colType.equalsIgnoreCase(SourceGeneratorConstants.FIELD_INFO_ODMG)) {
72        _jType = new JClass("org.odmg.DArray");
73      } else {
74        _jType = null;
75      }
76    }
77  
78  
79    // --------------------------------------------------------------------------
80  
81    /**
82     * Returns the type contained in the list.
83     * 
84     * @return The type contained in the list.
85     */
86    public final XSType getContentType() {
87      return _contentType;
88    }
89  
90    /**
91     * {@inheritDoc}
92     */
93    public JType getJType() {
94      return _jType;
95    }
96  
97    /**
98     * Returns the maximum allowed size for this list.
99     * 
100    * @return The maximum allowed size for this list.
101    */
102   public final int getMaximumSize() {
103     return _maxSize;
104   }
105 
106   /**
107    * Sets the maximum allowed size for this list.
108    * 
109    * @param size New maximum size for this list
110    */
111   public final void setMaximumSize(final int size) {
112     _maxSize = size;
113   }
114 
115   /**
116    * Returns the minimum allowed size for this list.
117    * 
118    * @return The minimum allowed size for this list.
119    */
120   public final int getMinimumSize() {
121     return _minSize;
122   }
123 
124   /**
125    * Sets the minimum allowed size for this list.
126    * 
127    * @param size New minimum size for this list
128    */
129   public final void setMinimumSize(final int size) {
130     _minSize = size;
131   }
132 
133   /**
134    * {@inheritDoc}
135    */
136   public boolean isCollection() {
137     return true;
138   }
139 
140   /**
141    * {@inheritDoc}
142    */
143   public String getName() {
144     return _jType.getName();
145   }
146 
147   /**
148    * {@inheritDoc}
149    */
150   public boolean isPrimitive() {
151     return false;
152   }
153 
154   /**
155    * {@inheritDoc}
156    */
157   public boolean isDateTime() {
158     return false;
159   }
160 
161   /**
162    * {@inheritDoc}
163    */
164   public String newInstanceCode() {
165     return "null;";
166   }
167 
168   /**
169    * {@inheritDoc}
170    */
171   public String createToJavaObjectCode(final String variableName) {
172     return variableName;
173   }
174 
175   /**
176    * {@inheritDoc}
177    */
178   public String createFromJavaObjectCode(final String variableName) {
179     return "(" + getJType().toString() + ") " + variableName;
180   }
181 
182   // --------------------------------------------------------------------------
183 
184   /**
185    * {@inheritDoc}
186    */
187   protected void setFacet(final Facet facet) {
188     // Not implemented
189   }
190 
191 
192 }