001 /*
002 * Copyright 2010-2015 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.kotlin.psi;
018
019 import com.google.common.base.Function;
020 import com.google.common.collect.Collections2;
021 import com.google.common.collect.Maps;
022 import com.intellij.openapi.project.Project;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.kotlin.resolve.ImportPath;
026
027 import java.util.Collection;
028 import java.util.Map;
029
030 import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
031
032 public class JetImportsFactory {
033 @NotNull private final Project project;
034
035 private final Map<ImportPath, JetImportDirective> importsCache = Maps.newHashMap();
036
037 public JetImportsFactory(@NotNull Project project) {
038 this.project = project;
039 }
040
041 @NotNull
042 public JetImportDirective createImportDirective(@NotNull ImportPath importPath) {
043 JetImportDirective directive = importsCache.get(importPath);
044 if (directive != null) {
045 return directive;
046 }
047
048 JetImportDirective createdDirective = JetPsiFactory(project).createImportDirective(importPath);
049 importsCache.put(importPath, createdDirective);
050
051 return createdDirective;
052 }
053
054 @NotNull
055 public Collection<JetImportDirective> createImportDirectives(@NotNull Collection<ImportPath> importPaths) {
056 return Collections2.transform(importPaths,
057 new Function<ImportPath, JetImportDirective>() {
058 @Override
059 public JetImportDirective apply(@Nullable ImportPath path) {
060 assert path != null;
061 return createImportDirective(path);
062 }
063 });
064 }
065 }