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.java.structure.impl;
018    
019    import com.intellij.psi.*;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.jet.lang.resolve.java.structure.*;
022    import org.jetbrains.jet.lang.resolve.name.Name;
023    
024    import java.util.ArrayList;
025    import java.util.Collection;
026    import java.util.Collections;
027    import java.util.List;
028    
029    public class JavaElementCollectionFromPsiArrayUtil {
030        private JavaElementCollectionFromPsiArrayUtil() {
031        }
032    
033        private interface Factory<Psi, Java> {
034            @NotNull
035            Java create(@NotNull Psi psi);
036        }
037    
038        private static class Factories {
039            private static final Factory<PsiClass, JavaClass> CLASSES = new Factory<PsiClass, JavaClass>() {
040                @NotNull
041                @Override
042                public JavaClass create(@NotNull PsiClass psiClass) {
043                    return new JavaClassImpl(psiClass);
044                }
045            };
046    
047            private static final Factory<PsiPackage, JavaPackage> PACKAGES = new Factory<PsiPackage, JavaPackage>() {
048                @NotNull
049                @Override
050                public JavaPackage create(@NotNull PsiPackage psiPackage) {
051                    return new JavaPackageImpl(psiPackage);
052                }
053            };
054    
055            private static final Factory<PsiMethod, JavaMethod> METHODS = new Factory<PsiMethod, JavaMethod>() {
056                @NotNull
057                @Override
058                public JavaMethod create(@NotNull PsiMethod psiMethod) {
059                    return new JavaMethodImpl(psiMethod);
060                }
061            };
062    
063            private static final Factory<PsiField, JavaField> FIELDS = new Factory<PsiField, JavaField>() {
064                @NotNull
065                @Override
066                public JavaField create(@NotNull PsiField psiField) {
067                    return new JavaFieldImpl(psiField);
068                }
069            };
070    
071            private static final Factory<PsiParameter, JavaValueParameter> VALUE_PARAMETERS = new Factory<PsiParameter, JavaValueParameter>() {
072                @NotNull
073                @Override
074                public JavaValueParameter create(@NotNull PsiParameter psiParameter) {
075                    return new JavaValueParameterImpl(psiParameter);
076                }
077            };
078    
079            private static final Factory<PsiTypeParameter, JavaTypeParameter> TYPE_PARAMETERS =
080                    new Factory<PsiTypeParameter, JavaTypeParameter>() {
081                @NotNull
082                @Override
083                public JavaTypeParameter create(@NotNull PsiTypeParameter psiTypeParameter) {
084                    return new JavaTypeParameterImpl(psiTypeParameter);
085                }
086            };
087    
088            private static final Factory<PsiType, JavaType> TYPES = new Factory<PsiType, JavaType>() {
089                @NotNull
090                @Override
091                public JavaType create(@NotNull PsiType psiType) {
092                    return JavaTypeImpl.create(psiType);
093                }
094            };
095    
096            private static final Factory<PsiClassType, JavaClassifierType> CLASSIFIER_TYPES = new Factory<PsiClassType, JavaClassifierType>() {
097                @NotNull
098                @Override
099                public JavaClassifierType create(@NotNull PsiClassType psiClassType) {
100                    return new JavaClassifierTypeImpl(psiClassType);
101                }
102            };
103    
104            private static final Factory<PsiAnnotation, JavaAnnotation> ANNOTATIONS = new Factory<PsiAnnotation, JavaAnnotation>() {
105                @NotNull
106                @Override
107                public JavaAnnotation create(@NotNull PsiAnnotation psiAnnotation) {
108                    return new JavaAnnotationImpl(psiAnnotation);
109                }
110            };
111    
112            private static final Factory<PsiAnnotationMemberValue, JavaAnnotationArgument> NAMELESS_ANNOTATION_ARGUMENTS =
113                    new Factory<PsiAnnotationMemberValue, JavaAnnotationArgument>() {
114                @NotNull
115                @Override
116                public JavaAnnotationArgument create(@NotNull PsiAnnotationMemberValue psiAnnotationMemberValue) {
117                    return JavaAnnotationArgumentImpl.create(psiAnnotationMemberValue, null);
118                }
119            };
120    
121            private static final Factory<PsiNameValuePair, JavaAnnotationArgument> NAMED_ANNOTATION_ARGUMENTS =
122                    new Factory<PsiNameValuePair, JavaAnnotationArgument>() {
123                @NotNull
124                @Override
125                public JavaAnnotationArgument create(@NotNull PsiNameValuePair psiNameValuePair) {
126                    String name = psiNameValuePair.getName();
127                    PsiAnnotationMemberValue value = psiNameValuePair.getValue();
128                    assert value != null : "Annotation argument value cannot be null: " + name;
129                    return JavaAnnotationArgumentImpl.create(value, name == null ? null : Name.identifier(name));
130                }
131            };
132        }
133    
134        @NotNull
135        private static <Psi, Java> List<Java> convert(@NotNull Psi[] elements, @NotNull Factory<Psi, Java> factory) {
136            if (elements.length == 0) return Collections.emptyList();
137            List<Java> result = new ArrayList<Java>(elements.length);
138            for (Psi element : elements) {
139                result.add(factory.create(element));
140            }
141            return result;
142        }
143    
144        @NotNull
145        public static Collection<JavaClass> classes(@NotNull PsiClass[] classes) {
146            return convert(classes, Factories.CLASSES);
147        }
148    
149        @NotNull
150        public static Collection<JavaPackage> packages(@NotNull PsiPackage[] packages) {
151            return convert(packages, Factories.PACKAGES);
152        }
153    
154        @NotNull
155        public static Collection<JavaMethod> methods(@NotNull PsiMethod[] methods) {
156            return convert(methods, Factories.METHODS);
157        }
158    
159        @NotNull
160        public static Collection<JavaField> fields(@NotNull PsiField[] fields) {
161            return convert(fields, Factories.FIELDS);
162        }
163    
164        @NotNull
165        public static List<JavaValueParameter> valueParameters(@NotNull PsiParameter[] parameters) {
166            return convert(parameters, Factories.VALUE_PARAMETERS);
167        }
168    
169        @NotNull
170        public static List<JavaTypeParameter> typeParameters(@NotNull PsiTypeParameter[] typeParameters) {
171            return convert(typeParameters, Factories.TYPE_PARAMETERS);
172        }
173    
174        @NotNull
175        public static List<JavaType> types(@NotNull PsiType[] types) {
176            return convert(types, Factories.TYPES);
177        }
178    
179        @NotNull
180        public static Collection<JavaClassifierType> classifierTypes(@NotNull PsiClassType[] classTypes) {
181            return convert(classTypes, Factories.CLASSIFIER_TYPES);
182        }
183    
184        @NotNull
185        public static Collection<JavaAnnotation> annotations(@NotNull PsiAnnotation[] annotations) {
186            return convert(annotations, Factories.ANNOTATIONS);
187        }
188    
189        @NotNull
190        public static List<JavaAnnotationArgument> namelessAnnotationArguments(@NotNull PsiAnnotationMemberValue[] memberValues) {
191            return convert(memberValues, Factories.NAMELESS_ANNOTATION_ARGUMENTS);
192        }
193    
194        @NotNull
195        public static Collection<JavaAnnotationArgument> namedAnnotationArguments(@NotNull PsiNameValuePair[] nameValuePairs) {
196            return convert(nameValuePairs, Factories.NAMED_ANNOTATION_ARGUMENTS);
197        }
198    }