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.descriptors.impl;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.*;
022    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
023    import org.jetbrains.jet.lang.resolve.name.FqName;
024    import org.jetbrains.jet.lang.resolve.scopes.ChainedScope;
025    import org.jetbrains.jet.lang.resolve.scopes.JetScope;
026    import org.jetbrains.jet.lang.types.TypeSubstitutor;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    public class PackageViewDescriptorImpl extends DeclarationDescriptorImpl implements PackageViewDescriptor {
032        private final ModuleDescriptor module;
033        private final FqName fqName;
034        private final JetScope memberScope;
035    
036        public PackageViewDescriptorImpl(
037                @NotNull ModuleDescriptor module,
038                @NotNull FqName fqName,
039                @NotNull List<PackageFragmentDescriptor> fragments
040        ) {
041            super(Annotations.EMPTY, fqName.shortNameOrSpecial());
042            this.module = module;
043            this.fqName = fqName;
044    
045            List<JetScope> scopes = new ArrayList<JetScope>(fragments.size() + 1);
046            assert !fragments.isEmpty() : fqName + " in " + module;
047            for (PackageFragmentDescriptor fragment : fragments) {
048                scopes.add(fragment.getMemberScope());
049            }
050            scopes.add(new SubpackagesScope(this));
051    
052            memberScope = new ChainedScope(this, "package view scope for " + fqName + " in " + module.getName(),
053                                           scopes.toArray(new JetScope[scopes.size()]));
054        }
055    
056        @Nullable
057        @Override
058        public PackageViewDescriptor getContainingDeclaration() {
059            return fqName.isRoot() ? null : module.getPackage(fqName.parent());
060        }
061    
062        @Nullable
063        @Override
064        public DeclarationDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
065            return this;
066        }
067    
068        @Override
069        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
070            return visitor.visitPackageViewDescriptor(this, data);
071        }
072    
073        @NotNull
074        @Override
075        public FqName getFqName() {
076            return fqName;
077        }
078    
079        @NotNull
080        @Override
081        public JetScope getMemberScope() {
082            return memberScope;
083        }
084    
085        @Override
086        @NotNull
087        public ModuleDescriptor getModule() {
088            return module;
089        }
090    
091        public boolean equals(Object o) {
092            if (this == o) return true;
093            if (o == null || getClass() != o.getClass()) return false;
094    
095            PackageViewDescriptorImpl that = (PackageViewDescriptorImpl) o;
096    
097            if (!fqName.equals(that.fqName)) return false;
098            if (!module.equals(that.module)) return false;
099    
100            return true;
101        }
102    
103    
104        @Override
105        public int hashCode() {
106            int result = module.hashCode();
107            result = 31 * result + fqName.hashCode();
108            return result;
109        }
110    }