CollectionFactory: factory methods added. Got rid of Guava dependency

This commit is contained in:
Danila Ponomarenko
2012-06-04 14:23:36 +04:00
parent 43b14bcad0
commit 639e90a5d9
21 changed files with 124 additions and 67 deletions
@@ -40,7 +40,7 @@ public class LightModifierList extends LightElement implements PsiModifierList {
public LightModifierList(PsiManager manager, final Language language, String... modifiers) {
super(manager, language);
myModifiers = CollectionFactory.newTroveSet(modifiers);
myModifiers = CollectionFactory.troveSet(modifiers);
}
public void addModifier(String modifier) {
@@ -31,8 +31,8 @@ import java.util.List;
import java.util.Set;
public class NaturalLanguageTextSelectioner extends ExtendWordSelectionHandlerBase {
private static final Set<Character> NATURAL = CollectionFactory.newTroveSet('(', ')', '.', ',', ':', ';', '!', '?', '$', '@', '%', '\"', '\'');
private static final Set<Character> SENTENCE_END = CollectionFactory.newTroveSet('.', '!', '?');
private static final Set<Character> NATURAL = CollectionFactory.troveSet('(', ')', '.', ',', ':', ';', '!', '?', '$', '@', '%', '\"', '\'');
private static final Set<Character> SENTENCE_END = CollectionFactory.troveSet('.', '!', '?');
public boolean canSelect(PsiElement e) {
return e instanceof PsiPlainText || e instanceof PsiComment;
@@ -36,7 +36,7 @@ import java.util.List;
import java.util.Map;
import static com.intellij.util.containers.CollectionFactory.arrayList;
import static com.intellij.util.containers.CollectionFactory.newTroveMap;
import static com.intellij.util.containers.CollectionFactory.troveMap;
/**
* @author cdr
@@ -70,12 +70,12 @@ class UpdateFoldRegionsOperation implements Runnable {
public void run() {
EditorFoldingInfo info = EditorFoldingInfo.get(myEditor);
FoldingModelEx foldingModel = (FoldingModelEx)myEditor.getFoldingModel();
Map<TextRange,Boolean> rangeToExpandStatusMap = newTroveMap();
Map<TextRange,Boolean> rangeToExpandStatusMap = troveMap();
removeInvalidRegions(info, foldingModel, rangeToExpandStatusMap);
Map<FoldRegion, Boolean> shouldExpand = newTroveMap();
Map<FoldingGroup, Boolean> groupExpand = newTroveMap();
Map<FoldRegion, Boolean> shouldExpand = troveMap();
Map<FoldingGroup, Boolean> groupExpand = troveMap();
List<FoldRegion> newRegions = addNewRegions(info, foldingModel, rangeToExpandStatusMap, shouldExpand, groupExpand);
applyExpandStatus(newRegions, shouldExpand, groupExpand);
@@ -96,7 +96,7 @@ public class ListTemplatesHandler implements CodeInsightActionHandler {
if (description == null) {
return super.getAllLookupStrings();
}
return CollectionFactory.newSet(getLookupString(), description);
return CollectionFactory.hashSet(getLookupString(), description);
}
};
}
@@ -70,7 +70,7 @@ public class ConsoleFoldingSettings implements PersistentStateComponent<ConsoleF
}
private void writeDiff(List<String> added, List<String> removed, boolean negated) {
Set<String> baseline = CollectionFactory.newTroveSet();
Set<String> baseline = CollectionFactory.troveSet();
for (CustomizableConsoleFoldingBean regexp : CustomizableConsoleFoldingBean.EP_NAME.getExtensions()) {
if (regexp.negate == negated) {
baseline.add(regexp.substring);
@@ -126,7 +126,7 @@ public abstract class PlatformIdTableBuilding {
@NotNull
@Override
public Map<TodoIndexEntry, Integer> map(FileContent inputData) {
Map<TodoIndexEntry, Integer> result = CollectionFactory.newTroveMap();
Map<TodoIndexEntry, Integer> result = CollectionFactory.troveMap();
for (DataIndexer<TodoIndexEntry, Integer, FileContent> indexer : indexers) {
for (Map.Entry<TodoIndexEntry, Integer> entry : indexer.map(inputData).entrySet()) {
TodoIndexEntry key = entry.getKey();
@@ -73,7 +73,7 @@ public class EventLog implements Notifications {
private static final String A_CLOSING = "</a>";
private static final Pattern TAG_PATTERN = Pattern.compile("<[^>]*>");
private static final Pattern A_PATTERN = Pattern.compile("<a ([^>]* )?href=[\"\']([^>]*)[\"\'][^>]*>");
private static final Set<String> NEW_LINES = CollectionFactory.newSet("<br>", "</br>", "<br/>", "<p>", "</p>", "<p/>");
private static final Set<String> NEW_LINES = CollectionFactory.hashSet("<br>", "</br>", "<br/>", "<p>", "</p>", "<p/>");
public EventLog() {
ApplicationManager.getApplication().getMessageBus().connect().subscribe(Notifications.TOPIC, this);
@@ -93,7 +93,7 @@ public final class ToolWindowManagerImpl extends ToolWindowManagerEx implements
private final HashMap<String, FloatingDecorator> myId2FloatingDecorator;
private final HashMap<String, StripeButton> myId2StripeButton;
private final HashMap<String, FocusWatcher> myId2FocusWatcher;
private final Set<String> myDumbAwareIds = Collections.synchronizedSet(CollectionFactory.<String>newTroveSet());
private final Set<String> myDumbAwareIds = Collections.synchronizedSet(CollectionFactory.<String>troveSet());
private final EditorComponentFocusWatcher myEditorComponentFocusWatcher;
private final MyToolWindowPropertyChangeListener myToolWindowPropertyChangeListener;
@@ -793,7 +793,7 @@ public abstract class LightPlatformTestCase extends UsefulTestCase implements Da
final String[] myUrls = mySdk.getRootProvider().getUrls(OrderRootType.CLASSES);
final String[] newUrls = newSdk.getRootProvider().getUrls(OrderRootType.CLASSES);
return CollectionFactory.newSet(myUrls).equals(CollectionFactory.newSet(newUrls));
return CollectionFactory.hashSet(myUrls).equals(CollectionFactory.hashSet(newUrls));
}
}
@@ -15,16 +15,11 @@
*/
package com.intellij.util.containers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.HashMap;
import java.util.Stack;
/**
* @author peter
@@ -33,34 +28,118 @@ public class CollectionFactory {
private CollectionFactory() {
}
public static <T> Set<T> newSet(T... elements) {
return new HashSet<T>(Arrays.asList(elements));
@NotNull
public static <T> Set<T> hashSet() {
return new HashSet<T>();
}
public static <T> Set<T> newTroveSet(T... elements) {
return newTroveSet(Arrays.asList(elements));
@NotNull
public static <T> Set<T> hashSet(@NotNull Collection<T> elements) {
return new HashSet<T>(elements);
}
public static <T> Set<T> newTroveSet(Collection<T> elements) {
@NotNull
public static <T> Set<T> hashSet(@NotNull T... elements) {
return hashSet(Arrays.asList(elements));
}
public static <T> LinkedHashSet<T> linkedHashSet() {
return new LinkedHashSet<T>();
}
@NotNull
public static <T> Set<T> troveSet(@NotNull T... elements) {
return troveSet(Arrays.asList(elements));
}
@NotNull
public static <T> Set<T> troveSet(@NotNull Collection<T> elements) {
return new THashSet<T>(elements);
}
public static <T> T[] ar(T... elements) {
return elements;
@NotNull
public static <T> TreeSet<T> treeSet() {
return new TreeSet<T>();
}
public static <T,V> THashMap<T, V> newTroveMap() {
return new THashMap<T,V>();
@NotNull
public static <T> TreeSet<T> treeSet(@NotNull Collection<T> elements) {
return new TreeSet<T>(elements);
}
@NotNull
public static <T> TreeSet<T> treeSet(@NotNull T... elements) {
return treeSet(Arrays.asList(elements));
}
@NotNull
public static <T> TreeSet<T> treeSet(@NotNull Comparator<? super T> comparator) {
return new TreeSet<T>(comparator);
}
@NotNull
public static <T> Set<T> unmodifiableHashSet(@NotNull T... elements) {
return unmodifiableHashSet(Arrays.asList(elements));
}
@NotNull
public static <T> Set<T> unmodifiableHashSet(@NotNull Collection<T> elements) {
return Collections.unmodifiableSet(hashSet(elements));
}
@NotNull
public static <T, V> HashMap<T, V> hashMap() {
return new HashMap<T, V>();
}
@NotNull
public static <T, V> TreeMap<T, V> treeMap() {
return new TreeMap<T, V>();
}
@NotNull
public static <T, V> THashMap<T, V> troveMap() {
return new THashMap<T, V>();
}
public static <T, V> LinkedHashMap<T, V> linkedHashMap() {
return new LinkedHashMap<T, V>();
}
@NotNull
public static <T, V> Map<T, V> hashMap(@NotNull final List<T> keys, @NotNull final List<V> values) {
if (keys.size() != values.size()) {
throw new IllegalArgumentException(keys + " should have same length as " + values);
}
final HashMap<T, V> map = new HashMap<T, V>();
for (int i = 0; i < keys.size(); ++i) {
map.put(keys.get(i), values.get(i));
}
return map;
}
@NotNull
public static <T> ArrayList<T> arrayList() {
return Lists.newArrayList();
return new ArrayList<T>();
}
@NotNull
public static <T> ArrayList<T> arrayList(int initialCapacity) {
return new ArrayList<T>(initialCapacity);
}
@NotNull
public static <T> ArrayList<T> arrayList(Collection<T> elements) {
return new ArrayList<T>(elements);
}
@NotNull
public static <T> ArrayList<T> arrayList(T... elements) {
return new ArrayList<T>(Arrays.asList(elements));
return arrayList(Arrays.asList(elements));
}
@NotNull
public static <T> List<T> arrayList(@NotNull final T[] elements, final int start, final int end) {
if (start < 0 || start > end || end > elements.length) throw new IllegalArgumentException("start:" + start + " end:" + end + " length:" + elements.length);
@@ -80,35 +159,13 @@ public class CollectionFactory {
};
}
@NotNull
public static <T> Stack<T> stack() {
return new Stack<T>();
}
public static <T> Set<T> hashSet() {
return Sets.newHashSet();
}
public static <T, V> Map<T, V> hashMap() {
return Maps.newHashMap();
}
public static <T, V> Map<T, V> hashMap(@NotNull final List<T> keys, @NotNull final List<V> values) {
if (keys.size() != values.size()) {
throw new IllegalArgumentException(keys + " should have some length as " + values);
}
final HashMap<T,V> map = Maps.newHashMap();
for (int i = 0; i < keys.size(); ++i) {
map.put(keys.get(i), values.get(i));
}
return map;
}
public static <T, V> LinkedHashMap<T, V> linkedMap() {
return Maps.newLinkedHashMap();
}
public static <T> LinkedHashSet<T> linkedHashSet() {
return Sets.newLinkedHashSet();
@NotNull
public static <T> T[] ar(@NotNull T... elements) {
return elements;
}
}
@@ -448,7 +448,7 @@ public class GroovyCompletionUtil {
return TailType.insertChar(editor, offset, ')');
}
public static final Set<String> OPERATOR_METHOD_NAMES = CollectionFactory.newSet(
public static final Set<String> OPERATOR_METHOD_NAMES = CollectionFactory.hashSet(
"plus", "minus", "multiply", "power", "div", "mod", "or", "and", "xor", "next", "previous", "getAt", "putAt", "leftShift", "rightShift",
"isCase", "bitwiseNegate", "negative", "positive", "call"
);
@@ -41,7 +41,7 @@ import java.util.Set;
*/
public class GrKindWeigher extends CompletionWeigher {
private static final Set<String> TRASH_CLASSES = new HashSet<String>(10);
private static final Set<String> PRIORITY_KEYWORDS = CollectionFactory.newSet(
private static final Set<String> PRIORITY_KEYWORDS = CollectionFactory.hashSet(
PsiKeyword.RETURN, PsiKeyword.INSTANCEOF, "in",
PsiKeyword.PRIVATE, PsiKeyword.PROTECTED, PsiKeyword.PUBLIC, PsiKeyword.STATIC, "def",
PsiKeyword.TRUE, PsiKeyword.FALSE, PsiKeyword.NULL);
@@ -44,7 +44,7 @@ public class GdkMethodUtil {
private static final Logger LOG = Logger.getInstance(GdkMethodUtil.class);
public static final Set<String> COLLECTION_METHOD_NAMES = CollectionFactory.newSet(
public static final Set<String> COLLECTION_METHOD_NAMES = CollectionFactory.hashSet(
"each", "eachWithIndex", "any", "every", "reverseEach", "collect", "collectAll", "find", "findAll", "retainAll", "removeAll", "split",
"groupBy", "groupEntriesBy", "findLastIndexOf", "findIndexValues", "findIndexOf"
);
@@ -331,8 +331,8 @@ public class MvcModuleStructureUtil {
}
private static void removeInvalidSourceRoots(List<Consumer<ModifiableRootModel>> actions, MvcProjectStructure structure) {
final Set<SourceFolder> toRemove = CollectionFactory.newTroveSet();
final Set<ContentEntry> toRemoveContent = CollectionFactory.newTroveSet();
final Set<SourceFolder> toRemove = CollectionFactory.troveSet();
final Set<ContentEntry> toRemoveContent = CollectionFactory.troveSet();
for (ContentEntry entry : ModuleRootManager.getInstance(structure.myModule).getContentEntries()) {
final VirtualFile file = entry.getFile();
if (file == null || !structure.isValidContentRoot(file)) {
@@ -167,7 +167,7 @@ public class StubGenerator implements ClassItemGenerator {
return Collections.emptySet();
}
final Set<String> result = CollectionFactory.newTroveSet(ArrayUtil.EMPTY_STRING_ARRAY);
final Set<String> result = CollectionFactory.troveSet(ArrayUtil.EMPTY_STRING_ARRAY);
for (PsiClassType type : chainedConstructor.getThrowsList().getReferencedTypes()) {
StringBuilder builder = new StringBuilder();
writeType(builder, substitutor.substitute(type), constructor, classNameProvider);
@@ -190,7 +190,7 @@ class FooSpec extends spock.lang.Specification {
assertNotNull(lookupElements);
Set<String> missedVariants = CollectionFactory.newSet(expectedVariants);
Set<String> missedVariants = CollectionFactory.hashSet(expectedVariants);
for (LookupElement lookupElement : lookupElements) {
missedVariants.remove(lookupElement.getLookupString());
@@ -59,7 +59,7 @@ import java.util.regex.Pattern;
public class MavenResourceCompiler implements ClassPostProcessingCompiler {
private static final Key<List<String>> FILES_TO_DELETE_KEY = Key.create(MavenResourceCompiler.class.getSimpleName() + ".FILES_TO_DELETE");
private static final Set<String> DEFAULT_NON_FILTERED_EXTENSIONS = CollectionFactory.newSet("jpg", "jpeg", "gif", "bmp", "png");
private static final Set<String> DEFAULT_NON_FILTERED_EXTENSIONS = CollectionFactory.hashSet("jpg", "jpeg", "gif", "bmp", "png");
private Map<String, Set<String>> myOutputItemsCache = new THashMap<String, Set<String>>();
@@ -58,7 +58,7 @@ import java.util.regex.Pattern;
public class MavenDomUtil {
// see http://maven.apache.org/settings.html
private static final Set<String> SUBTAGS_IN_SETTINGS_FILE = CollectionFactory.newSet("localRepository", "interactiveMode",
private static final Set<String> SUBTAGS_IN_SETTINGS_FILE = CollectionFactory.hashSet("localRepository", "interactiveMode",
"usePluginRegistry", "offline", "pluginGroups",
"servers", "mirrors", "proxies", "profiles",
"activeProfiles");
@@ -51,7 +51,7 @@ public class MavenResumeAction extends AnAction {
private static final Logger LOG = Logger.getInstance(MavenResumeAction.class);
private static final Set<String> PARAMS_DISABLING_RESUME = CollectionFactory.newSet("-rf", "-resume-from", "-pl", "-projects", "-am",
private static final Set<String> PARAMS_DISABLING_RESUME = CollectionFactory.hashSet("-rf", "-resume-from", "-pl", "-projects", "-am",
"-also-make", "-amd", "-also-make-dependents");
public static final int STATE_INITIAL = 0;
@@ -617,7 +617,7 @@ public class MavenProject {
@NotNull
public Set<String> getSupportedPackagings() {
Set<String> result = CollectionFactory.newSet(MavenConstants.TYPE_POM,
Set<String> result = CollectionFactory.hashSet(MavenConstants.TYPE_POM,
MavenConstants.TYPE_JAR,
"ejb", "ejb-client", "war", "ear", "bundle", "maven-plugin");
for (MavenImporter each : getSuitableImporters()) {
@@ -37,7 +37,7 @@ class AddToCompositeCollectionInvocation implements Invocation {
}
public Object invoke(final DomInvocationHandler<?> handler, final Object[] args) throws Throwable {
Set<XmlTag> set = CollectionFactory.newTroveSet();
Set<XmlTag> set = CollectionFactory.troveSet();
for (final CollectionChildDescriptionImpl qname : myQnames) {
set.addAll(qname.getTagsGetter().fun(handler));
}