diff --git a/java/java-tests/testSrc/com/intellij/psi/TreeIsCorrectAfterDiffReparseTest.java b/java/java-tests/testSrc/com/intellij/psi/TreeIsCorrectAfterDiffReparseTest.java index b5e1712fbbd6..ef874519d512 100644 --- a/java/java-tests/testSrc/com/intellij/psi/TreeIsCorrectAfterDiffReparseTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/TreeIsCorrectAfterDiffReparseTest.java @@ -6,11 +6,12 @@ package com.intellij.psi; import com.intellij.openapi.editor.Document; import com.intellij.psi.impl.DebugUtil; import com.intellij.testFramework.LightCodeInsightTestCase; +import org.jetbrains.annotations.NonNls; public class TreeIsCorrectAfterDiffReparseTest extends LightCodeInsightTestCase { public void testIDEADEV41862() throws Exception { - String part1 = "package com.test;\n" + + @NonNls String part1 = "package com.test;\n" + "\n" + "\n" + "//------------------------------------------------------------------\n" + @@ -952,7 +953,7 @@ public class TreeIsCorrectAfterDiffReparseTest extends LightCodeInsightTestCase "\n" + " "; - String part2 = "\n" + + @NonNls String part2 = "\n" + " private TransactionStatus getHighestPriorityError(TransactionStatus existingStatus, Message newError) {\n" + " if(existingStatus == null) {\n" + " return newError;\n" + diff --git a/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightFixtureTestCase.java b/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightFixtureTestCase.java index 4fc029dcd86c..d811f49e3358 100644 --- a/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightFixtureTestCase.java +++ b/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightFixtureTestCase.java @@ -55,6 +55,14 @@ public abstract class JavaCodeInsightFixtureTestCase extends UsefulTestCase{ myModule = moduleFixtureBuilder.getFixture().getModule(); } + @Override + protected void tearDown() throws Exception { + myModule = null; + myFixture.tearDown(); + myFixture = null; + super.tearDown(); + } + /** * Return relative path to the test data. Path is relative to the * {@link com.intellij.openapi.application.PathManager#getHomePath()} @@ -78,13 +86,6 @@ public abstract class JavaCodeInsightFixtureTestCase extends UsefulTestCase{ protected void tuneFixture(final JavaModuleFixtureBuilder moduleBuilder) throws Exception {} - @Override - protected void tearDown() throws Exception { - myModule = null; - myFixture.tearDown(); - myFixture = null; - super.tearDown(); - } protected Project getProject() { return myFixture.getProject(); diff --git a/platform/core-impl/src/com/intellij/openapi/editor/impl/IntervalTreeImpl.java b/platform/core-impl/src/com/intellij/openapi/editor/impl/IntervalTreeImpl.java index e70292090890..46628def5b91 100644 --- a/platform/core-impl/src/com/intellij/openapi/editor/impl/IntervalTreeImpl.java +++ b/platform/core-impl/src/com/intellij/openapi/editor/impl/IntervalTreeImpl.java @@ -764,8 +764,10 @@ public abstract class IntervalTreeImpl extends RedBla // returns true if all markers are valid public boolean checkMax(boolean assertInvalid) { - if (!VERIFY) return false; + return VERIFY && doCheckMax(assertInvalid); + } + protected boolean doCheckMax(boolean assertInvalid) { try { l.readLock().lock(); diff --git a/platform/lang-impl/src/com/intellij/psi/PsiAnchor.java b/platform/lang-impl/src/com/intellij/psi/PsiAnchor.java index 6529269590fd..c50727ee7fa2 100644 --- a/platform/lang-impl/src/com/intellij/psi/PsiAnchor.java +++ b/platform/lang-impl/src/com/intellij/psi/PsiAnchor.java @@ -327,7 +327,7 @@ public abstract class PsiAnchor { @Nullable public static PsiElement restoreFromStubIndex(PsiFileWithStubSupport fileImpl, int index, - IStubElementType elementType) { + @NotNull IStubElementType elementType) { if (fileImpl == null) return null; StubTree tree = fileImpl.getStubTree(); diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java index 4a38c34f6124..c4ddb41a944a 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/impl/local/LocalFileSystemImpl.java @@ -139,7 +139,7 @@ public final class LocalFileSystemImpl extends LocalFileSystemBase implements Ap } @TestOnly - public void cleanupForNextTest(Set survivors) throws IOException { + public void cleanupForNextTest(@NotNull Set survivors) throws IOException { ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run() { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorProcessingOnDocumentModificationTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorProcessingOnDocumentModificationTest.java index 59b39a0f53c1..c49532ca9315 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorProcessingOnDocumentModificationTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/editor/impl/AbstractEditorProcessingOnDocumentModificationTest.java @@ -25,6 +25,7 @@ import com.intellij.openapi.util.Trinity; import com.intellij.openapi.util.text.StringUtil; import com.intellij.testFramework.LightPlatformCodeInsightTestCase; import com.intellij.testFramework.TestFileType; +import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -44,7 +45,7 @@ import java.util.regex.Pattern; * @since 11/18/10 7:43 PM */ public abstract class AbstractEditorProcessingOnDocumentModificationTest extends LightPlatformCodeInsightTestCase { - protected void init(@NotNull String fileText, @NotNull TestFileType type) throws IOException { + protected void init(@NotNull @NonNls String fileText, @NotNull TestFileType type) throws IOException { configureFromFileText(getFileName(type), fileText); } @@ -52,18 +53,21 @@ public abstract class AbstractEditorProcessingOnDocumentModificationTest extends return getTestName(false) + type.getExtension(); } - protected static void addFoldRegion(final int startOffset, final int endOffset, final String placeholder) { + protected static FoldRegion addFoldRegion(final int startOffset, final int endOffset, final String placeholder) { + final FoldRegion[] result = new FoldRegion[1]; myEditor.getFoldingModel().runBatchFoldingOperation(new Runnable() { @Override public void run() { - myEditor.getFoldingModel().addFoldRegion(startOffset, endOffset, placeholder); + result[0] = myEditor.getFoldingModel().addFoldRegion(startOffset, endOffset, placeholder); } }); + return result[0]; } - protected static void addCollapsedFoldRegion(final int startOffset, final int endOffset, final String placeholder) { - addFoldRegion(startOffset, endOffset, placeholder); + protected static FoldRegion addCollapsedFoldRegion(final int startOffset, final int endOffset, final String placeholder) { + FoldRegion region = addFoldRegion(startOffset, endOffset, placeholder); toggleFoldRegionState(getFoldRegion(startOffset), false); + return region; } protected static void toggleFoldRegionState(final FoldRegion foldRegion, final boolean expanded) { @@ -200,10 +204,4 @@ public abstract class AbstractEditorProcessingOnDocumentModificationTest extends mapper.rawAdd(visualLine, startOffset, endOffset, startLogicalLine, startLogicalColumn, endLogicalLine, endLogicalColumn, endVisualColumn, foldRegions, tabData); } } - - public static void main(String[] args) { - setupFolding( - "FoldRegion +(0:600), FoldRegion +(640:1573), FoldRegion +(8130:8233), FoldRegion +(10189:10257), FoldRegion +(13582:13686), FoldRegion +(22805:22829), FoldRegion +(24069:24147), FoldRegion +(25024:25214), FoldRegion +(28313:28362), FoldRegion -(-1:-1)(invalid), FoldRegion -(-1:-1)(invalid), FoldRegion -(-1:-1)(invalid), FoldRegion -(-1:-1)(invalid)" - ); - } } diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java index 4966c5aca1c7..d2265d4254c4 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java @@ -595,7 +595,7 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest new CommentByLineCommentHandler().invoke(getProject(), getEditor(), getFile()); } - protected static void executeAction(@NotNull final String actionId) { + protected static void executeAction(@NonNls @NotNull final String actionId) { CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() { @Override public void run() { 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 45bfee429047..0b0c8ddddb1e 100644 --- a/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java +++ b/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java @@ -86,7 +86,7 @@ public class ContainerUtilRt { } @NotNull - public static LinkedList newLinkedList(T... elements) { + public static LinkedList newLinkedList(@NotNull T... elements) { final LinkedList list = newLinkedList(); Collections.addAll(list, elements); return list; @@ -103,7 +103,7 @@ public class ContainerUtilRt { } @NotNull - public static ArrayList newArrayList(T... elements) { + public static ArrayList newArrayList(@NotNull T... elements) { ArrayList list = newArrayListWithCapacity(elements.length); Collections.addAll(list, elements); return list; @@ -132,7 +132,8 @@ public class ContainerUtilRt { return 5 + size + size / 5; } - private static > C copy(C collection, @NotNull Iterable elements) { + @NotNull + private static > C copy(@NotNull C collection, @NotNull Iterable elements) { for (T element : elements) { collection.add(element); } @@ -145,7 +146,7 @@ public class ContainerUtilRt { } @NotNull - public static HashSet newHashSet(T... elements) { + public static HashSet newHashSet(@NotNull T... elements) { return new com.intellij.util.containers.HashSet(Arrays.asList(elements)); } @@ -171,7 +172,7 @@ public class ContainerUtilRt { } @NotNull - public static LinkedHashSet newLinkedHashSet(T... elements) { + public static LinkedHashSet newLinkedHashSet(@NotNull T... elements) { return newLinkedHashSet(Arrays.asList(elements)); } @@ -190,7 +191,7 @@ public class ContainerUtilRt { } @NotNull - public static TreeSet newTreeSet(T... elements) { + public static TreeSet newTreeSet(@NotNull T... elements) { TreeSet set = newTreeSet(); Collections.addAll(set, elements); return set; @@ -212,48 +213,62 @@ public class ContainerUtilRt { } @NotNull - public static Stack newStack(Collection elements) { + public static Stack newStack(@NotNull Collection elements) { return new Stack(elements); } @NotNull - public static Stack newStack(T... initial) { + public static Stack newStack(@NotNull T... initial) { return new Stack(Arrays.asList(initial)); } /** * Optimized toArray() as opposed to the {@link java.util.Collections#emptyList()}. */ - private static class EmptyList extends AbstractList implements RandomAccess { + private static class EmptyList extends AbstractList implements RandomAccess { private static final EmptyList INSTANCE = new EmptyList(); + @Override public int size() { return 0; } + @Override public boolean contains(Object obj) { return false; } - public Object get(int index) { + @Override + public T get(int index) { throw new IndexOutOfBoundsException("Index: " + index); } + @NotNull @Override public Object[] toArray() { return ArrayUtilRt.EMPTY_OBJECT_ARRAY; } + @NotNull @Override - public T[] toArray(T[] a) { + public T[] toArray(@NotNull T[] a) { + if (a.length != 0) { + a[0] = null; + } return a; } + + @NotNull + @Override + public Iterator iterator() { + return EmptyIterator.getInstance(); + } } @NotNull public static List emptyList() { - @SuppressWarnings({"unchecked"}) final List list = (List)EmptyList.INSTANCE; - return list; + //noinspection unchecked + return (List)EmptyList.INSTANCE; } @NotNull diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index 03fd44e0fbea..030124122e9f 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -42,7 +42,7 @@ public class ContainerUtil extends ContainerUtilRt { private static final int INSERTION_SORT_THRESHOLD = 10; @NotNull - public static T[] ar(T... elements) { + public static T[] ar(@NotNull T... elements) { return elements; } @@ -97,7 +97,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static LinkedList newLinkedList(T... elements) { + public static LinkedList newLinkedList(@NotNull T... elements) { return ContainerUtilRt.newLinkedList(elements); } @@ -112,7 +112,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static ArrayList newArrayList(E... array) { + public static ArrayList newArrayList(@NotNull E... array) { return ContainerUtilRt.newArrayList(array); } @@ -159,7 +159,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static HashSet newHashSet(T... elements) { + public static HashSet newHashSet(@NotNull T... elements) { return ContainerUtilRt.newHashSet(elements); } @@ -184,7 +184,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static LinkedHashSet newLinkedHashSet(T... elements) { + public static LinkedHashSet newLinkedHashSet(@NotNull T... elements) { return ContainerUtilRt.newLinkedHashSet(elements); } @@ -199,7 +199,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static THashSet newTroveSet(T... elements) { + public static THashSet newTroveSet(@NotNull T... elements) { return newTroveSet(Arrays.asList(elements)); } @@ -236,7 +236,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static TreeSet newTreeSet(T... elements) { + public static TreeSet newTreeSet(@NotNull T... elements) { return ContainerUtilRt.newTreeSet(elements); } @@ -250,7 +250,8 @@ public class ContainerUtil extends ContainerUtilRt { return new ConcurrentHashMap(); } - public static List reverse(final List elements) { + @NotNull + public static List reverse(@NotNull final List elements) { return new AbstractList() { @Override public E get(int index) { @@ -264,6 +265,7 @@ public class ContainerUtil extends ContainerUtilRt { }; } + @NotNull public static Map union(@NotNull Map map, @NotNull Map map2) { THashMap result = new THashMap(map.size() + map2.size()); result.putAll(map); @@ -271,6 +273,7 @@ public class ContainerUtil extends ContainerUtilRt { return result; } + @NotNull public static Set union(@NotNull Set set, @NotNull Set set2) { THashSet result = new THashSet(set.size() + set2.size()); result.addAll(set); @@ -278,24 +281,28 @@ public class ContainerUtil extends ContainerUtilRt { return result; } - public static Set immutableSet(final E ... elements) { + @NotNull + public static Set immutableSet(@NotNull E ... elements) { return Collections.unmodifiableSet(new THashSet(Arrays.asList(elements))); } - public static ImmutableList immutableList(E ... array) { + @NotNull + public static ImmutableList immutableList(@NotNull E ... array) { return new ImmutableListBackedByArray(array); } - public static ImmutableList immutableList(List list) { + @NotNull + public static ImmutableList immutableList(@NotNull List list) { return new ImmutableListBackedByList(list); } + @NotNull public static ImmutableMapBuilder immutableMapBuilder() { return new ImmutableMapBuilder(); } public static class ImmutableMapBuilder { - private THashMap myMap = new THashMap(); + private final Map myMap = new THashMap(); public ImmutableMapBuilder put(K key, V value) { myMap.put(key, value); @@ -310,7 +317,7 @@ public class ContainerUtil extends ContainerUtilRt { private static class ImmutableListBackedByList extends ImmutableList { private final List myStore; - private ImmutableListBackedByList(List list) { + private ImmutableListBackedByList(@NotNull List list) { myStore = list; } @@ -328,7 +335,7 @@ public class ContainerUtil extends ContainerUtilRt { private static class ImmutableListBackedByArray extends ImmutableList { private final E[] myStore; - private ImmutableListBackedByArray(E[] array) { + private ImmutableListBackedByArray(@NotNull E[] array) { myStore = array; } @@ -343,7 +350,8 @@ public class ContainerUtil extends ContainerUtilRt { } } - public static Map intersection(Map map1, Map map2) { + @NotNull + public static Map intersection(@NotNull Map map1, @NotNull Map map2) { final Map res = newHashMap(); final Set keys = newHashSet(); keys.addAll(map1.keySet()); @@ -351,14 +359,15 @@ public class ContainerUtil extends ContainerUtilRt { for (K k : keys) { V v1 = map1.get(k); V v2 = map2.get(k); - if (v1 == v2 || (v1 != null && v1.equals(v2))) { + if (v1 == v2 || v1 != null && v1.equals(v2)) { res.put(k, v1); } } return res; } - public static Map> diff(Map map1, Map map2) { + @NotNull + public static Map> diff(@NotNull Map map1, @NotNull Map map2) { final Map> res = newHashMap(); final Set keys = newHashSet(); keys.addAll(map1.keySet()); @@ -366,53 +375,70 @@ public class ContainerUtil extends ContainerUtilRt { for (K k : keys) { V v1 = map1.get(k); V v2 = map2.get(k); - if (!(v1 == v2 || (v1 != null && v1.equals(v2)))) { + if (!(v1 == v2 || v1 != null && v1.equals(v2))) { res.put(k, Pair.create(v1, v2)); } } return res; } - @NotNull - public static List mergeSortedLists(@NotNull List list1, @NotNull List list2, @NotNull Comparator comparator, boolean mergeEqualItems){ - List result = new ArrayList(list1.size() + list2.size()); - + public static boolean processSortedListsInOrder(@NotNull List list1, + @NotNull List list2, + @NotNull Comparator comparator, + boolean mergeEqualItems, + @NotNull Processor processor) { int index1 = 0; int index2 = 0; while (index1 < list1.size() || index2 < list2.size()) { + T e; if (index1 >= list1.size()) { - result.add(list2.get(index2++)); + e = list2.get(index2++); } else if (index2 >= list2.size()) { - result.add(list1.get(index1++)); + e = list1.get(index1++); } else { T element1 = list1.get(index1); T element2 = list2.get(index2); int c = comparator.compare(element1, element2); - if (c < 0) { - result.add(element1); + if (c <= 0) { + e = element1; index1++; } - else if (c > 0) { - result.add(element2); - index2++; - } else { - result.add(element1); - if (!mergeEqualItems) { - result.add(element2); - } - index1++; + e = element2; index2++; } + if (c == 0 && !mergeEqualItems) { + if (!processor.process(e)) return false; + index2++; + e = element2; + } } + if (!processor.process(e)) return false; } + return true; + } + + @NotNull + public static List mergeSortedLists(@NotNull List list1, + @NotNull List list2, + @NotNull Comparator comparator, + boolean mergeEqualItems) { + final List result = new ArrayList(list1.size() + list2.size()); + processSortedListsInOrder(list1, list2, comparator, mergeEqualItems, new Processor() { + @Override + public boolean process(T t) { + result.add(t); + return true; + } + }); return result; } + @NotNull - public static List mergeSortedArrays(@NotNull T[] list1, @NotNull T[] list2, @NotNull Comparator comparator, boolean mergeEqualItems, @Nullable Processor filter){ + public static List mergeSortedArrays(@NotNull T[] list1, @NotNull T[] list2, @NotNull Comparator comparator, boolean mergeEqualItems, @Nullable Processor filter) { int index1 = 0; int index2 = 0; List result = new ArrayList(list1.length + list2.length); @@ -462,7 +488,8 @@ public class ContainerUtil extends ContainerUtilRt { return result; } - public static List subList(List list, int from) { + @NotNull + public static List subList(@NotNull List list, int from) { return list.subList(from, list.size()); } @@ -624,6 +651,7 @@ public class ContainerUtil extends ContainerUtilRt { @Nullable public static T find(@NotNull Iterable iterable, final T equalTo) { return find(iterable, new Condition() { + @Override public boolean value(final T object) { return equalTo == object || equalTo.equals(object); } @@ -764,6 +792,7 @@ public class ContainerUtil extends ContainerUtilRt { } } + @NotNull public static Map stringMap(@NotNull final String... keyValues) { final Map result = newHashMap(); for (int i = 0; i < keyValues.length - 1; i+=2) { @@ -781,14 +810,17 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static Iterator iterate(@NotNull final Enumeration enumeration) { return new Iterator() { + @Override public boolean hasNext() { return enumeration.hasMoreElements(); } + @Override public T next() { return enumeration.nextElement(); } + @Override public void remove() { throw new UnsupportedOperationException(); } @@ -804,15 +836,18 @@ public class ContainerUtil extends ContainerUtilRt { public static Iterable iterate(@NotNull final Collection collection, @NotNull final Condition condition) { if (collection.isEmpty()) return emptyIterable(); return new Iterable() { + @Override public Iterator iterator() { return new Iterator() { Iterator impl = collection.iterator(); T next = findNext(); + @Override public boolean hasNext() { return next != null; } + @Override public T next() { T result = next; next = findNext(); @@ -830,6 +865,7 @@ public class ContainerUtil extends ContainerUtilRt { return null; } + @Override public void remove() { throw new UnsupportedOperationException(); } @@ -841,18 +877,22 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static Iterable iterateBackward(@NotNull final List list) { return new Iterable() { + @Override public Iterator iterator() { return new Iterator() { ListIterator it = list.listIterator(list.size()); + @Override public boolean hasNext() { return it.hasPrevious(); } + @Override public T next() { return it.previous(); } + @Override public void remove() { it.remove(); } @@ -880,6 +920,7 @@ public class ContainerUtil extends ContainerUtilRt { } } + @NotNull public static > C addAll(@NotNull C collection, @NotNull A... elements) { //noinspection ManualArrayToCollectionCopy for (T element : elements) { @@ -891,6 +932,7 @@ public class ContainerUtil extends ContainerUtilRt { /** * Adds all not-null elements from the {@code elements}, ignoring nulls */ + @NotNull public static > C addAllNotNull(@NotNull C collection, @NotNull A... elements) { //noinspection ManualArrayToCollectionCopy for (T element : elements) { @@ -936,6 +978,7 @@ public class ContainerUtil extends ContainerUtilRt { final int size = size1 + list2.size(); return new AbstractList() { + @Override public T get(int index) { if (index < size1) { return list1.get(index); @@ -944,6 +987,7 @@ public class ContainerUtil extends ContainerUtilRt { return list2.get(index - size1); } + @Override public int size() { return size; } @@ -953,6 +997,7 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static Iterable concat(@NotNull final Iterable... iterables) { return new Iterable() { + @Override public Iterator iterator() { Iterator[] iterators = new Iterator[iterables.length]; for (int i = 0, iterablesLength = iterables.length; i < iterablesLength; i++) { @@ -966,6 +1011,7 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static Iterable concat(@NotNull final T[]... iterables) { return new Iterable() { + @Override public Iterator iterator() { Iterator[] iterators = new Iterator[iterables.length]; for (int i = 0, iterablesLength = iterables.length; i < iterablesLength; i++) { @@ -985,6 +1031,7 @@ public class ContainerUtil extends ContainerUtilRt { } final int finalSize = size; return new AbstractList() { + @Override public T get(final int index) { if (index >= 0 && index < finalSize) { int from = 0; @@ -996,6 +1043,7 @@ public class ContainerUtil extends ContainerUtilRt { throw new IndexOutOfBoundsException("index: " + index + "size: " + size()); } + @Override public int size() { return finalSize; } @@ -1039,7 +1087,7 @@ public class ContainerUtil extends ContainerUtilRt { } @Nullable - public static T getFirstItem(final Collection items) { + public static T getFirstItem(@Nullable Collection items) { return getFirstItem(items, null); } @@ -1065,14 +1113,17 @@ public class ContainerUtil extends ContainerUtilRt { return set; } + @NotNull public static T[] toArray(@Nullable Collection c, @NotNull ArrayFactory factory) { return c != null ? c.toArray(factory.create(c.size())) : factory.create(0); } + @NotNull public static T[] toArray(@NotNull Collection c1, @NotNull Collection c2, @NotNull ArrayFactory factory) { return ArrayUtil.mergeCollections(c1, c2, factory); } + @NotNull public static T[] mergeCollectionsToArray(@NotNull Collection c1, @NotNull Collection c2, @NotNull ArrayFactory factory) { return ArrayUtil.mergeCollections(c1, c2, factory); } @@ -1219,9 +1270,11 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static List mapNotNull(@NotNull T[] array, Function mapping) { + public static List mapNotNull(@NotNull T[] array, @NotNull Function mapping) { return mapNotNull(Arrays.asList(array), mapping); } + + @NotNull public static V[] mapNotNull(@NotNull T[] arr, @NotNull Function mapping, @NotNull V[] emptyArray) { List result = new ArrayList(arr.length); for (T t : arr) { @@ -1234,7 +1287,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static List mapNotNull(Iterable iterable, Function mapping) { + public static List mapNotNull(@NotNull Iterable iterable, @NotNull Function mapping) { List result = new ArrayList(); for (T t : iterable) { final V o = mapping.fun(t); @@ -1244,8 +1297,9 @@ public class ContainerUtil extends ContainerUtilRt { } return result; } + @NotNull - public static List mapNotNull(Collection iterable, Function mapping) { + public static List mapNotNull(@NotNull Collection iterable, @NotNull Function mapping) { List result = new ArrayList(iterable.size()); for (T t : iterable) { final V o = mapping.fun(t); @@ -1284,7 +1338,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static Set set(T ... items) { + public static Set set(@NotNull T ... items) { return addAll(newHashSet(items)); } @@ -1297,6 +1351,7 @@ public class ContainerUtil extends ContainerUtilRt { public static void add(final T element, @NotNull final Collection result, @NotNull final Disposable parentDisposable) { if (result.add(element)) { Disposer.register(parentDisposable, new Disposable() { + @Override public void dispose() { result.remove(element); } @@ -1361,6 +1416,7 @@ public class ContainerUtil extends ContainerUtilRt { return false; } + @NotNull public static List unfold(@Nullable T t, @NotNull NullableFunction next) { if (t == null) return Collections.emptyList(); @@ -1473,10 +1529,10 @@ public class ContainerUtil extends ContainerUtilRt { * Merge sorted points, which are sorted by x and with equal x by y. * Result is put to x1 y1. */ - public static void mergeSortedArrays(TIntArrayList x1, - TIntArrayList y1, - TIntArrayList x2, - TIntArrayList y2) { + public static void mergeSortedArrays(@NotNull TIntArrayList x1, + @NotNull TIntArrayList y1, + @NotNull TIntArrayList x2, + @NotNull TIntArrayList y2) { TIntArrayList newX = new TIntArrayList(); TIntArrayList newY = new TIntArrayList(); @@ -1484,12 +1540,12 @@ public class ContainerUtil extends ContainerUtilRt { int j = 0; while (i < x1.size() && j < x2.size()) { - if (x1.get(i) < x2.get(j) || (x1.get(i) == x2.get(j) && y1.get(i) < y2.get(j))) { + if (x1.get(i) < x2.get(j) || x1.get(i) == x2.get(j) && y1.get(i) < y2.get(j)) { newX.add(x1.get(i)); newY.add(y1.get(i)); i++; } - else if (x1.get(i) > x2.get(j) || (x1.get(i) == x2.get(j) && y1.get(i) > y2.get(j))) { + else if (x1.get(i) > x2.get(j) || x1.get(i) == x2.get(j) && y1.get(i) > y2.get(j)) { newX.add(x2.get(j)); newY.add(y2.get(j)); j++; @@ -1523,72 +1579,91 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static Set singleton(final T o, @NotNull final TObjectHashingStrategy strategy) { return new Set() { + @Override public int size() { return 1; } + @Override public boolean isEmpty() { return false; } + @Override public boolean contains(Object elem) { return strategy.equals(o, (T)elem); } + @NotNull + @Override public Iterator iterator() { return new Iterator() { boolean atEnd; + @Override public boolean hasNext() { return !atEnd; } + @Override public T next() { if (atEnd) throw new NoSuchElementException(); atEnd = true; return o; } + @Override public void remove() { throw new IncorrectOperationException(); } }; } + @NotNull + @Override public Object[] toArray() { return new Object[]{o}; } + @NotNull + @Override public T[] toArray(T[] a) { assert a.length == 1; a[0] = (T)o; return a; } + @Override public boolean add(T t) { throw new IncorrectOperationException(); } + @Override public boolean remove(Object o) { throw new IncorrectOperationException(); } + @Override public boolean containsAll(Collection c) { return false; } + @Override public boolean addAll(Collection c) { throw new IncorrectOperationException(); } + @Override public boolean retainAll(Collection c) { throw new IncorrectOperationException(); } + @Override public boolean removeAll(Collection c) { throw new IncorrectOperationException(); } + @Override public void clear() { throw new IncorrectOperationException(); } @@ -1599,6 +1674,7 @@ public class ContainerUtil extends ContainerUtilRt { public static List flatten(@NotNull Collection[] collections) { return flatten(Arrays.asList(collections)); } + @NotNull public static List flatten(@NotNull Iterable> collections) { List result = new ArrayList(); @@ -1609,7 +1685,8 @@ public class ContainerUtil extends ContainerUtilRt { return result; } - public static V[] convert(K[] from, V[] to, Function fun) { + @NotNull + public static V[] convert(@NotNull K[] from, @NotNull V[] to, @NotNull Function fun) { if (to.length < from.length) { to = (V[])Array.newInstance(to.getClass().getComponentType(), from.length); } @@ -1619,7 +1696,7 @@ public class ContainerUtil extends ContainerUtilRt { return to; } - public static boolean containsIdentity(Iterable list, T element) { + public static boolean containsIdentity(@NotNull Iterable list, T element) { for (T t : list) { if (t == element) { return true; @@ -1628,7 +1705,7 @@ public class ContainerUtil extends ContainerUtilRt { return false; } - public static int indexOfIdentity(List list, T element) { + public static int indexOfIdentity(@NotNull List list, T element) { for (int i = 0, listSize = list.size(); i < listSize; i++) { if (list.get(i) == element) { return i; @@ -1638,7 +1715,7 @@ public class ContainerUtil extends ContainerUtilRt { } - public static boolean equalsIdentity(List list1, List list2) { + public static boolean equalsIdentity(@NotNull List list1, @NotNull List list2) { int listSize = list1.size(); if (list2.size() != listSize) { return false; @@ -1653,7 +1730,7 @@ public class ContainerUtil extends ContainerUtilRt { } - public static int indexOf(List list, Condition condition) { + public static int indexOf(@NotNull List list, @NotNull Condition condition) { for (int i = 0, listSize = list.size(); i < listSize; i++) { T t = list.get(i); if (condition.value(t)) { @@ -1663,7 +1740,8 @@ public class ContainerUtil extends ContainerUtilRt { return -1; } - public static Map reverseMap(Map map) { + @NotNull + public static Map reverseMap(@NotNull Map map) { final Map result = newHashMap(); for (A a : map.keySet()) { result.put(map.get(a), a); @@ -1671,7 +1749,7 @@ public class ContainerUtil extends ContainerUtilRt { return result; } - public static boolean processRecursively(final T root, final PairProcessor> processor) { + public static boolean processRecursively(final T root, @NotNull PairProcessor> processor) { final LinkedList list = new LinkedList(); list.add(root); while (!list.isEmpty()) { @@ -1699,12 +1777,12 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static Stack newStack(Collection initial) { + public static Stack newStack(@NotNull Collection initial) { return ContainerUtilRt.newStack(initial); } @NotNull - public static Stack newStack(T... initial) { + public static Stack newStack(@NotNull T... initial) { return ContainerUtilRt.newStack(initial); } @@ -1718,6 +1796,10 @@ public class ContainerUtil extends ContainerUtilRt { return ContainerUtilRt.createEmptyCOWList(); } + public static List createLockFreeCopyOnWriteList(@NotNull Collection c) { + return new CopyOnWriteArrayList(c); + } + public static void addIfNotNull(@Nullable T element, @NotNull Collection result) { ContainerUtilRt.addIfNotNull(element, result); } diff --git a/platform/util/src/com/intellij/util/containers/UnsafeWeakList.java b/platform/util/src/com/intellij/util/containers/UnsafeWeakList.java index 57d8ed8fc2a9..cca415694f6e 100644 --- a/platform/util/src/com/intellij/util/containers/UnsafeWeakList.java +++ b/platform/util/src/com/intellij/util/containers/UnsafeWeakList.java @@ -41,10 +41,12 @@ public class UnsafeWeakList extends AbstractList { myArray = array; } + @Override public T get(int index) { return myArray.get(index); } + @Override public boolean add(T element) { tryReduceCapacity(-1); myArray.add(element); @@ -63,24 +65,33 @@ public class UnsafeWeakList extends AbstractList { return true; } + @Override public void add(int index, T element) { tryReduceCapacity(-1); myArray.add(index, element); } + @Override public T remove(int index) { tryReduceCapacity(-1); return myArray.remove(index); } + @NotNull + @Override public Iterator iterator() { return new MyIterator(); } + @Override public int size() { return myArray.size(); } + public void clear(int index) { + myArray.removeReference(index); + } + public List toStrongList() { List result = new ArrayList(myArray.size()); myArray.toStrongCollection(result); @@ -134,10 +145,12 @@ public class UnsafeWeakList extends AbstractList { } } + @Override public boolean hasNext() { return myNextElement != null; } + @Override public T next() { if (modCount != myModCount) throw new ConcurrentModificationException(); if (myNextElement == null) throw new NoSuchElementException(); @@ -147,6 +160,7 @@ public class UnsafeWeakList extends AbstractList { return element; } + @Override public void remove() { if (myCurrentIndex == -1) throw new IllegalStateException(); myArray.remove(myCurrentIndex);