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 public class KtImportsFactory {
031 @NotNull private final Project project;
032
033 private final Map<ImportPath, KtImportDirective> importsCache = Maps.newHashMap();
034
035 public KtImportsFactory(@NotNull Project project) {
036 this.project = project;
037 }
038
039 @NotNull
040 public KtImportDirective createImportDirective(@NotNull ImportPath importPath) {
041 KtImportDirective directive = importsCache.get(importPath);
042 if (directive != null) {
043 return directive;
044 }
045
046 KtImportDirective createdDirective = KtPsiFactoryKt.KtPsiFactory(project).createImportDirective(importPath);
047 importsCache.put(importPath, createdDirective);
048
049 return createdDirective;
050 }
051
052 @NotNull
053 public Collection<KtImportDirective> createImportDirectives(@NotNull Collection<ImportPath> importPaths) {
054 return Collections2.transform(importPaths,
055 new Function<ImportPath, KtImportDirective>() {
056 @Override
057 public KtImportDirective apply(@Nullable ImportPath path) {
058 assert path != null;
059 return createImportDirective(path);
060 }
061 });
062 }
063 }