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 ClassId {
022        @NotNull
023        public static ClassId topLevel(@NotNull FqName topLevelFqName) {
024            return new ClassId(topLevelFqName.parent(), topLevelFqName.shortName());
025        }
026    
027        private final FqName packageFqName;
028        private final FqNameUnsafe relativeClassName;
029    
030        public ClassId(@NotNull FqName packageFqName, @NotNull FqNameUnsafe relativeClassName) {
031            this.packageFqName = packageFqName;
032            assert !relativeClassName.isRoot() : "Class name must not be root. " + packageFqName;
033            this.relativeClassName = relativeClassName;
034        }
035    
036        public ClassId(@NotNull FqName packageFqName, @NotNull Name topLevelName) {
037            this(packageFqName, FqNameUnsafe.topLevel(topLevelName));
038        }
039    
040        @NotNull
041        public FqName getPackageFqName() {
042            return packageFqName;
043        }
044    
045        @NotNull
046        public FqNameUnsafe getRelativeClassName() {
047            return relativeClassName;
048        }
049    
050        @NotNull
051        public ClassId createNestedClassId(@NotNull Name name) {
052            return new ClassId(getPackageFqName(), relativeClassName.child(name));
053        }
054    
055        @NotNull
056        public ClassId getOuterClassId() {
057            return new ClassId(getPackageFqName(), relativeClassName.parent());
058        }
059    
060        public boolean isTopLevelClass() {
061            return relativeClassName.parent().isRoot();
062        }
063    
064        @NotNull
065        public FqNameUnsafe asSingleFqName() {
066            if (packageFqName.isRoot()) return relativeClassName;
067            return new FqNameUnsafe(packageFqName.asString() + "." + relativeClassName.asString());
068        }
069    
070        @Override
071        public boolean equals(Object o) {
072            if (this == o) return true;
073            if (o == null || getClass() != o.getClass()) return false;
074    
075            ClassId id = (ClassId) o;
076    
077            if (!packageFqName.equals(id.packageFqName)) return false;
078            if (!relativeClassName.equals(id.relativeClassName)) return false;
079    
080            return true;
081        }
082    
083        @Override
084        public int hashCode() {
085            int result = packageFqName.hashCode();
086            result = 31 * result + relativeClassName.hashCode();
087            return result;
088        }
089    
090        @Override
091        public String toString() {
092            if (packageFqName.isRoot()) return "/" + relativeClassName;
093            return packageFqName.toString().replace('.', '/') + "/" + relativeClassName;
094        }
095    }