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