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
017package org.jetbrains.jet.lang.resolve.scopes;
018
019import com.google.common.base.Predicate;
020import com.google.common.collect.Collections2;
021import org.jetbrains.annotations.NotNull;
022import org.jetbrains.annotations.Nullable;
023import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
024import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
025import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
026import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
027import org.jetbrains.jet.lang.resolve.name.LabelName;
028import org.jetbrains.jet.lang.resolve.name.Name;
029
030import java.util.Collection;
031import java.util.Collections;
032import java.util.List;
033
034public class InnerClassesScopeWrapper extends AbstractScopeAdapter {
035    private final JetScope actualScope;
036
037    public InnerClassesScopeWrapper(JetScope actualScope) {
038        this.actualScope = actualScope;
039    }
040
041    @NotNull
042    @Override
043    protected JetScope getWorkerScope() {
044        return actualScope;
045    }
046
047    @Override
048    public ClassifierDescriptor getClassifier(@NotNull Name name) {
049        ClassifierDescriptor classifier = actualScope.getClassifier(name);
050        if (isClass(classifier)) return classifier;
051        return null;
052    }
053
054    @NotNull
055    @Override
056    public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
057        Collection<DeclarationDescriptor> declarationsByLabel = actualScope.getDeclarationsByLabel(labelName);
058        return Collections2.filter(declarationsByLabel, new Predicate<DeclarationDescriptor>() {
059            @Override
060            public boolean apply(@Nullable DeclarationDescriptor descriptor) {
061                return isClass(descriptor);
062            }
063        });
064    }
065
066    @NotNull
067    @Override
068    public Collection<DeclarationDescriptor> getAllDescriptors() {
069        Collection<DeclarationDescriptor> allDescriptors = actualScope.getAllDescriptors();
070        return Collections2.filter(allDescriptors, new Predicate<DeclarationDescriptor>() {
071            @Override
072            public boolean apply(@Nullable DeclarationDescriptor descriptor) {
073                return isClass(descriptor);
074            }
075        });
076    }
077
078    @NotNull
079    @Override
080    public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
081        return Collections.emptyList();
082    }
083
084    @Override
085    public String toString() {
086        return "Classes from " + actualScope;
087    }
088
089    private static boolean isClass(DeclarationDescriptor descriptor) {
090        return descriptor instanceof ClassDescriptor && !((ClassDescriptor) descriptor).getKind().isObject();
091    }
092}