diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java index b5881c1d7768..0614771ba379 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/projectRoot/ModuleStructureConfigurable.java @@ -330,8 +330,7 @@ public class ModuleStructureConfigurable extends BaseStructureConfigurable imple return moduleStructureExtension.getNodeComparator(); } }); - comparators.add(NODE_COMPARATOR); - return new MergingComparator(comparators); + return new MergingComparator(ContainerUtil.concat(comparators, Collections.singletonList(NODE_COMPARATOR))); } @Override @@ -678,7 +677,7 @@ public class ModuleStructureConfigurable extends BaseStructureConfigurable imple extension.copy(configurable, TREE_UPDATER); } } - + private class MyDataProviderWrapper extends JPanel implements DataProvider { public MyDataProviderWrapper(final JComponent component) { super(new BorderLayout()); diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java index 952929641a9d..a5abf15f9744 100644 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java +++ b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DfaUtil.java @@ -232,13 +232,13 @@ public class DfaUtil { } @NotNull - public static Collection getVariableAssignmentsInFile(final PsiVariable psiVariable, + public static Collection getVariableAssignmentsInFile(@NotNull PsiVariable psiVariable, final boolean literalsOnly, final PsiElement place) { final Ref modificationRef = Ref.create(Boolean.FALSE); final PsiCodeBlock codeBlock = place == null? null : getTopmostBlockInSameClass(place); final int placeOffset = codeBlock != null? place.getTextRange().getStartOffset() : 0; - final List list = ContainerUtil.mapNotNull( + List list = ContainerUtil.mapNotNull( ReferencesSearch.search(psiVariable, new LocalSearchScope(new PsiElement[] {psiVariable.getContainingFile()}, null, true)).findAll(), new NullableFunction() { @Override @@ -272,8 +272,9 @@ public class DfaUtil { } }); if (modificationRef.get()) return Collections.emptyList(); - if (!literalsOnly || allOperandsAreLiterals(psiVariable.getInitializer())) { - ContainerUtil.addIfNotNull(psiVariable.getInitializer(), list); + PsiExpression initializer = psiVariable.getInitializer(); + if (initializer != null && (!literalsOnly || allOperandsAreLiterals(initializer))) { + list = ContainerUtil.concat(list, Collections.singletonList(initializer)); } return list; } 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 0b0c8ddddb1e..443f07e04246 100644 --- a/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java +++ b/platform/util-rt/src/com/intellij/util/containers/ContainerUtilRt.java @@ -289,28 +289,42 @@ public class ContainerUtilRt { } } + /** + * @return read-only list consisting of the elements from array converted by mapper + */ @NotNull public static List map2List(@NotNull T[] array, @NotNull Function mapper) { return map2List(Arrays.asList(array), mapper); } + /** + * @return read-only list consisting of the elements from collection converted by mapper + */ @NotNull public static List map2List(@NotNull Collection collection, @NotNull Function mapper) { - final ArrayList list = new ArrayList(collection.size()); + if (collection.isEmpty()) return emptyList(); + List list = new ArrayList(collection.size()); for (final T t : collection) { list.add(mapper.fun(t)); } return list; } + /** + * @return read-only set consisting of the elements from collection converted by mapper + */ @NotNull public static Set map2Set(@NotNull T[] collection, @NotNull Function mapper) { return map2Set(Arrays.asList(collection), mapper); } + /** + * @return read-only set consisting of the elements from collection converted by mapper + */ @NotNull public static Set map2Set(@NotNull Collection collection, @NotNull Function mapper) { - final HashSet set = new HashSet(collection.size()); + if (collection.isEmpty()) return Collections.emptySet(); + Set set = new HashSet(collection.size()); for (final T t : collection) { set.add(mapper.fun(t)); } @@ -326,9 +340,7 @@ public class ContainerUtilRt { } return array; } - else { - return collection.toArray(array); - } + return collection.toArray(array); } /** diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index 108c0e61c1ca..cd9ad7c55aff 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -171,7 +171,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static List newSmartList(T... elements) { + public static List newSmartList(@NotNull T... elements) { return new SmartList(elements); } @@ -216,7 +216,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static THashSet newTroveSet(TObjectHashingStrategy strategy) { + public static THashSet newTroveSet(@NotNull TObjectHashingStrategy strategy) { return new THashSet(strategy); } @@ -226,7 +226,7 @@ public class ContainerUtil extends ContainerUtilRt { } @NotNull - public static THashSet newTroveSet(TObjectHashingStrategy strategy, T... elements) { + public static THashSet newTroveSet(@NotNull TObjectHashingStrategy strategy, @NotNull T... elements) { return new THashSet(Arrays.asList(elements), strategy); } @@ -554,7 +554,7 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static List collect(@NotNull Iterator iterator) { - if (!iterator.hasNext()) return Collections.emptyList(); + if (!iterator.hasNext()) return emptyList(); List list = new ArrayList(); addAll(list, iterator); return list; @@ -1003,9 +1003,7 @@ public class ContainerUtil extends ContainerUtilRt { } public static U findInstance(@NotNull Iterator iterator, @NotNull Class aClass) { - // uncomment for 1.5 - //return (U)find(iterator, new FilteringIterator.InstanceOf(aClass)); - return (U)find(iterator, new FilteringIterator.InstanceOf((Class)aClass)); + return (U)find(iterator, new FilteringIterator.InstanceOf(aClass)); } @Nullable @@ -1018,15 +1016,21 @@ public class ContainerUtil extends ContainerUtilRt { return concat(Arrays.asList(array), fun); } + /** + * @return read-only list consisting of the elements from the collections stored in list added together + */ @NotNull public static List concat(@NotNull Iterable> list) { List result = new ArrayList(); for (final Collection ts : list) { result.addAll(ts); } - return result; + return result.isEmpty() ? Collections.emptyList() : result; } + /** + * @return read-only list consisting of the two lists added together + */ @NotNull public static List concat(@NotNull final List list1, @NotNull final List list2) { final int size1 = list1.size(); @@ -1089,12 +1093,16 @@ public class ContainerUtil extends ContainerUtilRt { }; } + /** + * @return read-only list consisting of the lists added together + */ @NotNull public static List concat(@NotNull final List... lists) { int size = 0; for (List each : lists) { size += each.size(); } + if (size == 0) return emptyList(); final int finalSize = size; return new AbstractList() { @Override @@ -1116,19 +1124,25 @@ public class ContainerUtil extends ContainerUtilRt { }; } + /** + * @return read-only list consisting of the lists added together + */ @NotNull public static List concat(@NotNull final List> lists) { List[] array = lists.toArray(new List[lists.size()]); return concat(array); } + /** + * @return read-only list consisting of the lists (made by listGenerator) added together + */ @NotNull - public static List concat(@NotNull Iterable list, @NotNull Function> fun) { - final ArrayList result = new ArrayList(); + public static List concat(@NotNull Iterable list, @NotNull Function> listGenerator) { + List result = new ArrayList(); for (final V v : list) { - result.addAll(fun.fun(v)); + result.addAll(listGenerator.fun(v)); } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } public static boolean intersects(@NotNull Collection collection1, @NotNull Collection collection2) { @@ -1141,15 +1155,18 @@ public class ContainerUtil extends ContainerUtilRt { return false; } + /** + * @return read-only collection consisting of elements from both collections + */ @NotNull public static Collection intersection(@NotNull Collection collection1, @NotNull Collection collection2) { - ArrayList result = new ArrayList(); + List result = new ArrayList(); for (T t : collection1) { if (collection2.contains(t)) { result.add(t); } } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } @Nullable @@ -1172,11 +1189,14 @@ public class ContainerUtil extends ContainerUtilRt { return res; } + /** + * @return read-only collection consisting of elements from the 'from' collection which are absent from the 'what' collection + */ @NotNull public static Collection subtract(@NotNull Collection from, @NotNull Collection what) { final Set set = newHashSet(from); set.removeAll(what); - return set; + return set.isEmpty() ? ContainerUtil.emptyList() : set; } @NotNull @@ -1318,16 +1338,24 @@ public class ContainerUtil extends ContainerUtilRt { } } + /** + * @return read-only list consisting of the elements from the iterable converted by mapping + */ @NotNull public static List map(@NotNull Iterable iterable, @NotNull Function mapping) { List result = new ArrayList(); for (T t : iterable) { result.add(mapping.fun(t)); } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } + + /** + * @return read-only list consisting of the elements from the iterable converted by mapping + */ @NotNull public static List map(@NotNull Collection iterable, @NotNull Function mapping) { + if (iterable.isEmpty()) return emptyList(); List result = new ArrayList(iterable.size()); for (T t : iterable) { result.add(mapping.fun(t)); @@ -1335,23 +1363,36 @@ public class ContainerUtil extends ContainerUtilRt { return result; } + /** + * @return read-only list consisting of the elements from the array converted by mapping with nulls filtered out + */ @NotNull public static List mapNotNull(@NotNull T[] array, @NotNull Function mapping) { return mapNotNull(Arrays.asList(array), mapping); } + /** + * @return read-only list consisting of the elements from the array converted by mapping with nulls filtered out + */ @NotNull - public static V[] mapNotNull(@NotNull T[] arr, @NotNull Function mapping, @NotNull V[] emptyArray) { - List result = new ArrayList(arr.length); - for (T t : arr) { + public static V[] mapNotNull(@NotNull T[] array, @NotNull Function mapping, @NotNull V[] emptyArray) { + List result = new ArrayList(array.length); + for (T t : array) { V v = mapping.fun(t); if (v != null) { result.add(v); } } + if (result.isEmpty()) { + assert emptyArray.length == 0 : "You must pass an empty array"; + return emptyArray; + } return result.toArray(emptyArray); } + /** + * @return read-only list consisting of the elements from the iterable converted by mapping with nulls filtered out + */ @NotNull public static List mapNotNull(@NotNull Iterable iterable, @NotNull Function mapping) { List result = new ArrayList(); @@ -1361,9 +1402,12 @@ public class ContainerUtil extends ContainerUtilRt { result.add(o); } } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } + /** + * @return read-only list consisting of the elements from the array converted by mapping with nulls filtered out + */ @NotNull public static List mapNotNull(@NotNull Collection iterable, @NotNull Function mapping) { List result = new ArrayList(iterable.size()); @@ -1373,29 +1417,40 @@ public class ContainerUtil extends ContainerUtilRt { result.add(o); } } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } + /** + * @return read-only list consisting of the elements with nulls filtered out + */ @NotNull public static List packNullables(@NotNull T... elements) { - ArrayList list = new ArrayList(); + List list = new ArrayList(); for (T element : elements) { addIfNotNull(element, list); } - return list; + return list.isEmpty() ? ContainerUtil.emptyList() : list; } + /** + * @return read-only list consisting of the elements from the array converted by mapping + */ @NotNull - public static List map(@NotNull T[] arr, @NotNull Function mapping) { - List result = new ArrayList(arr.length); - for (T t : arr) { + public static List map(@NotNull T[] array, @NotNull Function mapping) { + List result = new ArrayList(array.length); + for (T t : array) { result.add(mapping.fun(t)); } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } @NotNull public static V[] map(@NotNull T[] arr, @NotNull Function mapping, @NotNull V[] emptyArray) { + if (arr.length==0) { + assert emptyArray.length == 0 : "You must pass an empty array"; + return emptyArray; + } + List result = new ArrayList(arr.length); for (T t : arr) { result.add(mapping.fun(t)); @@ -1407,7 +1462,7 @@ public class ContainerUtil extends ContainerUtilRt { public static Set set(@NotNull T ... items) { return addAll(newHashSet(items)); } - + public static void putIfNotNull(final K key, @Nullable V value, @NotNull final Map result) { if (value != null) { result.put(key, value); @@ -1427,10 +1482,11 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static List createMaybeSingletonList(@Nullable T element) { - return element == null ? Collections.emptyList() : Collections.singletonList(element); + return element == null ? ContainerUtil.emptyList() : Collections.singletonList(element); } - public static V getOrCreate(@NotNull Map result, final T key, final V defaultValue) { + @NotNull + public static V getOrCreate(@NotNull Map result, final T key, @NotNull V defaultValue) { V value = result.get(key); if (value == null) { result.put(key, value = defaultValue); @@ -1438,7 +1494,7 @@ public class ContainerUtil extends ContainerUtilRt { return value; } - public static V getOrCreate(@NotNull Map result, final T key, @NotNull Factory factory) { + public static V getOrCreate(@NotNull Map result, final T key, @NotNull Factory factory) { V value = result.get(key); if (value == null) { result.put(key, value = factory.create()); @@ -1484,7 +1540,7 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static List unfold(@Nullable T t, @NotNull NullableFunction next) { - if (t == null) return Collections.emptyList(); + if (t == null) return emptyList(); final ArrayList list = new ArrayList(); while (t != null) { @@ -1642,6 +1698,10 @@ public class ContainerUtil extends ContainerUtilRt { y1.add(newY.toNativeArray()); } + + /** + * @return read-only set consisting of the only element o + */ @NotNull public static Set singleton(final T o, @NotNull final TObjectHashingStrategy strategy) { return new Set() { @@ -1736,11 +1796,17 @@ public class ContainerUtil extends ContainerUtilRt { }; } + /** + * @return read-only list consisting of the elements from all of the collections + */ @NotNull public static List flatten(@NotNull Collection[] collections) { return flatten(Arrays.asList(collections)); } + /** + * @return read-only list consisting of the elements from all of the collections + */ @NotNull public static List flatten(@NotNull Iterable> collections) { List result = new ArrayList(); @@ -1748,8 +1814,12 @@ public class ContainerUtil extends ContainerUtilRt { result.addAll(list); } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } + + /** + * @return read-only list consisting of the elements from all of the collections + */ @NotNull public static List flattenIterables(@NotNull Iterable> collections) { List result = new ArrayList(); @@ -1758,7 +1828,7 @@ public class ContainerUtil extends ContainerUtilRt { result.add(e); } } - return result; + return result.isEmpty() ? ContainerUtil.emptyList() : result; } @NotNull @@ -1838,7 +1908,7 @@ public class ContainerUtil extends ContainerUtilRt { @Nullable public static List trimToSize(@Nullable List list) { if (list == null) return null; - if (list.isEmpty()) return Collections.emptyList(); + if (list.isEmpty()) return emptyList(); if (list instanceof ArrayList) { ((ArrayList)list).trimToSize();