1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.arrow.util;
19 
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Spliterator;
29 import java.util.Spliterators;
30 import java.util.stream.Collectors;
31 import java.util.stream.StreamSupport;
32 
33 /**
34  * Utility methods for manipulating {@link java.util.Collections} and their subclasses/implementations.
35  */
36 public final class Collections2 {
Collections2()37   private Collections2() {}
38 
39   /**
40    * Creates a {@link List} from the elements remaining in iterator.
41    */
toList(Iterator<T> iterator)42   public static <T> List<T> toList(Iterator<T> iterator) {
43     List<T> target = new ArrayList<>();
44     iterator.forEachRemaining(target::add);
45     return target;
46   }
47 
48   /**
49    * Converts the iterable into a new {@link List}.
50    */
toList(Iterable<T> iterable)51   public static <T> List<T> toList(Iterable<T> iterable) {
52     if (iterable instanceof Collection<?>) {
53       // If iterable is a collection, take advantage of it for a more efficient copy
54       return new ArrayList<T>((Collection<T>) iterable);
55     }
56     return toList(iterable.iterator());
57   }
58 
59   /**
60    * Converts the iterable into a new immutable {@link List}.
61    */
toImmutableList(Iterable<T> iterable)62   public static <T> List<T> toImmutableList(Iterable<T> iterable) {
63     return Collections.unmodifiableList(toList(iterable));
64   }
65 
66 
67   /** Copies the elements of <code>map</code> to a new unmodifiable map. */
immutableMapCopy(Map<K, V> map)68   public static <K, V> Map<K, V> immutableMapCopy(Map<K, V> map) {
69     return Collections.unmodifiableMap(new HashMap<>(map));
70   }
71 
72   /** Copies the elements of list to a new unmodifiable list. */
immutableListCopy(List<V> list)73   public static <V> List<V> immutableListCopy(List<V> list) {
74     return Collections.unmodifiableList(new ArrayList<>(list));
75   }
76 
77   /** Copies the values to a new unmodifiable list. */
asImmutableList(V...values)78   public static <V> List<V> asImmutableList(V...values) {
79     return Collections.unmodifiableList(Arrays.asList(values));
80   }
81 
82   /**
83    * Creates a human readable string from the remaining elements in iterator.
84    *
85    * The output should be similar to {@code Arrays#toString(Object[])}
86    */
toString(Iterator<?> iterator)87   public static String toString(Iterator<?> iterator) {
88     return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
89         .map(String::valueOf)
90         .collect(Collectors.joining(", ", "[", "]"));
91   }
92 }
93