001    /*
002     * Copyright 2010-2015 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.kotlin.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            if (!isValidIdentifier(name)) {
056                throw new IllegalArgumentException("invalid identifier: " + name);
057            }
058            return new Name(name, false);
059        }
060    
061        public static boolean isValidIdentifier(@NotNull String name) {
062            return !name.isEmpty() && !name.startsWith("<") && !name.contains(".") && !name.contains("/");
063        }
064    
065        /** Must be validated by caller */
066        @NotNull
067        public static Name identifierNoValidate(@NotNull String name) {
068            return new Name(name, false);
069        }
070    
071        @NotNull
072        public static Name special(@NotNull String name) {
073            if (!name.startsWith("<")) {
074                throw new IllegalArgumentException("special name must start with '<': " + name);
075            }
076            return new Name(name, true);
077        }
078    
079        // TODO: wrong
080        @NotNull
081        public static Name guess(@NotNull String name) {
082            if (name.startsWith("<")) {
083                return special(name);
084            }
085            else {
086                return identifier(name);
087            }
088        }
089    
090        @Override
091        public String toString() {
092            return name;
093        }
094    
095        @Override
096        public boolean equals(Object o) {
097            if (this == o) return true;
098            if (!(o instanceof Name)) return false;
099    
100            Name name1 = (Name) o;
101    
102            if (special != name1.special) return false;
103            if (!name.equals(name1.name)) return false;
104    
105            return true;
106        }
107    
108        @Override
109        public int hashCode() {
110            int result = name.hashCode();
111            result = 31 * result + (special ? 1 : 0);
112            return result;
113        }
114    }