01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.codemodel;
21:
22: import java.util.ArrayList;
23: import java.util.List;
24:
25: /**
26: * Implementation of {@link JGenerifiable}.
27: *
28: * @author
29: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
30: */
31: abstract class JGenerifiableImpl implements JGenerifiable, JDeclaration {
32:
33: /** Lazily created list of {@link JTypeVar}s. */
34: private List<JTypeVar> typeVariables = null;
35:
36: protected abstract JCodeModel owner();
37:
38: public void declare(JFormatter f) {
39: if (typeVariables != null) {
40: f.p('<');
41: for (int i = 0; i < typeVariables.size(); i++) {
42: if (i != 0)
43: f.p(',');
44: f.d(typeVariables.get(i));
45: }
46: f.p('>');
47: }
48: }
49:
50: public JTypeVar generify(String name) {
51: JTypeVar v = new JTypeVar(owner(), name);
52: if (typeVariables == null)
53: typeVariables = new ArrayList<JTypeVar>(3);
54: typeVariables.add(v);
55: return v;
56: }
57:
58: public JTypeVar generify(String name, Class bound) {
59: return generify(name, owner().ref(bound));
60: }
61:
62: public JTypeVar generify(String name, JClass bound) {
63: return generify(name).bound(bound);
64: }
65:
66: public JTypeVar[] typeParams() {
67: if (typeVariables == null)
68: return JTypeVar.EMPTY_ARRAY;
69: else
70: return typeVariables.toArray(new JTypeVar[typeVariables
71: .size()]);
72: }
73:
74: }
|