diff options
Diffstat (limited to 'platform/util-rt')
7 files changed, 100 insertions, 10 deletions
diff --git a/platform/util-rt/src/com/intellij/openapi/util/Condition.java b/platform/util-rt/src/com/intellij/openapi/util/Condition.java index 7eb9c1c7a8f6..d75ec46aa8cc 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/Condition.java +++ b/platform/util-rt/src/com/intellij/openapi/util/Condition.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * 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. @@ -28,6 +28,7 @@ public interface Condition<T> { boolean value(T t); Condition<Object> NOT_NULL = new Condition<Object>() { + @Override public boolean value(final Object object) { return object != null; } @@ -43,6 +44,7 @@ public interface Condition<T> { * @see com.intellij.openapi.util.Conditions#alwaysTrue() */ Condition TRUE = new Condition() { + @Override public boolean value(final Object object) { return true; } @@ -57,6 +59,7 @@ public interface Condition<T> { * @see com.intellij.openapi.util.Conditions#alwaysFalse() */ Condition FALSE = new Condition() { + @Override public boolean value(final Object object) { return false; } diff --git a/platform/util-rt/src/com/intellij/openapi/util/Conditions.java b/platform/util-rt/src/com/intellij/openapi/util/Conditions.java index 48bb06ad8b8e..ef3c0d2acbf6 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/Conditions.java +++ b/platform/util-rt/src/com/intellij/openapi/util/Conditions.java @@ -18,6 +18,7 @@ package com.intellij.openapi.util; import com.intellij.reference.SoftReference; import com.intellij.util.ArrayUtilRt; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; @@ -27,15 +28,18 @@ import java.util.HashMap; public class Conditions { private Conditions() {} + @NotNull public static <T> Condition<T> alwaysTrue() { return (Condition<T>)TRUE; } + @NotNull public static <T> Condition<T> alwaysFalse() { return (Condition<T>)FALSE; } public static <T> Condition<T> instanceOf(final Class<?> clazz) { return new Condition<T>() { + @Override public boolean value(T t) { return clazz.isInstance(t); } @@ -44,6 +48,7 @@ public class Conditions { public static <T> Condition<T> is(final T option) { return new Condition<T>() { + @Override public boolean value(T t) { return Comparing.equal(t, option); } @@ -52,6 +57,7 @@ public class Conditions { public static <T> Condition<T> oneOf(final T... options) { return new Condition<T>() { + @Override public boolean value(T t) { return ArrayUtilRt.find(options, t) >= 0; } @@ -78,6 +84,7 @@ public class Conditions { myCondition = condition; } + @Override public boolean value(T value) { return !myCondition.value(value); } @@ -91,6 +98,7 @@ public class Conditions { this.t2 = t2; } + @Override public boolean value(final T object) { return t1.value(object) && t2.value(object); } @@ -104,17 +112,20 @@ public class Conditions { this.t2 = t2; } + @Override public boolean value(final T object) { return t1.value(object) || t2.value(object); } } public static Condition<Object> TRUE = new Condition<Object>() { + @Override public boolean value(final Object object) { return true; } }; public static Condition<Object> FALSE = new Condition<Object>() { + @Override public boolean value(final Object object) { return false; } @@ -128,6 +139,7 @@ public class Conditions { myCondition = condition; } + @Override public final boolean value(T object) { final int key = object.hashCode(); final Pair<SoftReference<T>, Boolean> entry = myCache.get(key); diff --git a/platform/util-rt/src/com/intellij/openapi/util/text/StringUtilRt.java b/platform/util-rt/src/com/intellij/openapi/util/text/StringUtilRt.java index 8b6e353fdbea..cc667ba8a80d 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/text/StringUtilRt.java +++ b/platform/util-rt/src/com/intellij/openapi/util/text/StringUtilRt.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -15,6 +15,7 @@ */ package com.intellij.openapi.util.text; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -27,11 +28,13 @@ import org.jetbrains.annotations.Nullable; */ @SuppressWarnings({"UtilityClassWithoutPrivateConstructor"}) public class StringUtilRt { + @Contract(pure = true) public static boolean charsEqualIgnoreCase(char a, char b) { return a == b || toUpperCase(a) == toUpperCase(b) || toLowerCase(a) == toLowerCase(b); } @NotNull + @Contract(pure = true) public static String toUpperCase(@NotNull String s) { StringBuilder answer = null; @@ -51,6 +54,7 @@ public class StringUtilRt { return answer == null ? s : answer.toString(); } + @Contract(pure = true) public static char toUpperCase(char a) { if (a < 'a') { return a; @@ -61,6 +65,7 @@ public class StringUtilRt { return Character.toUpperCase(a); } + @Contract(pure = true) public static char toLowerCase(char a) { if (a < 'A' || a >= 'a' && a <= 'z') { return a; @@ -77,21 +82,25 @@ public class StringUtilRt { * Converts line separators to <code>"\n"</code> */ @NotNull + @Contract(pure = true) public static String convertLineSeparators(@NotNull String text) { return convertLineSeparators(text, false); } @NotNull + @Contract(pure = true) public static String convertLineSeparators(@NotNull String text, boolean keepCarriageReturn) { return convertLineSeparators(text, "\n", null, keepCarriageReturn); } @NotNull + @Contract(pure = true) public static String convertLineSeparators(@NotNull String text, @NotNull String newSeparator) { return convertLineSeparators(text, newSeparator, null); } @NotNull + @Contract(pure = true) public static CharSequence convertLineSeparators(@NotNull CharSequence text, @NotNull String newSeparator) { return unifyLineSeparators(text, newSeparator, null, false); } @@ -110,6 +119,7 @@ public class StringUtilRt { } @NotNull + @Contract(pure = true) public static CharSequence unifyLineSeparators(@NotNull CharSequence text) { return unifyLineSeparators(text, "\n", null, false); } @@ -189,6 +199,7 @@ public class StringUtilRt { } } + @Contract(pure = true) public static int parseInt(final String string, final int defaultValue) { try { return Integer.parseInt(string); @@ -198,6 +209,7 @@ public class StringUtilRt { } } + @Contract(pure = true) public static double parseDouble(final String string, final double defaultValue) { try { return Double.parseDouble(string); @@ -207,6 +219,7 @@ public class StringUtilRt { } } + @Contract(pure = true) public static boolean parseBoolean(final String string, final boolean defaultValue) { try { return Boolean.parseBoolean(string); @@ -217,16 +230,19 @@ public class StringUtilRt { } @NotNull + @Contract(pure = true) public static String getShortName(@NotNull Class aClass) { return getShortName(aClass.getName()); } @NotNull + @Contract(pure = true) public static String getShortName(@NotNull String fqName) { return getShortName(fqName, '.'); } @NotNull + @Contract(pure = true) public static String getShortName(@NotNull String fqName, char separator) { int lastPointIdx = fqName.lastIndexOf(separator); if (lastPointIdx >= 0) { @@ -235,16 +251,19 @@ public class StringUtilRt { return fqName; } + @Contract(pure = true) public static boolean endsWithChar(@Nullable CharSequence s, char suffix) { return s != null && s.length() != 0 && s.charAt(s.length() - 1) == suffix; } + @Contract(pure = true) public static boolean startsWithIgnoreCase(@NonNls @NotNull String str, @NonNls @NotNull String prefix) { final int stringLength = str.length(); final int prefixLength = prefix.length(); return stringLength >= prefixLength && str.regionMatches(true, 0, prefix, 0, prefixLength); } + @Contract(pure = true) public static boolean endsWithIgnoreCase(@NonNls @NotNull CharSequence text, @NonNls @NotNull CharSequence suffix) { int l1 = text.length(); int l2 = suffix.length(); @@ -269,6 +288,7 @@ public class StringUtilRt { * @return index of the last occurrence of the given symbol at the target sub-sequence of the given text if any; * <code>-1</code> otherwise */ + @Contract(pure = true) public static int lastIndexOf(@NotNull CharSequence s, char c, int start, int end) { for (int i = end - 1; i >= start; i--) { if (s.charAt(i) == c) return i; diff --git a/platform/util-rt/src/com/intellij/util/ArrayUtilRt.java b/platform/util-rt/src/com/intellij/util/ArrayUtilRt.java index b18b62889623..115a6ceba1e5 100644 --- a/platform/util-rt/src/com/intellij/util/ArrayUtilRt.java +++ b/platform/util-rt/src/com/intellij/util/ArrayUtilRt.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -15,8 +15,9 @@ */ package com.intellij.util; -import org.jetbrains.annotations.NotNull; import com.intellij.util.containers.ContainerUtilRt; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; @@ -44,6 +45,7 @@ public class ArrayUtilRt { public static final Runnable[] EMPTY_RUNNABLE_ARRAY = new Runnable[0]; @NotNull + @Contract(pure=true) public static String[] toStringArray(@Nullable Collection<String> collection) { return collection == null || collection.isEmpty() ? EMPTY_STRING_ARRAY : ContainerUtilRt.toArray(collection, new String[collection.size()]); @@ -57,6 +59,7 @@ public class ArrayUtilRt { * <code>equals</code> of arrays elements to compare <code>obj</code> with * these elements. */ + @Contract(pure=true) public static <T> int find(@NotNull final T[] src, final T obj) { for (int i = 0; i < src.length; i++) { final T o = src[i]; diff --git a/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java b/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java index e4b3153917f7..a89de17fe6fe 100644 --- a/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java +++ b/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java @@ -18,6 +18,7 @@ package com.intellij.util.containers; import com.intellij.openapi.util.Pair; import com.intellij.util.ArrayUtilRt; import com.intellij.util.Function; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -40,16 +41,19 @@ public class ContainerUtilRt { private static final int ARRAY_COPY_THRESHOLD = 20; @NotNull + @Contract(pure=true) public static <K, V> HashMap<K, V> newHashMap() { return new com.intellij.util.containers.HashMap<K, V>(); } @NotNull + @Contract(pure=true) public static <K, V> HashMap<K, V> newHashMap(@NotNull Map<K, V> map) { return new com.intellij.util.containers.HashMap<K, V>(map); } @NotNull + @Contract(pure=true) public static <K, V> Map<K, V> newHashMap(@NotNull List<K> keys, @NotNull List<V> values) { if (keys.size() != values.size()) { throw new IllegalArgumentException(keys + " should have same length as " + values); @@ -63,6 +67,7 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <K, V> Map<K,V> newHashMap(@NotNull Pair<K, V> first, Pair<K, V>[] entries) { Map<K, V> map = newHashMap(); map.put(first.getFirst(), first.getSecond()); @@ -73,26 +78,43 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) + public static <K, V> Map<K, V> newHashMap(int initialCapacity) { + return new com.intellij.util.containers.HashMap<K, V>(initialCapacity); + } + + @NotNull + @Contract(pure=true) public static <K extends Comparable, V> TreeMap<K, V> newTreeMap() { return new TreeMap<K, V>(); } @NotNull + @Contract(pure=true) public static <K extends Comparable, V> TreeMap<K, V> newTreeMap(@NotNull Map<K, V> map) { return new TreeMap<K, V>(map); } @NotNull + @Contract(pure=true) public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() { return new com.intellij.util.containers.LinkedHashMap<K, V>(); } @NotNull + @Contract(pure=true) + public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int capacity) { + return new com.intellij.util.containers.LinkedHashMap<K, V>(capacity); + } + + @NotNull + @Contract(pure=true) public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(@NotNull Map<K, V> map) { return new com.intellij.util.containers.LinkedHashMap<K, V>(map); } @NotNull + @Contract(pure=true) public static <K, V> LinkedHashMap<K,V> newLinkedHashMap(@NotNull Pair<K, V> first, Pair<K, V>[] entries) { LinkedHashMap<K, V> map = newLinkedHashMap(); map.put(first.getFirst(), first.getSecond()); @@ -103,11 +125,13 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> LinkedList<T> newLinkedList() { return new LinkedList<T>(); } @NotNull + @Contract(pure=true) public static <T> LinkedList<T> newLinkedList(@NotNull T... elements) { final LinkedList<T> list = newLinkedList(); Collections.addAll(list, elements); @@ -115,16 +139,19 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> LinkedList<T> newLinkedList(@NotNull Iterable<? extends T> elements) { return copy(ContainerUtilRt.<T>newLinkedList(), elements); } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayList() { return new ArrayList<T>(); } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayList(@NotNull T... elements) { ArrayList<T> list = newArrayListWithCapacity(elements.length); Collections.addAll(list, elements); @@ -132,6 +159,7 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayList(@NotNull Iterable<? extends T> elements) { if (elements instanceof Collection) { @SuppressWarnings("unchecked") Collection<? extends T> collection = (Collection<? extends T>)elements; @@ -141,11 +169,13 @@ public class ContainerUtilRt { } /** @deprecated Use {@link #newArrayListWithCapacity(int)} (to remove in IDEA 15) */ + @Contract(pure=true) public static <T> ArrayList<T> newArrayListWithExpectedSize(int size) { return newArrayListWithCapacity(size); } @NotNull + @Contract(pure=true) public static <T> ArrayList<T> newArrayListWithCapacity(int size) { return new ArrayList<T>(size); } @@ -159,21 +189,25 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet() { return new com.intellij.util.containers.HashSet<T>(); } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(int initialCapacity) { return new com.intellij.util.containers.HashSet<T>(initialCapacity); } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(@NotNull T... elements) { return new com.intellij.util.containers.HashSet<T>(Arrays.asList(elements)); } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(@NotNull Iterable<? extends T> elements) { if (elements instanceof Collection) { @SuppressWarnings("unchecked") Collection<? extends T> collection = (Collection<? extends T>)elements; @@ -183,6 +217,7 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> HashSet<T> newHashSet(@NotNull Iterator<? extends T> iterator) { HashSet<T> set = newHashSet(); while (iterator.hasNext()) set.add(iterator.next()); @@ -190,16 +225,19 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> LinkedHashSet<T> newLinkedHashSet() { return new com.intellij.util.containers.LinkedHashSet<T>(); } @NotNull + @Contract(pure=true) public static <T> LinkedHashSet<T> newLinkedHashSet(@NotNull T... elements) { return newLinkedHashSet(Arrays.asList(elements)); } @NotNull + @Contract(pure=true) public static <T> LinkedHashSet<T> newLinkedHashSet(@NotNull Iterable<? extends T> elements) { if (elements instanceof Collection) { @SuppressWarnings("unchecked") Collection<? extends T> collection = (Collection<? extends T>)elements; @@ -209,11 +247,13 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet() { return new TreeSet<T>(); } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet(@NotNull T... elements) { TreeSet<T> set = newTreeSet(); Collections.addAll(set, elements); @@ -221,26 +261,31 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet(@NotNull Iterable<? extends T> elements) { return copy(ContainerUtilRt.<T>newTreeSet(), elements); } @NotNull + @Contract(pure=true) public static <T> TreeSet<T> newTreeSet(@Nullable Comparator<? super T> comparator) { return new TreeSet<T>(comparator); } @NotNull + @Contract(pure=true) public static <T> Stack<T> newStack() { return new Stack<T>(); } @NotNull + @Contract(pure=true) public static <T> Stack<T> newStack(@NotNull Collection<T> elements) { return new Stack<T>(elements); } @NotNull + @Contract(pure=true) public static <T> Stack<T> newStack(@NotNull T... initial) { return new Stack<T>(Arrays.asList(initial)); } @@ -292,12 +337,14 @@ public class ContainerUtilRt { } @NotNull + @Contract(pure=true) public static <T> List<T> emptyList() { //noinspection unchecked return (List<T>)EmptyList.INSTANCE; } @NotNull + @Contract(pure=true) public static <T> CopyOnWriteArrayList<T> createEmptyCOWList() { // does not create garbage new Object[0] return new CopyOnWriteArrayList<T>(ContainerUtilRt.<T>emptyList()); @@ -319,6 +366,7 @@ public class ContainerUtilRt { * @return read-only list consisting of the elements from array converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> List<V> map2List(@NotNull T[] array, @NotNull Function<T, V> mapper) { return map2List(Arrays.asList(array), mapper); } @@ -327,6 +375,7 @@ public class ContainerUtilRt { * @return read-only list consisting of the elements from collection converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> List<V> map2List(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) { if (collection.isEmpty()) return emptyList(); List<V> list = new ArrayList<V>(collection.size()); @@ -340,6 +389,7 @@ public class ContainerUtilRt { * @return read-only set consisting of the elements from collection converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> Set<V> map2Set(@NotNull T[] collection, @NotNull Function<T, V> mapper) { return map2Set(Arrays.asList(collection), mapper); } @@ -348,6 +398,7 @@ public class ContainerUtilRt { * @return read-only set consisting of the elements from collection converted by mapper */ @NotNull + @Contract(pure=true) public static <T, V> Set<V> map2Set(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) { if (collection.isEmpty()) return Collections.emptySet(); Set <V> set = new HashSet<V>(collection.size()); diff --git a/platform/util-rt/src/com/intellij/util/containers/HashMap.java b/platform/util-rt/src/com/intellij/util/containers/HashMap.java index af41488814d0..fa8608f50bf8 100644 --- a/platform/util-rt/src/com/intellij/util/containers/HashMap.java +++ b/platform/util-rt/src/com/intellij/util/containers/HashMap.java @@ -21,12 +21,12 @@ import java.util.Map; public class HashMap<K, V> extends java.util.HashMap<K, V> { public HashMap() { } - public HashMap(int i, float v) { - super(i, v); + public HashMap(int initialCapacity, float loadFactor) { + super(initialCapacity, loadFactor); } - public HashMap(int i) { - super(i); + public HashMap(int initialCapacity) { + super(initialCapacity); } public <K1 extends K, V1 extends V> HashMap(Map<K1, V1> map) { diff --git a/platform/util-rt/src/com/intellij/util/containers/Stack.java b/platform/util-rt/src/com/intellij/util/containers/Stack.java index 29337c9c99b1..f1a0e37b1967 100644 --- a/platform/util-rt/src/com/intellij/util/containers/Stack.java +++ b/platform/util-rt/src/com/intellij/util/containers/Stack.java @@ -15,6 +15,7 @@ */ package com.intellij.util.containers; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -32,11 +33,11 @@ public class Stack<T> extends ArrayList<T> { super(initialCapacity); } - public Stack(Collection<T> init) { + public Stack(@NotNull Collection<T> init) { super(init); } - public Stack(T... items) { + public Stack(@NotNull T... items) { for (T item : items) { push(item); } |