001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.resolve.name;
018    
019    import org.jetbrains.annotations.NotNull;
020    
021    public final class Name implements Comparable<Name> {
022        @NotNull
023        private final String name;
024        private final boolean special;
025    
026        private Name(@NotNull String name, boolean special) {
027            this.name = name;
028            this.special = special;
029        }
030    
031        @NotNull
032        public String asString() {
033            return name;
034        }
035    
036        @NotNull
037        public String getIdentifier() {
038            if (special) {
039                throw new IllegalStateException("not identifier: " + this);
040            }
041            return asString();
042        }
043    
044        public boolean isSpecial() {
045            return special;
046        }
047    
048        @Override
049        public int compareTo(Name that) {
050            return this.name.compareTo(that.name);
051        }
052    
053        @NotNull
054        public static Name identifier(@NotNull String name) {
055            NameUtils.requireIdentifier(name);
056            return new Name(name, false);
057        }
058    
059        /** Must be validated by caller */
060        @NotNull
061        public static Name identifierNoValidate(@NotNull String name) {
062            return new Name(name, false);
063        }
064    
065        @NotNull
066        public static Name special(@NotNull String name) {
067            if (!name.startsWith("<")) {
068                throw new IllegalArgumentException("special name must start with '<': " + name);
069            }
070            return new Name(name, true);
071        }
072    
073        // TODO: wrong
074        @NotNull
075        public static Name guess(@NotNull String name) {
076            if (name.startsWith("<")) {
077                return special(name);
078            }
079            else {
080                return identifier(name);
081            }
082        }
083    
084        @Override
085        public String toString() {
086            return name;
087        }
088    
089        @Override
090        public boolean equals(Object o) {
091            if (this == o) return true;
092            if (!(o instanceof Name)) return false;
093    
094            Name name1 = (Name) o;
095    
096            if (special != name1.special) return false;
097            if (!name.equals(name1.name)) return false;
098    
099            return true;
100        }
101    
102        @Override
103        public int hashCode() {
104            int result = name.hashCode();
105            result = 31 * result + (special ? 1 : 0);
106            return result;
107        }
108    }