get rid of Pair/Couple

This commit is contained in:
Vladimir Krivosheev
2014-08-05 16:08:37 +02:00
parent db26bcceb5
commit d271d3ec98
7 changed files with 115 additions and 66 deletions
@@ -1,7 +1,6 @@
package org.jetbrains.builtInWebServer;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.packaging.artifacts.Artifact;
@@ -13,13 +12,13 @@ import org.jetbrains.annotations.Nullable;
final class ArtifactWebServerRootsProvider extends PrefixlessWebServerRootsProvider {
@Nullable
@Override
public Pair<VirtualFile, Pair<VirtualFile, String>> resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction<String, VirtualFile, VirtualFile> resolver) {
public PathInfo resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction<String, VirtualFile, VirtualFile> resolver) {
for (Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) {
VirtualFile root = artifact.getOutputFile();
if (root != null) {
VirtualFile file = root.findFileByRelativePath(path);
if (file != null) {
return Pair.create(file, new Pair<VirtualFile, String>(root, null));
return new PathInfo(file, root);
}
}
}
@@ -28,11 +27,11 @@ final class ArtifactWebServerRootsProvider extends PrefixlessWebServerRootsProvi
@Nullable
@Override
public Pair<VirtualFile, String> getRoot(@NotNull VirtualFile file, @NotNull Project project) {
public PathInfo getRoot(@NotNull VirtualFile file, @NotNull Project project) {
for (Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) {
VirtualFile root = artifact.getOutputFile();
if (root != null && VfsUtilCore.isAncestor(root, file, true)) {
return Pair.create(root, null);
return new PathInfo(file, root);
}
}
return null;
@@ -1,16 +1,28 @@
/*
* Copyright 2000-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.builtInWebServer;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.PairFunction;
@@ -21,7 +33,7 @@ import org.jetbrains.annotations.Nullable;
final class DefaultWebServerRootsProvider extends WebServerRootsProvider {
@Nullable
@Override
public Pair<VirtualFile, Pair<VirtualFile, String>> resolve(@NotNull String path, @NotNull Project project) {
public PathInfo resolve(@NotNull String path, @NotNull Project project) {
PairFunction<String, VirtualFile, VirtualFile> resolver;
if (PlatformUtils.isIntelliJ()) {
int index = path.indexOf('/');
@@ -41,12 +53,12 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider {
resolver = WebServerPathToFileManager.getInstance(project).getResolver(path);
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
Couple<VirtualFile> result = resolve(path, moduleRootManager.getSourceRoots(), resolver);
PathInfo result = resolve(path, moduleRootManager.getSourceRoots(), resolver, moduleName);
if (result == null) {
result = resolve(path, moduleRootManager.getContentRoots(), resolver);
result = resolve(path, moduleRootManager.getContentRoots(), resolver, moduleName);
}
if (result != null) {
return Pair.create(result.first, Pair.create(result.second, module.getName()));
return result;
}
}
}
@@ -62,7 +74,7 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider {
}
resolver = WebServerPathToFileManager.getInstance(project).getResolver(path);
Pair<VirtualFile, Pair<VirtualFile, String>> result = findByRelativePath(project, path, modules, true, resolver);
PathInfo result = findByRelativePath(project, path, modules, true, resolver);
if (result == null) {
// let's find in content roots
return findByRelativePath(project, path, modules, false, resolver);
@@ -74,24 +86,26 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider {
@Nullable
@Override
public Pair<VirtualFile, String> getRoot(@NotNull final VirtualFile file, @NotNull final Project project) {
return new ReadAction<Pair<VirtualFile, String>>() {
protected void run(@NotNull final Result<Pair<VirtualFile, String>> result) {
VirtualFile root = null;
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
if (fileIndex.isInSourceContent(file)) {
root = fileIndex.getSourceRootForFile(file);
}
else if (fileIndex.isInContent(file)) {
root = fileIndex.getContentRootForFile(file);
}
else if (fileIndex.isInLibraryClasses(file)) {
root = fileIndex.getClassRootForFile(file);
}
assert root != null : file.getPresentableUrl();
result.setResult(Pair.create(root, getModuleNameQualifier(project, fileIndex.getModuleForFile(file))));
public PathInfo getRoot(@NotNull VirtualFile file, @NotNull Project project) {
AccessToken token = ReadAction.start();
try {
VirtualFile root = null;
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
if (fileIndex.isInSourceContent(file)) {
root = fileIndex.getSourceRootForFile(file);
}
}.execute().getResultObject();
else if (fileIndex.isInContent(file)) {
root = fileIndex.getContentRootForFile(file);
}
else if (fileIndex.isInLibraryClasses(file)) {
root = fileIndex.getClassRootForFile(file);
}
assert root != null : file.getPresentableUrl();
return new PathInfo(file, root, getModuleNameQualifier(project, fileIndex.getModuleForFile(file)));
}
finally {
token.finish();
}
}
@Nullable
@@ -105,28 +119,29 @@ final class DefaultWebServerRootsProvider extends WebServerRootsProvider {
}
@Nullable
private static Couple<VirtualFile> resolve(@NotNull String path, @NotNull VirtualFile[] roots, @NotNull PairFunction<String, VirtualFile, VirtualFile> resolver) {
private static PathInfo resolve(@NotNull String path, @NotNull VirtualFile[] roots, @NotNull PairFunction<String, VirtualFile, VirtualFile> resolver, @Nullable String moduleName) {
for (VirtualFile root : roots) {
VirtualFile file = resolver.fun(path, root);
if (file != null) {
return Couple.of(file, root);
return new PathInfo(file, root, moduleName);
}
}
return null;
}
@Nullable
private static Pair<VirtualFile, Pair<VirtualFile, String>> findByRelativePath(@NotNull Project project,
@NotNull String path,
@NotNull Module[] modules,
boolean inSourceRoot,
@NotNull PairFunction<String, VirtualFile, VirtualFile> resolver) {
private static PathInfo findByRelativePath(@NotNull Project project,
@NotNull String path,
@NotNull Module[] modules,
boolean inSourceRoot,
@NotNull PairFunction<String, VirtualFile, VirtualFile> resolver) {
for (Module module : modules) {
if (!module.isDisposed()) {
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
Couple<VirtualFile> result = resolve(path, inSourceRoot ? moduleRootManager.getSourceRoots() : moduleRootManager.getContentRoots(), resolver);
PathInfo result = resolve(path, inSourceRoot ? moduleRootManager.getSourceRoots() : moduleRootManager.getContentRoots(), resolver, null);
if (result != null) {
return Pair.create(result.first, Pair.create(result.second, getModuleNameQualifier(project, module)));
result.moduleName = getModuleNameQualifier(project, module);
return result;
}
}
}
@@ -0,0 +1,47 @@
package org.jetbrains.builtInWebServer;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PathInfo {
private final VirtualFile child;
private final VirtualFile root;
String moduleName;
private String computedPath;
public PathInfo(@NotNull VirtualFile child, @NotNull VirtualFile root, @Nullable String moduleName) {
this.child = child;
this.root = root;
this.moduleName = moduleName;
}
public PathInfo(@NotNull VirtualFile child, @NotNull VirtualFile root) {
this(child, root, null);
}
@NotNull
public VirtualFile getChild() {
return child;
}
@NotNull
public VirtualFile getRoot() {
return root;
}
@Nullable
public String getModuleName() {
return moduleName;
}
@NotNull
public String getPath() {
if (computedPath == null) {
computedPath = (moduleName == null ? "" : moduleName + '/') + VfsUtilCore.getRelativePath(child, root, '/');
}
return computedPath;
}
}
@@ -1,7 +1,6 @@
package org.jetbrains.builtInWebServer;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.PairFunction;
import org.jetbrains.annotations.NotNull;
@@ -10,10 +9,10 @@ import org.jetbrains.annotations.Nullable;
public abstract class PrefixlessWebServerRootsProvider extends WebServerRootsProvider {
@Nullable
@Override
public final Pair<VirtualFile, Pair<VirtualFile, String>> resolve(@NotNull String path, @NotNull Project project) {
public final PathInfo resolve(@NotNull String path, @NotNull Project project) {
return resolve(path, project, WebServerPathToFileManager.getInstance(project).getResolver(path));
}
@Nullable
public abstract Pair<VirtualFile, Pair<VirtualFile, String>> resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction<String, VirtualFile, VirtualFile> resolver);
public abstract PathInfo resolve(@NotNull String path, @NotNull Project project, @NotNull PairFunction<String, VirtualFile, VirtualFile> resolver);
}
@@ -8,8 +8,6 @@ import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootAdapter;
import com.intellij.openapi.roots.ModuleRootEvent;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
@@ -46,7 +44,7 @@ public class WebServerPathToFileManager {
final Cache<String, VirtualFile> pathToFileCache = CacheBuilder.newBuilder().maximumSize(512).expireAfterAccess(10, TimeUnit.MINUTES).build();
// time to expire should be greater than pathToFileCache
private final Cache<VirtualFile, Pair<VirtualFile, String>> fileToRoot = CacheBuilder.newBuilder().maximumSize(512).expireAfterAccess(11, TimeUnit.MINUTES).build();
private final Cache<VirtualFile, PathInfo> fileToRoot = CacheBuilder.newBuilder().maximumSize(512).expireAfterAccess(11, TimeUnit.MINUTES).build();
public static WebServerPathToFileManager getInstance(@NotNull Project project) {
return ServiceManager.getService(project, WebServerPathToFileManager.class);
@@ -106,18 +104,13 @@ public class WebServerPathToFileManager {
@Nullable
public String getPath(@NotNull VirtualFile file) {
Pair<VirtualFile, String> root = getRoot(file);
if (root == null) {
return null;
}
else {
return (root.second == null ? "" : root.second + '/') + VfsUtilCore.getRelativePath(file, root.first, '/');
}
PathInfo pathInfo = getRoot(file);
return pathInfo == null ? null : pathInfo.getPath();
}
@Nullable
public Pair<VirtualFile, String> getRoot(@NotNull VirtualFile child) {
Pair<VirtualFile, String> result = fileToRoot.getIfPresent(child);
public PathInfo getRoot(@NotNull VirtualFile child) {
PathInfo result = fileToRoot.getIfPresent(child);
if (result == null) {
for (WebServerRootsProvider rootsProvider : WebServerRootsProvider.EP_NAME.getExtensions()) {
result = rootsProvider.getRoot(child, project);
@@ -133,10 +126,10 @@ public class WebServerPathToFileManager {
@Nullable
VirtualFile findByRelativePath(@NotNull Project project, @NotNull String path) {
for (WebServerRootsProvider rootsProvider : WebServerRootsProvider.EP_NAME.getExtensions()) {
Pair<VirtualFile, Pair<VirtualFile, String>> result = rootsProvider.resolve(path, project);
PathInfo result = rootsProvider.resolve(path, project);
if (result != null) {
fileToRoot.put(result.first, result.second);
return result.first;
fileToRoot.put(result.getChild(), result);
return result.getChild();
}
}
return null;
@@ -2,7 +2,6 @@ package org.jetbrains.builtInWebServer;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -11,13 +10,10 @@ public abstract class WebServerRootsProvider {
static final ExtensionPointName<WebServerRootsProvider> EP_NAME = ExtensionPointName.create("org.jetbrains.webServerRootsProvider");
@Nullable
/**
* @return pair of child and root or null if cannot resolve
*/
public abstract Pair<VirtualFile, Pair<VirtualFile, String>> resolve(@NotNull String path, @NotNull Project project);
public abstract PathInfo resolve(@NotNull String path, @NotNull Project project);
@Nullable
public abstract Pair<VirtualFile, String> getRoot(@NotNull VirtualFile file, @NotNull Project project);
public abstract PathInfo getRoot(@NotNull VirtualFile file, @NotNull Project project);
public boolean isClearCacheOnFileContentChanged(@NotNull VirtualFile file) {
return false;
@@ -1,7 +1,6 @@
package org.jetbrains.io.fastCgi;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
@@ -12,6 +11,7 @@ import io.netty.handler.codec.http.HttpHeaders;
import io.netty.util.CharsetUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.builtInWebServer.PathInfo;
import org.jetbrains.builtInWebServer.WebServerPathToFileManager;
import org.jetbrains.io.Responses;
@@ -41,9 +41,9 @@ public class FastCgiRequest {
}
public void writeFileHeaders(@NotNull VirtualFile file, @NotNull Project project, @NotNull CharSequence canonicalRequestPath) {
Pair<VirtualFile, String> root = WebServerPathToFileManager.getInstance(project).getRoot(file);
PathInfo root = WebServerPathToFileManager.getInstance(project).getRoot(file);
FastCgiService.LOG.assertTrue(root != null);
addHeader("DOCUMENT_ROOT", root.first.getPath());
addHeader("DOCUMENT_ROOT", root.getRoot().getPath());
addHeader("SCRIPT_FILENAME", file.getPath());
addHeader("SCRIPT_NAME", canonicalRequestPath);
}