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.Iterator;
29import java.util.List;
30
31/**
32 * Type variable used to declare generics.
33 *
34 * @see JGenerifiable
35 * @author
36 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
37 */
38public final class JTypeVar extends JClass implements JDeclaration {
39
40    private final String name;
41
42    private JClass bound;
43
44    JTypeVar(JCodeModel owner, String _name) {
45        super(owner);
46        this.name = _name;
47    }
48
49    public String name() {
50        return name;
51    }
52
53    public String fullName() {
54        return name;
55    }
56
57    public JPackage _package() {
58        return null;
59    }
60
61    /**
62     * Adds a bound to this variable.
63     *
64     * @return  this
65     */
66    public JTypeVar bound( JClass c ) {
67        if(bound!=null)
68            throw new IllegalArgumentException("type variable has an existing class bound "+bound);
69        bound = c;
70        return this;
71    }
72
73    /**
74     * Returns the class bound of this variable.
75     *
76     * <p>
77     * If no bound is given, this method returns {@link Object}.
78     */
79    public JClass _extends() {
80        if(bound!=null)
81            return bound;
82        else
83            return owner().ref(Object.class);
84    }
85
86    /**
87     * Returns the interface bounds of this variable.
88     */
89    public Iterator<JClass> _implements() {
90        return bound._implements();
91    }
92
93    public boolean isInterface() {
94        return false;
95    }
96
97    public boolean isAbstract() {
98        return false;
99    }
100
101    /**
102     * Prints out the declaration of the variable.
103     */
104    public void declare(JFormatter f) {
105        f.id(name);
106        if(bound!=null)
107            f.p("extends").g(bound);
108    }
109
110
111    protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) {
112        for(int i=0;i<variables.length;i++)
113            if(variables[i]==this)
114                return bindings.get(i);
115        return this;
116    }
117
118    public void generate(JFormatter f) {
119        f.id(name);
120    }
121}
122