summaryrefslogtreecommitdiff
path: root/sources/scalac/util/Name.java
blob: af088d6f79aef21beba25660b72879646f303273 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.util;

/** This class implements the common part of TermName and TypeName. */
public abstract class Name {

    //########################################################################
    // Public Fields

    /** The unique identifier */
    public final int index;

    //########################################################################
    // Private Fields

    /** The related term name */
    private final TermName term;

    /** The related type name (null if not yet created) */
    private TypeName type;

    //########################################################################
    // Protected Constructors

    /** Initializes this instance. */
    protected Name(int index, TermName term) {
        this.index = index;
        this.term = term == null ? (TermName)this : term;
        this.type = term == null ? null : (TypeName)this;
    }

    //########################################################################
    // Public Factories

    /** Returns the term name with given ASCII representation. */
    public static TermName fromAscii(byte[] bytes, int start, int count) {
        return TermName.fromAscii(bytes, start, count);
    }

    /** Returns the term name with given string representation. */
    public static TermName fromString(String string) {
        return TermName.fromString(string);
    }

    //########################################################################
    // Public Methods

    /** Is this name a variable identifier? */
    public final boolean isVariable() {
        char first = charAt(0);
        return (('a' <= first && first <= 'z') || first == '_')
            && this != Names.false_
            && this != Names.true_
            && this != Names.null_;
    }

    /** Is this name a term name? */
    public final boolean isTermName() {
        return this == term;
    }

    /** Is this name a type name? */
    public final boolean isTypeName() {
        return this == type;
    }

    /** Returns the term name with the same representation. */
    public final TermName toTermName() {
        return term;
    }

    /** Returns the type name with the same representation. */
    public final TypeName toTypeName() {
        return type != null ? type : (type = new TypeName(term));
    }

    /** Returns the result of "toString().length()". */
    public final int length() {
        return toString().length();
    }

    /** Returns the result of "toString().charAt(index)". */
    public final char charAt(int index) {
        return toString().charAt(index);
    }

    /** Returns the result of "toString().indexOf(ch)". */
    public final int indexOf(char ch) {
        return toString().indexOf(ch);
    }

    /** Returns the result of "toString().indexOf(ch, start)". */
    public final int indexOf(char ch, int start) {
        return toString().indexOf(ch, start);
    }

    /** Returns the result of "toString().lastIndexOf(ch)". */
    public final int lastIndexOf(char ch) {
        return toString().lastIndexOf(ch);
    }

    /** Returns the result of "toString().lastIndexOf(ch, start)". */
    public final int lastIndexOf(char ch, int start) {
        return toString().lastIndexOf(ch, start);
    }

    /** Returns the hash code of this name. */
    public final int hashCode() {
        return index;
    }

    /** Returns the string representation of this name. */
    public String toString() {
        return term.toString();
    }

    /**
     * Returns the ASCII representation of this name. The returned
     * array is not a copy. Therefore, it is forbidden to modify it.
     */
    public byte[] toAsciiUnsafe() {
        return term.toAsciiUnsafe();
    }

    //########################################################################
}