1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.codemodel.internal;
27
28import java.util.ArrayList;
29import java.util.List;
30
31/**
32 * Implementation of {@link JGenerifiable}.
33 *
34 * @author
35 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
36 */
37abstract 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)    f.p(',');
49                f.d(typeVariables.get(i));
50            }
51            f.p('>');
52        }
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.size()]);
77    }
78
79}
80