]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/java/memory/memory-core/src/main/java/org/apache/arrow/util/Collections2.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / memory / memory-core / src / main / java / org / apache / arrow / util / Collections2.java
CommitLineData
1d09f67e
TL
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
18package org.apache.arrow.util;
19
20import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.Iterator;
26import java.util.List;
27import java.util.Map;
28import java.util.Spliterator;
29import java.util.Spliterators;
30import java.util.stream.Collectors;
31import java.util.stream.StreamSupport;
32
33/**
34 * Utility methods for manipulating {@link java.util.Collections} and their subclasses/implementations.
35 */
36public final class Collections2 {
37 private Collections2() {}
38
39 /**
40 * Creates a {@link List} from the elements remaining in iterator.
41 */
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 */
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 */
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. */
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. */
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. */
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 */
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}