View Javadoc
1   /*
2    * Copyright 2006 Werner Guttmann
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.javasource;
15  
16  /**
17   * JType sub-class for collections.
18   * 
19   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttman</a>
20   * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
21   * @since 1.0.4
22   */
23  public final class JCollectionType extends JComponentizedType {
24    // --------------------------------------------------------------------------
25  
26    /** Name of the actual collection instance to be used, e.g. java.util.ArrayList. */
27    private String _instanceName;
28  
29    /**
30     * Indicates whether (for generics) '? extends' should be emitted.
31     */
32    private boolean _useExtends;
33    // --------------------------------------------------------------------------
34  
35    /**
36     * Creates an instance of a collection type, of type 'collectionName'.
37     * 
38     * @param typeName Name of the collection type interface.
39     * @param componentType Component type.
40     * @param useJava50 True if Java 5.0 should be used.
41     */
42    public JCollectionType(final String typeName, final JType componentType,
43        final boolean useJava50) {
44      super(typeName, componentType, useJava50);
45      _useExtends = false;
46    }
47  
48    /**
49     * Creates an instance of a collection type, of type 'collectionName'.
50     * 
51     * @param typeName Name of the collection type interface.
52     * @param componentType Component type.
53     * @param useJava50 True if Java 5.0 should be used.
54     * @param useExtends True if '? extends' should be emitted for generics (Java 5.0 ff only).
55     */
56    public JCollectionType(final String typeName, final JType componentType, final boolean useJava50,
57        final boolean useExtends) {
58      super(typeName, componentType, useJava50);
59      _useExtends = useExtends;
60    }
61  
62    /**
63     * Creates an instance of a collection type, of type 'collectionName'.
64     * 
65     * @param typeName Name of the collection type interface.
66     * @param instanceName Name of the actual collection type instance.
67     * @param componentType Component type.
68     * @param useJava50 True if Java 5.0 should be used.
69     */
70    public JCollectionType(final String typeName, final String instanceName,
71        final JType componentType, final boolean useJava50) {
72      super(typeName, componentType, useJava50);
73      _instanceName = instanceName;
74    }
75  
76    // --------------------------------------------------------------------------
77  
78    /**
79     * Returns the instance name of this collection type.
80     * 
81     * @return The instance name of this collection type.
82     */
83    public String getInstanceName() {
84      if (_instanceName != null) {
85        if (isUseJava50()) {
86          if (getComponentType().isPrimitive()) {
87            JPrimitiveType primitive = (JPrimitiveType) getComponentType();
88            return _instanceName + '<' + primitive.getWrapperName() + '>';
89          }
90          if (_useExtends) {
91            return _instanceName + "<? extends " + getComponentType() + '>';
92          }
93          return _instanceName + '<' + getComponentType() + '>';
94        }
95  
96        return _instanceName;
97      }
98  
99      return toString();
100   }
101 
102   /**
103    * {@inheritDoc} <br/>
104    * Returns the String representation of this JType.
105    */
106   public String toString() {
107     if (isUseJava50()) {
108       if (getComponentType().isPrimitive()) {
109         JPrimitiveType primitive = (JPrimitiveType) getComponentType();
110         return getName() + '<' + primitive.getWrapperName() + '>';
111       }
112       if (_useExtends) {
113         return getName() + "<? extends " + getComponentType() + '>';
114       }
115       return getName() + '<' + getComponentType() + '>';
116     }
117 
118     return getName();
119   }
120 
121   // --------------------------------------------------------------------------
122 }