All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.jet.lang.resolve.java.structure.impl.JavaElementCollectionFromPsiArrayUtil Maven / Gradle / Ivy

/*
 * Copyright 2010-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.jet.lang.resolve.java.structure.impl;

import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import kotlin.Function1;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.structure.*;
import org.jetbrains.jet.lang.resolve.name.Name;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class JavaElementCollectionFromPsiArrayUtil {
    private JavaElementCollectionFromPsiArrayUtil() {
    }

    private interface Factory {
        @NotNull
        Java create(@NotNull Psi psi);
    }

    private static class Factories {
        private static final Factory CLASSES = new Factory() {
            @NotNull
            @Override
            public JavaClass create(@NotNull PsiClass psiClass) {
                return new JavaClassImpl(psiClass);
            }
        };

        private static final Factory METHODS = new Factory() {
            @NotNull
            @Override
            public JavaMethod create(@NotNull PsiMethod psiMethod) {
                return new JavaMethodImpl(psiMethod);
            }
        };

        private static final Factory CONSTRUCTORS = new Factory() {
            @NotNull
            @Override
            public JavaConstructor create(@NotNull PsiMethod psiMethod) {
                return new JavaConstructorImpl(psiMethod);
            }
        };

        private static final Factory FIELDS = new Factory() {
            @NotNull
            @Override
            public JavaField create(@NotNull PsiField psiField) {
                return new JavaFieldImpl(psiField);
            }
        };

        private static final Factory VALUE_PARAMETERS = new Factory() {
            @NotNull
            @Override
            public JavaValueParameter create(@NotNull PsiParameter psiParameter) {
                return new JavaValueParameterImpl(psiParameter);
            }
        };

        private static final Factory TYPE_PARAMETERS =
                new Factory() {
            @NotNull
            @Override
            public JavaTypeParameter create(@NotNull PsiTypeParameter psiTypeParameter) {
                return new JavaTypeParameterImpl(psiTypeParameter);
            }
        };

        private static final Factory TYPES = new Factory() {
            @NotNull
            @Override
            public JavaType create(@NotNull PsiType psiType) {
                return JavaTypeImpl.create(psiType);
            }
        };

        private static final Factory CLASSIFIER_TYPES = new Factory() {
            @NotNull
            @Override
            public JavaClassifierType create(@NotNull PsiClassType psiClassType) {
                return new JavaClassifierTypeImpl(psiClassType);
            }
        };

        private static final Factory ANNOTATIONS = new Factory() {
            @NotNull
            @Override
            public JavaAnnotation create(@NotNull PsiAnnotation psiAnnotation) {
                return new JavaAnnotationImpl(psiAnnotation);
            }
        };

        private static final Factory NAMED_ANNOTATION_ARGUMENTS =
                new Factory() {
            @NotNull
            @Override
            public JavaAnnotationArgument create(@NotNull PsiNameValuePair psiNameValuePair) {
                String name = psiNameValuePair.getName();
                PsiAnnotationMemberValue value = psiNameValuePair.getValue();
                assert value != null : "Annotation argument value cannot be null: " + name;
                return JavaAnnotationArgumentImpl.OBJECT$.create(value, name == null ? null : Name.identifier(name));
            }
        };
    }

    @NotNull
    private static  List convert(@NotNull Psi[] elements, @NotNull Factory factory) {
        if (elements.length == 0) return Collections.emptyList();
        List result = new ArrayList(elements.length);
        for (Psi element : elements) {
            result.add(factory.create(element));
        }
        return result;
    }

    @NotNull
    private static  List convert(@NotNull Iterable elements, @NotNull final Factory factory) {
        if (!elements.iterator().hasNext()) return Collections.emptyList();
        return KotlinPackage.map(elements, new Function1() {
            @Override
            public Java invoke(Psi psi) {
                return factory.create(psi);
            }
        });
    }

    @NotNull
    public static Collection classes(@NotNull PsiClass[] classes) {
        return convert(classes, Factories.CLASSES);
    }

    @NotNull
    public static Collection classes(@NotNull Iterable classes) {
        return convert(classes, Factories.CLASSES);
    }

    @NotNull
    public static Collection packages(@NotNull PsiPackage[] packages, @NotNull final GlobalSearchScope scope) {
        return convert(packages, new Factory() {
            @NotNull
            @Override
            public JavaPackage create(@NotNull PsiPackage aPackage) {
                return new JavaPackageImpl(aPackage, scope);
            }
        });
    }

    @NotNull
    public static Collection methods(@NotNull PsiMethod[] methods) {
        return convert(methods, Factories.METHODS);
    }

    @NotNull
    public static Collection methods(@NotNull Iterable methods) {
        return convert(methods, Factories.METHODS);
    }

    @NotNull
    public static Collection constructors(@NotNull PsiMethod[] methods) {
        return convert(methods, Factories.CONSTRUCTORS);
    }

    @NotNull
    public static Collection fields(@NotNull PsiField[] fields) {
        return convert(fields, Factories.FIELDS);
    }

    @NotNull
    public static List valueParameters(@NotNull PsiParameter[] parameters) {
        return convert(parameters, Factories.VALUE_PARAMETERS);
    }

    @NotNull
    public static List typeParameters(@NotNull PsiTypeParameter[] typeParameters) {
        return convert(typeParameters, Factories.TYPE_PARAMETERS);
    }

    @NotNull
    public static List types(@NotNull PsiType[] types) {
        return convert(types, Factories.TYPES);
    }

    @NotNull
    public static Collection classifierTypes(@NotNull PsiClassType[] classTypes) {
        return convert(classTypes, Factories.CLASSIFIER_TYPES);
    }

    @NotNull
    public static Collection annotations(@NotNull PsiAnnotation[] annotations) {
        return convert(annotations, Factories.ANNOTATIONS);
    }

    @NotNull
    public static Collection namedAnnotationArguments(@NotNull PsiNameValuePair[] nameValuePairs) {
        return convert(nameValuePairs, Factories.NAMED_ANNOTATION_ARGUMENTS);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy