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.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.jet.lang.resolve.ImportPath;
026
027 import javax.inject.Inject;
028 import java.util.Collection;
029 import java.util.Map;
030
031 public class JetImportsFactory {
032 @NotNull
033 private Project project;
034
035 private final Map<ImportPath, JetImportDirective> importsCache = Maps.newHashMap();
036
037 @Inject
038 public void setProject(@NotNull Project project) {
039 importsCache.clear();
040 this.project = project;
041 }
042
043 @NotNull
044 public JetImportDirective createImportDirective(@NotNull ImportPath importPath) {
045 JetImportDirective directive = importsCache.get(importPath);
046 if (directive != null) {
047 return directive;
048 }
049
050 JetImportDirective createdDirective = JetPsiFactory.createImportDirective(project, importPath);
051 importsCache.put(importPath, createdDirective);
052
053 return createdDirective;
054 }
055
056 @NotNull
057 public Collection<JetImportDirective> createImportDirectives(@NotNull Collection<ImportPath> importPaths) {
058 return Collections2.transform(importPaths,
059 new Function<ImportPath, JetImportDirective>() {
060 @Override
061 public JetImportDirective apply(@Nullable ImportPath path) {
062 assert path != null;
063 return createImportDirective(path);
064 }
065 });
066 }
067 }