01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.codemodel.internal;
27:
28: import java.util.ArrayList;
29: import java.util.List;
30:
31: /**
32: * Implementation of {@link JGenerifiable}.
33: *
34: * @author
35: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
36: */
37: abstract class JGenerifiableImpl implements JGenerifiable, JDeclaration {
38:
39: /** Lazily created list of {@link JTypeVar}s. */
40: private List<JTypeVar> typeVariables = null;
41:
42: protected abstract JCodeModel owner();
43:
44: public void declare(JFormatter f) {
45: if (typeVariables != null) {
46: f.p('<');
47: for (int i = 0; i < typeVariables.size(); i++) {
48: if (i != 0)
49: f.p(',');
50: f.d(typeVariables.get(i));
51: }
52: f.p('>');
53: }
54: }
55:
56: public JTypeVar generify(String name) {
57: JTypeVar v = new JTypeVar(owner(), name);
58: if (typeVariables == null)
59: typeVariables = new ArrayList<JTypeVar>(3);
60: typeVariables.add(v);
61: return v;
62: }
63:
64: public JTypeVar generify(String name, Class bound) {
65: return generify(name, owner().ref(bound));
66: }
67:
68: public JTypeVar generify(String name, JClass bound) {
69: return generify(name).bound(bound);
70: }
71:
72: public JTypeVar[] typeParams() {
73: if (typeVariables == null)
74: return JTypeVar.EMPTY_ARRAY;
75: else
76: return typeVariables.toArray(new JTypeVar[typeVariables
77: .size()]);
78: }
79:
80: }
|