com.intellij.util.PathsList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-api Show documentation
Show all versions of core-api Show documentation
A packaging of the IntelliJ Community Edition core-api library.
This is release number 1 of trunk branch 142.
The newest version!
/*
* Copyright 2000-2012 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 com.intellij.util;
import com.intellij.ide.highlighter.ArchiveFileType;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeRegistry;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.StandardFileSystems;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.FilteringIterator;
import com.intellij.util.containers.HashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.*;
import static com.intellij.util.containers.ContainerUtil.*;
public class PathsList {
private final List myPath = new ArrayList();
private final List myPathTail = new ArrayList();
private final Set myPathSet = new HashSet();
private static final Function PATH_TO_LOCAL_VFILE = new NullableFunction() {
@Override
public VirtualFile fun(String path) {
return StandardFileSystems.local().findFileByPath(path.replace(File.separatorChar, '/'));
}
};
private static final Function LOCAL_PATH = new Function() {
@Override
public String fun(VirtualFile file) {
return PathUtil.getLocalPath(file);
}
};
private static final Function PATH_TO_DIR = new NullableFunction() {
@Override
public VirtualFile fun(String s) {
final FileType fileType = FileTypeRegistry.getInstance().getFileTypeByFileName(s);
final VirtualFile localFile = PATH_TO_LOCAL_VFILE.fun(s);
if (localFile == null) return null;
if (ArchiveFileType.INSTANCE.equals(fileType) && !localFile.isDirectory()) {
return StandardFileSystems.getJarRootForLocalFile(localFile);
}
return localFile;
}
};
public void add(@NonNls String path) {
addAllLast(chooseFirstTimeItems(path), myPath);
}
public void remove(@NotNull String path) {
myPath.remove(path);
myPathTail.remove(path);
myPathSet.remove(path);
}
public void add(VirtualFile file) {
add(LOCAL_PATH.fun(file));
}
public void addFirst(@NonNls String path) {
final Iterator elements = chooseFirstTimeItems(path);
int index = 0;
while (elements.hasNext()) {
final String element = elements.next();
myPath.add(index, element);
myPathSet.add(element);
index++;
}
}
public void addTail(String path) {
addAllLast(chooseFirstTimeItems(path), myPathTail);
}
private Iterator chooseFirstTimeItems(String path) {
if (path == null) {
return emptyIterator();
}
final StringTokenizer tokenizer = new StringTokenizer(path, File.pathSeparator);
// in JDK 1.5 StringTokenizer implements Enumeration