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