2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.containers;
18 import com.intellij.openapi.Disposable;
19 import com.intellij.openapi.util.Condition;
20 import com.intellij.openapi.util.Disposer;
21 import com.intellij.openapi.util.Factory;
22 import com.intellij.util.*;
23 import gnu.trove.TIntArrayList;
24 import gnu.trove.TObjectHashingStrategy;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
28 import java.io.Serializable;
29 import java.lang.reflect.Array;
31 import java.util.concurrent.CopyOnWriteArrayList;
33 @SuppressWarnings({"UtilityClassWithoutPrivateConstructor", "unchecked"})
34 public class ContainerUtil {
35 private static final int INSERTION_SORT_THRESHOLD = 10;
38 public static <T> List<T> mergeSortedLists(@NotNull List<T> list1, @NotNull List<T> list2, @NotNull Comparator<? super T> comparator, boolean mergeEqualItems){
39 List<T> result = new ArrayList<T>(list1.size() + list2.size());
43 while (index1 < list1.size() || index2 < list2.size()) {
44 if (index1 >= list1.size()) {
45 result.add(list2.get(index2++));
47 else if (index2 >= list2.size()) {
48 result.add(list1.get(index1++));
51 T element1 = list1.get(index1);
52 T element2 = list2.get(index2);
53 int c = comparator.compare(element1, element2);
64 if (!mergeEqualItems) {
76 public static <T> List<T> mergeSortedArrays(@NotNull T[] list1, @NotNull T[] list2, @NotNull Comparator<? super T> comparator, boolean mergeEqualItems, @Nullable Processor<? super T> filter){
79 List<T> result = new ArrayList<T>(list1.length + list2.length);
81 while (index1 < list1.length || index2 < list2.length) {
82 if (index1 >= list1.length) {
83 T t = list2[index2++];
84 if (filter != null && !filter.process(t)) continue;
87 else if (index2 >= list2.length) {
88 T t = list1[index1++];
89 if (filter != null && !filter.process(t)) continue;
93 T element1 = list1[index1];
94 if (filter != null && !filter.process(element1)) {
98 T element2 = list2[index2];
99 if (filter != null && !filter.process(element2)) {
103 int c = comparator.compare(element1, element2);
105 result.add(element1);
109 result.add(element2);
113 result.add(element1);
114 if (!mergeEqualItems) {
115 result.add(element2);
126 public static <T> void addAll(@NotNull Collection<T> collection, @NotNull Iterable<T> appendix) {
127 addAll(collection, appendix.iterator());
130 public static <T> void addAll(@NotNull Collection<T> collection, @NotNull Iterator<T> iterator) {
131 while (iterator.hasNext()) {
132 T o = iterator.next();
137 public static <T> ArrayList<T> collect(@NotNull Iterator<T> iterator) {
138 ArrayList<T> list = new ArrayList<T>();
139 addAll(list, iterator);
144 public static <T> HashSet<T> collectSet(@NotNull Iterator<T> iterator) {
145 HashSet<T> hashSet = new HashSet<T>();
146 addAll(hashSet, iterator);
151 public static <K, V> HashMap<K, V> assignKeys(@NotNull Iterator<V> iterator, @NotNull Convertor<V, K> keyConvertor) {
152 HashMap<K, V> hashMap = new HashMap<K, V>();
153 while (iterator.hasNext()) {
154 V value = iterator.next();
155 hashMap.put(keyConvertor.convert(value), value);
161 public static <K, V> Map<K, Set<V>> classify(@NotNull Iterator<V> iterator, @NotNull Convertor<V, K> keyConvertor) {
162 Map<K, Set<V>> hashMap = new LinkedHashMap<K, Set<V>>();
163 while (iterator.hasNext()) {
164 V value = iterator.next();
165 final K key = keyConvertor.convert(value);
166 Set<V> set = hashMap.get(key);
168 hashMap.put(key, set = new LinkedHashSet<V>()); // ordered set!!
176 public static <K, V> HashMap<K, V> assignValues(@NotNull Iterator<K> iterator, @NotNull Convertor<K, V> valueConvertor) {
177 HashMap<K, V> hashMap = new HashMap<K, V>();
178 while (iterator.hasNext()) {
179 K key = iterator.next();
180 hashMap.put(key, valueConvertor.convert(key));
186 public static <T> Iterator<T> emptyIterator() {
187 return EmptyIterator.getInstance();
191 public static <T> Iterable<T> emptyIterable() {
192 return EmptyIterable.getInstance();
196 public static <T> T find(@NotNull T[] array, @NotNull Condition<T> condition) {
197 for (T element : array) {
198 if (condition.value(element)) return element;
203 public static <T> boolean process(@NotNull Iterable<? extends T> iterable, @NotNull Processor<T> processor) {
204 for (final T t : iterable) {
205 if (!processor.process(t)) {
212 public static <T> boolean process(@NotNull T[] iterable, @NotNull Processor<? super T> processor) {
213 for (final T t : iterable) {
214 if (!processor.process(t)) {
222 public static <T, V extends T> V find(@NotNull Iterable<V> iterable, @NotNull Condition<T> condition) {
223 return find(iterable.iterator(), condition);
227 public static <T> T find(@NotNull Iterable<? extends T> iterable, final T equalTo) {
228 return find(iterable, new Condition<T>() {
229 public boolean value(final T object) {
230 return equalTo == object || equalTo.equals(object);
236 public static <T, V extends T> V find(@NotNull Iterator<V> iterator, @NotNull Condition<T> condition) {
237 while (iterator.hasNext()) {
238 V value = iterator.next();
239 if (condition.value(value)) return value;
245 public static <T, V> List<V> map2List(@NotNull T[] array, @NotNull Function<T, V> mapper) {
246 return map2List(Arrays.asList(array), mapper);
250 public static <T, V> List<V> map2List(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) {
251 final ArrayList<V> list = new ArrayList<V>(collection.size());
252 for (final T t : collection) {
253 list.add(mapper.fun(t));
259 public static <T, V> Set<V> map2Set(@NotNull T[] collection, @NotNull Function<T, V> mapper) {
260 return map2Set(Arrays.asList(collection), mapper);
264 public static <T, V> Set<V> map2Set(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) {
265 final HashSet<V> set = new HashSet<V>(collection.size());
266 for (final T t : collection) {
267 set.add(mapper.fun(t));
273 public static <T> Object[] map2Array(@NotNull T[] array, @NotNull Function<T, Object> mapper) {
274 return map2Array(array, Object.class, mapper);
278 public static <T> Object[] map2Array(@NotNull Collection<T> array, @NotNull Function<T, Object> mapper) {
279 return map2Array(array, Object.class, mapper);
283 public static <T, V> V[] map2Array(@NotNull T[] array, @NotNull Class<? extends V> aClass, @NotNull Function<T, V> mapper) {
284 return map2Array(Arrays.asList(array), aClass, mapper);
288 public static <T, V> V[] map2Array(@NotNull Collection<? extends T> collection, @NotNull Class<? extends V> aClass, @NotNull Function<T, V> mapper) {
289 final List<V> list = map2List(collection, mapper);
290 return list.toArray((V[])Array.newInstance(aClass, list.size()));
294 public static <T, V> V[] map2Array(@NotNull Collection<? extends T> collection, @NotNull V[] to, @NotNull Function<T, V> mapper) {
295 return map2List(collection, mapper).toArray(to);
299 public static <T> List<T> filter(@NotNull T[] collection, @NotNull Condition<? super T> condition) {
300 return findAll(collection, condition);
304 public static <T> List<T> findAll(@NotNull T[] collection, @NotNull Condition<? super T> condition) {
305 return findAll(Arrays.asList(collection), condition);
309 public static <T> List<T> filter(@NotNull Collection<? extends T> collection, @NotNull Condition<? super T> condition) {
310 return findAll(collection, condition);
314 public static <T> List<T> findAll(@NotNull Collection<? extends T> collection, @NotNull Condition<? super T> condition) {
315 final ArrayList<T> result = new ArrayList<T>();
316 for (final T t : collection) {
317 if (condition.value(t)) {
325 public static <T> List<T> skipNulls(@NotNull Collection<? extends T> collection) {
326 return findAll(collection, Condition.NOT_NULL);
330 public static <T, V> List<V> findAll(@NotNull T[] collection, @NotNull Class<V> instanceOf) {
331 return findAll(Arrays.asList(collection), instanceOf);
335 public static <T, V> V[] findAllAsArray(@NotNull T[] collection, @NotNull Class<V> instanceOf) {
336 List<V> list = findAll(Arrays.asList(collection), instanceOf);
337 return list.toArray((V[])Array.newInstance(instanceOf, list.size()));
341 public static <T, V> V[] findAllAsArray(@NotNull Collection<? extends T> collection, @NotNull Class<V> instanceOf) {
342 List<V> list = findAll(collection, instanceOf);
343 return list.toArray((V[])Array.newInstance(instanceOf, list.size()));
347 public static <T> T[] findAllAsArray(@NotNull T[] collection, @NotNull Condition<? super T> instanceOf) {
348 List<T> list = findAll(collection, instanceOf);
349 return list.toArray((T[])Array.newInstance(collection.getClass().getComponentType(), list.size()));
353 public static <T, V> List<V> findAll(@NotNull Collection<? extends T> collection, @NotNull Class<V> instanceOf) {
354 final ArrayList<V> result = new ArrayList<V>();
355 for (final T t : collection) {
356 if (instanceOf.isInstance(t)) {
363 public static <T> void removeDuplicates(@NotNull Collection<T> collection) {
364 Set<T> collected = new HashSet<T>();
365 for (Iterator<T> iterator = collection.iterator(); iterator.hasNext();) {
366 T t = iterator.next();
367 if (!collected.contains(t)) {
376 public static Map<String, String> stringMap(@NotNull final String... keyValues) {
377 final Map<String, String> result = new HashMap<String, String>();
378 for (int i = 0; i < keyValues.length - 1; i+=2) {
379 result.put(keyValues[i], keyValues[i+1]);
386 public static <T> Iterator<T> iterate(@NotNull T[] arrays) {
387 return Arrays.asList(arrays).iterator();
391 public static <T> Iterator<T> iterate(@NotNull final Enumeration<T> enumeration) {
392 return new Iterator<T>() {
393 public boolean hasNext() {
394 return enumeration.hasMoreElements();
398 return enumeration.nextElement();
401 public void remove() {
402 throw new UnsupportedOperationException();
408 public static <T> Iterable<T> iterate(@NotNull T[] arrays, @NotNull Condition<? super T> condition) {
409 return iterate(Arrays.asList(arrays), condition);
413 public static <T> Iterable<T> iterate(@NotNull final Collection<? extends T> collection, @NotNull final Condition<? super T> condition) {
414 if (collection.isEmpty()) return emptyIterable();
415 return new Iterable<T>() {
416 public Iterator<T> iterator() {
417 return new Iterator<T>() {
418 Iterator<? extends T> impl = collection.iterator();
421 public boolean hasNext() {
431 private T findNext() {
432 while (impl.hasNext()) {
433 T each = impl.next();
434 if (condition.value(each)) {
441 public void remove() {
442 throw new UnsupportedOperationException();
450 public static <T> Iterable<T> iterateBackward(@NotNull final List<? extends T> list) {
451 return new Iterable<T>() {
452 public Iterator<T> iterator() {
453 return new Iterator<T>() {
454 ListIterator<? extends T> it = list.listIterator(list.size());
456 public boolean hasNext() {
457 return it.hasPrevious();
461 return it.previous();
464 public void remove() {
472 public static <E> void swapElements(@NotNull List<E> list, int index1, int index2) {
473 E e1 = list.get(index1);
474 E e2 = list.get(index2);
475 list.set(index1, e2);
476 list.set(index2, e1);
480 public static <T> ArrayList<T> collect(@NotNull Iterator<?> iterator, @NotNull FilteringIterator.InstanceOf<T> instanceOf) {
481 return collect(FilteringIterator.create((Iterator<T>)iterator, instanceOf));
484 public static <T> void addAll(@NotNull Collection<T> collection, @NotNull Enumeration<T> enumeration) {
485 while (enumeration.hasMoreElements()) {
486 T element = enumeration.nextElement();
487 collection.add(element);
491 public static <T> Collection<T> addAll(@NotNull Collection<T> collection, @NotNull T... elements) {
492 //noinspection ManualArrayToCollectionCopy
493 for (T element : elements) {
494 collection.add(element);
499 public static <T, U extends T> U findInstance(@NotNull Iterable<T> iterable, @NotNull Class<U> aClass) {
500 return findInstance(iterable.iterator(), aClass);
503 public static <T, U extends T> U findInstance(@NotNull Iterator<T> iterator, @NotNull Class<U> aClass) {
505 //return (U)find(iterator, new FilteringIterator.InstanceOf<U>(aClass));
506 return (U)find(iterator, new FilteringIterator.InstanceOf<T>((Class<T>)aClass));
510 public static <T, U extends T> U findInstance(@NotNull T[] array, @NotNull Class<U> aClass) {
511 return findInstance(Arrays.asList(array), aClass);
515 public static <T, V> List<T> concat(@NotNull V[] array, @NotNull Function<V, Collection<? extends T>> fun) {
516 return concat(Arrays.asList(array), fun);
520 public static <T> List<T> concat(@NotNull Iterable<? extends Collection<T>> list) {
521 List<T> result = new ArrayList<T>();
522 for (final Collection<T> ts : list) {
529 public static <T> List<T> concat(@NotNull final List<? extends T> list1, @NotNull final List<? extends T> list2) {
530 return concat(new List[]{list1, list2});
534 public static <T> Iterable<T> concat(@NotNull final Iterable<? extends T>... iterables) {
535 return new Iterable<T>() {
536 public Iterator<T> iterator() {
537 Iterator[] iterators = new Iterator[iterables.length];
538 for (int i = 0, iterablesLength = iterables.length; i < iterablesLength; i++) {
539 Iterable<? extends T> iterable = iterables[i];
540 iterators[i] = iterable.iterator();
542 return new SequenceIterator<T>(iterators);
548 public static <T> List<T> concat(@NotNull final List<List<? extends T>> lists) {
550 for (List<? extends T> each : lists) {
553 final int finalSize = size;
554 return new AbstractList<T>() {
555 public T get(final int index) {
556 if (index >= 0 && index < finalSize) {
558 for (List<? extends T> each : lists) {
559 if (from <= index && index < from + each.size()) return each.get(index - from);
563 throw new IndexOutOfBoundsException("index: " + index + "size: " + size());
573 public static <T> List<T> concat(@NotNull final List<? extends T>... lists) {
574 return concat(Arrays.asList(lists));
578 public static <T, V> List<T> concat(@NotNull Iterable<? extends V> list, @NotNull Function<V, Collection<? extends T>> fun) {
579 final ArrayList<T> result = new ArrayList<T>();
580 for (final V v : list) {
581 result.addAll(fun.fun(v));
586 public static <T> boolean intersects(@NotNull Collection<? extends T> collection1, @NotNull Collection<? extends T> collection2) {
587 for (T t : collection1) {
588 //noinspection SuspiciousMethodCalls
589 if (collection2.contains(t)) {
596 public static <T> T getFirstItem(final Collection<T> items, final T def) {
597 return items == null || items.isEmpty() ? def : items.iterator().next();
601 public static <T> Collection<T> subtract(@NotNull Collection<T> from, @NotNull Collection<T> what) {
602 final HashSet<T> set = new HashSet<T>(from);
608 public static <T> T[] toArray(@NotNull List<T> collection, @NotNull T[] array) {
609 final int length = array.length;
611 for (int i = 0; i < collection.size(); i++) {
612 array[i] = collection.get(i);
617 return collection.toArray(array);
622 * This is a replacement for {@link Collection#toArray(Object[])}. For small collections it is faster to stay at java level and refrain
623 * from calling JNI {@link System#arraycopy(Object, int, Object, int, int)}
626 public static <T> T[] toArray(@NotNull Collection<T> c, @NotNull T[] sample) {
627 final int size = c.size();
628 if (size == sample.length && size < 20) {
636 return c.toArray(sample);
639 public static <T extends Comparable<T>> void sort(@NotNull List<T> list) {
640 int size = list.size();
642 if (size < 2) return;
647 if (t0.compareTo(t1) > 0) {
652 else if (size < INSERTION_SORT_THRESHOLD) {
653 for (int i = 0; i < size; i++) {
654 for (int j = 0; j < i; j++) {
658 if (ti.compareTo(tj) < 0) {
666 Collections.sort(list);
670 public static <T> void sort(@NotNull List<T> list, @NotNull Comparator<T> comparator) {
671 int size = list.size();
673 if (size < 2) return;
678 if (comparator.compare(t0, t1) > 0) {
683 else if (size < INSERTION_SORT_THRESHOLD) {
684 for (int i = 0; i < size; i++) {
685 for (int j = 0; j < i; j++) {
689 if (comparator.compare(ti, tj) < 0) {
697 Collections.sort(list, comparator);
701 public static <T extends Comparable<T>> void sort(@NotNull T[] a) {
704 if (size < 2) return;
709 if (t0.compareTo(t1) > 0) {
714 else if (size < INSERTION_SORT_THRESHOLD) {
715 for (int i = 0; i < size; i++) {
716 for (int j = 0; j < i; j++) {
720 if (ti.compareTo(tj) < 0) {
732 public static <T> void sort(@NotNull T[] a, @NotNull Comparator<T> comparator) {
735 if (size < 2) return;
740 if (comparator.compare(t0, t1) > 0) {
745 else if (size < INSERTION_SORT_THRESHOLD) {
746 for (int i = 0; i < size; i++) {
747 for (int j = 0; j < i; j++) {
751 if (comparator.compare(ti, tj) < 0) {
759 Arrays.sort(a, comparator);
764 public static <T,V> List<V> map(@NotNull Iterable<? extends T> iterable, @NotNull Function<T, V> mapping) {
765 List<V> result = new ArrayList<V>();
766 for (T t : iterable) {
767 result.add(mapping.fun(t));
773 public static <T, V> List<V> mapNotNull(@NotNull T[] array, Function<T, V> mapping) {
774 return mapNotNull(Arrays.asList(array), mapping);
778 public static <T, V> List<V> mapNotNull(Iterable<? extends T> iterable, Function<T, V> mapping) {
779 List<V> result = new ArrayList<V>();
780 for (T t : iterable) {
781 final V o = mapping.fun(t);
790 public static <T> List<T> packNullables(@NotNull T... elements) {
791 ArrayList<T> list = new ArrayList<T>();
792 for (T element : elements) {
793 addIfNotNull(element, list);
799 public static <T, V> List<V> map(@NotNull T[] arr, @NotNull Function<T, V> mapping) {
800 List<V> result = new ArrayList<V>(arr.length);
802 result.add(mapping.fun(t));
808 public static <T, V> V[] map(@NotNull T[] arr, @NotNull Function<T, V> mapping, @NotNull V[] emptyArray) {
809 List<V> result = new ArrayList<V>(arr.length);
811 result.add(mapping.fun(t));
813 return result.toArray(emptyArray);
816 public static <T> void addIfNotNull(final T element, @NotNull Collection<T> result) {
817 if (element != null) {
821 public static <T> void addIfNotNull(@NotNull Collection<T> result, @Nullable final T element) {
822 if (element != null) {
827 public static <K, V> void putIfNotNull(final K key, @Nullable V value, @NotNull final Map<K, V> result) {
829 result.put(key, value);
833 public static <T> void add(final T element, @NotNull final Collection<T> result, @NotNull final Disposable parentDisposable) {
834 if (result.add(element)) {
835 Disposer.register(parentDisposable, new Disposable() {
836 public void dispose() {
837 result.remove(element);
844 public static <T> List<T> createMaybeSingletonList(@Nullable T element) {
845 return element == null ? Collections.<T>emptyList() : Collections.singletonList(element);
848 public static <T, V> V getOrCreate(@NotNull Map<T, V> result, final T key, final V defaultValue) {
849 V value = result.get(key);
851 result.put(key, value = defaultValue);
856 public static <T, V> V getOrCreate(final Map<T, V> result, final T key, final Factory<V> factory) {
857 V value = result.get(key);
859 result.put(key, value = factory.create());
864 public static <T> boolean and(@NotNull T[] iterable, @NotNull Condition<T> condition) {
865 return and(Arrays.asList(iterable), condition);
868 public static <T> boolean and(@NotNull Iterable<T> iterable, @NotNull Condition<T> condition) {
869 for (final T t : iterable) {
870 if (!condition.value(t)) return false;
875 public static <T> boolean or(@NotNull T[] iterable, @NotNull Condition<T> condition) {
876 return or(Arrays.asList(iterable), condition);
879 public static <T> boolean or(@NotNull Iterable<T> iterable, @NotNull Condition<T> condition) {
880 for (final T t : iterable) {
881 if (condition.value(t)) return true;
886 public static <T> List<T> unfold(@Nullable T t, @NotNull NullableFunction<T, T> next) {
887 if (t == null) return Collections.emptyList();
889 final ArrayList<T> list = new ArrayList<T>();
898 public static <T> List<T> dropTail(@NotNull List<T> items) {
899 return items.subList(0, items.size() - 1);
903 public static <T> List<T> list(@NotNull T... items) {
904 return Arrays.asList(items);
907 // Generalized Quick Sort. Does neither array.clone() nor list.toArray()
909 public static <T> void quickSort(@NotNull List<T> list, @NotNull Comparator<? super T> comparator) {
910 quickSort(list, comparator, 0, list.size());
913 private static <T> void quickSort(@NotNull List<T> x, @NotNull Comparator<? super T> comparator, int off, int len) {
914 // Insertion sort on smallest arrays
916 for (int i = off; i < len + off; i++) {
917 for (int j = i; j > off && comparator.compare(x.get(j), x.get(j - 1)) < 0; j--) {
918 swapElements(x, j, j - 1);
924 // Choose a partition element, v
925 int m = off + (len >> 1); // Small arrays, middle element
928 int n = off + len - 1;
929 if (len > 40) { // Big arrays, pseudomedian of 9
931 l = med3(x, comparator, l, l + s, l + 2 * s);
932 m = med3(x, comparator, m - s, m, m + s);
933 n = med3(x, comparator, n - 2 * s, n - s, n);
935 m = med3(x, comparator, l, m, n); // Mid-size, med of 3
939 // Establish Invariant: v* (<v)* (>v)* v*
942 int c = off + len - 1;
945 while (b <= c && comparator.compare(x.get(b), v) <= 0) {
946 if (comparator.compare(x.get(b), v) == 0) {
947 swapElements(x, a++, b);
951 while (c >= b && comparator.compare(v, x.get(c)) <= 0) {
952 if (comparator.compare(x.get(c), v) == 0) {
953 swapElements(x, c, d--);
958 swapElements(x, b++, c--);
961 // Swap partition elements back to middle
963 int s = Math.min(a - off, b - a);
964 vecswap(x, off, b - s, s);
965 s = Math.min(d - c, n - d - 1);
966 vecswap(x, b, n - s, s);
968 // Recursively sort non-partition-elements
969 if ((s = b - a) > 1) quickSort(x, comparator, off, s);
970 if ((s = d - c) > 1) quickSort(x, comparator, n - s, s);
974 * Returns the index of the median of the three indexed longs.
976 private static <T> int med3(@NotNull List<T> x, Comparator<? super T> comparator, int a, int b, int c) {
977 return comparator.compare(x.get(a), x.get(b)) < 0 ? comparator.compare(x.get(b), x.get(c)) < 0
979 : comparator.compare(x.get(a), x.get(c)) < 0 ? c : a
980 : comparator.compare(x.get(c), x.get(b)) < 0
982 : comparator.compare(x.get(c), x.get(a)) < 0 ? c : a;
986 * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
988 private static <T> void vecswap(List<T> x, int a, int b, int n) {
989 for (int i = 0; i < n; i++, a++, b++) {
990 swapElements(x, a, b);
995 public static <T> CopyOnWriteArrayList<T> createEmptyCOWList() {
996 // does not create garbage new Object[0]
997 return new CopyOnWriteArrayList<T>(ContainerUtil.<T>emptyList());
1001 public static <T> List<T> emptyList() {
1002 return (List<T>)EmptyList.INSTANCE;
1006 * Merge sorted points, which are sorted by x and with equal x by y.
1007 * Result is put to x1 y1.
1009 public static void mergeSortedArrays(TIntArrayList x1,
1013 TIntArrayList newX = new TIntArrayList();
1014 TIntArrayList newY = new TIntArrayList();
1019 while (i < x1.size() && j < x2.size()) {
1020 if (x1.get(i) < x2.get(j) || (x1.get(i) == x2.get(j) && y1.get(i) < y2.get(j))) {
1021 newX.add(x1.get(i));
1022 newY.add(y1.get(i));
1025 else if (x1.get(i) > x2.get(j) || (x1.get(i) == x2.get(j) && y1.get(i) > y2.get(j))) {
1026 newX.add(x2.get(j));
1027 newY.add(y2.get(j));
1031 newX.add(x1.get(i));
1032 newY.add(y1.get(i));
1038 while (i < x1.size()) {
1039 newX.add(x1.get(i));
1040 newY.add(y1.get(i));
1044 while (j < x2.size()) {
1045 newX.add(x2.get(j));
1046 newY.add(y2.get(j));
1052 x1.add(newX.toNativeArray());
1053 y1.add(newY.toNativeArray());
1056 private static class EmptyList extends AbstractList<Object> implements RandomAccess, Serializable {
1057 private static final EmptyList INSTANCE = new EmptyList();
1063 public boolean contains(Object obj) {
1067 public Object get(int index) {
1068 throw new IndexOutOfBoundsException("Index: " + index);
1072 public Object[] toArray() {
1073 return ArrayUtil.EMPTY_OBJECT_ARRAY;
1077 public <T> T[] toArray(T[] a) {
1083 public static <T> Set<T> singleton(final T o, @NotNull final TObjectHashingStrategy<T> strategy) {
1084 return new Set<T>() {
1089 public boolean isEmpty() {
1093 public boolean contains(Object elem) {
1094 return strategy.equals(o, (T)elem);
1097 public Iterator<T> iterator() {
1098 return new Iterator<T>() {
1101 public boolean hasNext() {
1106 if (atEnd) throw new NoSuchElementException();
1111 public void remove() {
1112 throw new IncorrectOperationException();
1117 public Object[] toArray() {
1118 return new Object[]{o};
1121 public <T> T[] toArray(T[] a) {
1122 assert a.length == 1;
1127 public boolean add(T t) {
1128 throw new IncorrectOperationException();
1131 public boolean remove(Object o) {
1132 throw new IncorrectOperationException();
1135 public boolean containsAll(Collection<?> c) {
1139 public boolean addAll(Collection<? extends T> c) {
1140 throw new IncorrectOperationException();
1143 public boolean retainAll(Collection<?> c) {
1144 throw new IncorrectOperationException();
1147 public boolean removeAll(Collection<?> c) {
1148 throw new IncorrectOperationException();
1151 public void clear() {
1152 throw new IncorrectOperationException();
1158 public static <E> List<E> flatten(@NotNull Iterable<? extends Collection<E>> collections) {
1159 List<E> result = new ArrayList<E>();
1160 for (Collection<E> list : collections) {
1161 result.addAll(list);
1167 public static <K,V> V[] convert(K[] from, V[] to, Function<K,V> fun) {
1168 if (to.length < from.length) {
1169 to = (V[])Array.newInstance(to.getClass().getComponentType(), from.length);
1171 for (int i = 0; i < from.length; i++) {
1172 to[i] = fun.fun(from[i]);
1177 public static <T> int indexOf(List<T> list, Condition<T> condition) {
1178 for (int i = 0, listSize = list.size(); i < listSize; i++) {
1180 if (condition.value(t)) {
1187 public static <A,B> Map<B,A> reverseMap(Map<A,B> map) {
1188 final Map<B,A> result = new HashMap<B, A>();
1189 for (A a : map.keySet()) {
1190 result.put(map.get(a), a);