memory: do not allocate empty collections, javadoc

This commit is contained in:
Alexey Kudravtsev
2013-05-20 13:42:26 +04:00
parent e4986d61a5
commit bbb14498a7
4 changed files with 129 additions and 47 deletions
@@ -330,8 +330,7 @@ public class ModuleStructureConfigurable extends BaseStructureConfigurable imple
return moduleStructureExtension.getNodeComparator();
}
});
comparators.add(NODE_COMPARATOR);
return new MergingComparator<MyNode>(comparators);
return new MergingComparator<MyNode>(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());
@@ -232,13 +232,13 @@ public class DfaUtil {
}
@NotNull
public static Collection<PsiExpression> getVariableAssignmentsInFile(final PsiVariable psiVariable,
public static Collection<PsiExpression> getVariableAssignmentsInFile(@NotNull PsiVariable psiVariable,
final boolean literalsOnly,
final PsiElement place) {
final Ref<Boolean> modificationRef = Ref.create(Boolean.FALSE);
final PsiCodeBlock codeBlock = place == null? null : getTopmostBlockInSameClass(place);
final int placeOffset = codeBlock != null? place.getTextRange().getStartOffset() : 0;
final List<PsiExpression> list = ContainerUtil.mapNotNull(
List<PsiExpression> list = ContainerUtil.mapNotNull(
ReferencesSearch.search(psiVariable, new LocalSearchScope(new PsiElement[] {psiVariable.getContainingFile()}, null, true)).findAll(),
new NullableFunction<PsiReference, PsiExpression>() {
@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;
}
@@ -289,28 +289,42 @@ public class ContainerUtilRt {
}
}
/**
* @return read-only list consisting of the elements from array converted by mapper
*/
@NotNull
public static <T, V> List<V> map2List(@NotNull T[] array, @NotNull Function<T, V> mapper) {
return map2List(Arrays.asList(array), mapper);
}
/**
* @return read-only list consisting of the elements from collection converted by mapper
*/
@NotNull
public static <T, V> List<V> map2List(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) {
final ArrayList<V> list = new ArrayList<V>(collection.size());
if (collection.isEmpty()) return emptyList();
List<V> list = new ArrayList<V>(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 <T, V> Set<V> map2Set(@NotNull T[] collection, @NotNull Function<T, V> mapper) {
return map2Set(Arrays.asList(collection), mapper);
}
/**
* @return read-only set consisting of the elements from collection converted by mapper
*/
@NotNull
public static <T, V> Set<V> map2Set(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) {
final HashSet<V> set = new HashSet<V>(collection.size());
if (collection.isEmpty()) return Collections.emptySet();
Set <V> set = new HashSet<V>(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);
}
/**
@@ -171,7 +171,7 @@ public class ContainerUtil extends ContainerUtilRt {
}
@NotNull
public static <T> List<T> newSmartList(T... elements) {
public static <T> List<T> newSmartList(@NotNull T... elements) {
return new SmartList<T>(elements);
}
@@ -216,7 +216,7 @@ public class ContainerUtil extends ContainerUtilRt {
}
@NotNull
public static <T> THashSet<T> newTroveSet(TObjectHashingStrategy<T> strategy) {
public static <T> THashSet<T> newTroveSet(@NotNull TObjectHashingStrategy<T> strategy) {
return new THashSet<T>(strategy);
}
@@ -226,7 +226,7 @@ public class ContainerUtil extends ContainerUtilRt {
}
@NotNull
public static <T> THashSet<T> newTroveSet(TObjectHashingStrategy<T> strategy, T... elements) {
public static <T> THashSet<T> newTroveSet(@NotNull TObjectHashingStrategy<T> strategy, @NotNull T... elements) {
return new THashSet<T>(Arrays.asList(elements), strategy);
}
@@ -554,7 +554,7 @@ public class ContainerUtil extends ContainerUtilRt {
@NotNull
public static <T> List<T> collect(@NotNull Iterator<T> iterator) {
if (!iterator.hasNext()) return Collections.emptyList();
if (!iterator.hasNext()) return emptyList();
List<T> list = new ArrayList<T>();
addAll(list, iterator);
return list;
@@ -1003,9 +1003,7 @@ public class ContainerUtil extends ContainerUtilRt {
}
public static <T, U extends T> U findInstance(@NotNull Iterator<T> iterator, @NotNull Class<U> aClass) {
// uncomment for 1.5
//return (U)find(iterator, new FilteringIterator.InstanceOf<U>(aClass));
return (U)find(iterator, new FilteringIterator.InstanceOf<T>((Class<T>)aClass));
return (U)find(iterator, new FilteringIterator.InstanceOf<U>(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 <T> List<T> concat(@NotNull Iterable<? extends Collection<T>> list) {
List<T> result = new ArrayList<T>();
for (final Collection<T> ts : list) {
result.addAll(ts);
}
return result;
return result.isEmpty() ? Collections.<T>emptyList() : result;
}
/**
* @return read-only list consisting of the two lists added together
*/
@NotNull
public static <T> List<T> concat(@NotNull final List<? extends T> list1, @NotNull final List<? extends T> 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 <T> List<T> concat(@NotNull final List<? extends T>... lists) {
int size = 0;
for (List<? extends T> each : lists) {
size += each.size();
}
if (size == 0) return emptyList();
final int finalSize = size;
return new AbstractList<T>() {
@Override
@@ -1116,19 +1124,25 @@ public class ContainerUtil extends ContainerUtilRt {
};
}
/**
* @return read-only list consisting of the lists added together
*/
@NotNull
public static <T> List<T> concat(@NotNull final List<List<? extends T>> lists) {
List<? extends T>[] 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 <T, V> List<T> concat(@NotNull Iterable<? extends V> list, @NotNull Function<V, Collection<? extends T>> fun) {
final ArrayList<T> result = new ArrayList<T>();
public static <T, V> List<T> concat(@NotNull Iterable<? extends V> list, @NotNull Function<V, Collection<? extends T>> listGenerator) {
List<T> result = new ArrayList<T>();
for (final V v : list) {
result.addAll(fun.fun(v));
result.addAll(listGenerator.fun(v));
}
return result;
return result.isEmpty() ? ContainerUtil.<T>emptyList() : result;
}
public static <T> boolean intersects(@NotNull Collection<? extends T> collection1, @NotNull Collection<? extends T> 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 <T> Collection<T> intersection(@NotNull Collection<? extends T> collection1, @NotNull Collection<? extends T> collection2) {
ArrayList<T> result = new ArrayList<T>();
List<T> result = new ArrayList<T>();
for (T t : collection1) {
if (collection2.contains(t)) {
result.add(t);
}
}
return result;
return result.isEmpty() ? ContainerUtil.<T>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 <T> Collection<T> subtract(@NotNull Collection<T> from, @NotNull Collection<T> what) {
final Set<T> set = newHashSet(from);
set.removeAll(what);
return set;
return set.isEmpty() ? ContainerUtil.<T>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 <T,V> List<V> map(@NotNull Iterable<? extends T> iterable, @NotNull Function<T, V> mapping) {
List<V> result = new ArrayList<V>();
for (T t : iterable) {
result.add(mapping.fun(t));
}
return result;
return result.isEmpty() ? ContainerUtil.<V>emptyList() : result;
}
/**
* @return read-only list consisting of the elements from the iterable converted by mapping
*/
@NotNull
public static <T,V> List<V> map(@NotNull Collection<? extends T> iterable, @NotNull Function<T, V> mapping) {
if (iterable.isEmpty()) return emptyList();
List<V> result = new ArrayList<V>(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 <T, V> List<V> mapNotNull(@NotNull T[] array, @NotNull Function<T, V> 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 <T, V> V[] mapNotNull(@NotNull T[] arr, @NotNull Function<T, V> mapping, @NotNull V[] emptyArray) {
List<V> result = new ArrayList<V>(arr.length);
for (T t : arr) {
public static <T, V> V[] mapNotNull(@NotNull T[] array, @NotNull Function<T, V> mapping, @NotNull V[] emptyArray) {
List<V> result = new ArrayList<V>(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 <T, V> List<V> mapNotNull(@NotNull Iterable<? extends T> iterable, @NotNull Function<T, V> mapping) {
List<V> result = new ArrayList<V>();
@@ -1361,9 +1402,12 @@ public class ContainerUtil extends ContainerUtilRt {
result.add(o);
}
}
return result;
return result.isEmpty() ? ContainerUtil.<V>emptyList() : result;
}
/**
* @return read-only list consisting of the elements from the array converted by mapping with nulls filtered out
*/
@NotNull
public static <T, V> List<V> mapNotNull(@NotNull Collection<? extends T> iterable, @NotNull Function<T, V> mapping) {
List<V> result = new ArrayList<V>(iterable.size());
@@ -1373,29 +1417,40 @@ public class ContainerUtil extends ContainerUtilRt {
result.add(o);
}
}
return result;
return result.isEmpty() ? ContainerUtil.<V>emptyList() : result;
}
/**
* @return read-only list consisting of the elements with nulls filtered out
*/
@NotNull
public static <T> List<T> packNullables(@NotNull T... elements) {
ArrayList<T> list = new ArrayList<T>();
List<T> list = new ArrayList<T>();
for (T element : elements) {
addIfNotNull(element, list);
}
return list;
return list.isEmpty() ? ContainerUtil.<T>emptyList() : list;
}
/**
* @return read-only list consisting of the elements from the array converted by mapping
*/
@NotNull
public static <T, V> List<V> map(@NotNull T[] arr, @NotNull Function<T, V> mapping) {
List<V> result = new ArrayList<V>(arr.length);
for (T t : arr) {
public static <T, V> List<V> map(@NotNull T[] array, @NotNull Function<T, V> mapping) {
List<V> result = new ArrayList<V>(array.length);
for (T t : array) {
result.add(mapping.fun(t));
}
return result;
return result.isEmpty() ? ContainerUtil.<V>emptyList() : result;
}
@NotNull
public static <T, V> V[] map(@NotNull T[] arr, @NotNull Function<T, V> mapping, @NotNull V[] emptyArray) {
if (arr.length==0) {
assert emptyArray.length == 0 : "You must pass an empty array";
return emptyArray;
}
List<V> result = new ArrayList<V>(arr.length);
for (T t : arr) {
result.add(mapping.fun(t));
@@ -1407,7 +1462,7 @@ public class ContainerUtil extends ContainerUtilRt {
public static <T> Set<T> set(@NotNull T ... items) {
return addAll(newHashSet(items));
}
public static <K, V> void putIfNotNull(final K key, @Nullable V value, @NotNull final Map<K, V> result) {
if (value != null) {
result.put(key, value);
@@ -1427,10 +1482,11 @@ public class ContainerUtil extends ContainerUtilRt {
@NotNull
public static <T> List<T> createMaybeSingletonList(@Nullable T element) {
return element == null ? Collections.<T>emptyList() : Collections.singletonList(element);
return element == null ? ContainerUtil.<T>emptyList() : Collections.singletonList(element);
}
public static <T, V> V getOrCreate(@NotNull Map<T, V> result, final T key, final V defaultValue) {
@NotNull
public static <T, V> V getOrCreate(@NotNull Map<T, V> 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 <T, V> V getOrCreate(@NotNull Map<T, V> result, final T key, @NotNull Factory<V> factory) {
public static <T, V> V getOrCreate(@NotNull Map<T, V> result, final T key, @NotNull Factory<V> 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 <T> List<T> unfold(@Nullable T t, @NotNull NullableFunction<T, T> next) {
if (t == null) return Collections.emptyList();
if (t == null) return emptyList();
final ArrayList<T> list = new ArrayList<T>();
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 <T> Set<T> singleton(final T o, @NotNull final TObjectHashingStrategy<T> strategy) {
return new Set<T>() {
@@ -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 <E> List<E> flatten(@NotNull Collection<E>[] collections) {
return flatten(Arrays.asList(collections));
}
/**
* @return read-only list consisting of the elements from all of the collections
*/
@NotNull
public static <E> List<E> flatten(@NotNull Iterable<? extends Collection<E>> collections) {
List<E> result = new ArrayList<E>();
@@ -1748,8 +1814,12 @@ public class ContainerUtil extends ContainerUtilRt {
result.addAll(list);
}
return result;
return result.isEmpty() ? ContainerUtil.<E>emptyList() : result;
}
/**
* @return read-only list consisting of the elements from all of the collections
*/
@NotNull
public static <E> List<E> flattenIterables(@NotNull Iterable<? extends Iterable<E>> collections) {
List<E> result = new ArrayList<E>();
@@ -1758,7 +1828,7 @@ public class ContainerUtil extends ContainerUtilRt {
result.add(e);
}
}
return result;
return result.isEmpty() ? ContainerUtil.<E>emptyList() : result;
}
@NotNull
@@ -1838,7 +1908,7 @@ public class ContainerUtil extends ContainerUtilRt {
@Nullable
public static <T> List<T> trimToSize(@Nullable List<T> list) {
if (list == null) return null;
if (list.isEmpty()) return Collections.emptyList();
if (list.isEmpty()) return emptyList();
if (list instanceof ArrayList) {
((ArrayList)list).trimToSize();