All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.couchbase.client.core.util.CbCollections Maven / Gradle / Ivy

There is a newer version: 3.7.2
Show newest version
/*
 * Copyright 2019 Couchbase, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.couchbase.client.core.util;

import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.error.InvalidArgumentException;
import reactor.util.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

@Stability.Internal
public class CbCollections {
  private CbCollections() {
    throw new AssertionError("not instantiable");
  }

  /**
   * Backport of {@code List.copyOf}.
   * 

* Returns an unmodifiable list containing all elements of the given collection. * Subsequent changes to the original collection are not reflected in the returned list. * * @throws NullPointerException if original is null or contains any nulls */ @SuppressWarnings("unchecked") public static List listCopyOf(Collection original) { return (List) listOf(original.toArray()); } /** * Backport of {@code Set.copyOf}. *

* Returns an unmodifiable set containing all elements of the given collection. * If the original contains duplicate items, an arbitrary element is preserved. * Subsequent changes to the original collection are not reflected in the returned set. * * @throws NullPointerException if original is null or contains any nulls */ @SuppressWarnings("unchecked") public static Set setCopyOf(Collection original) { return (Set) setOf(new HashSet<>(original).toArray()); } /** * Backport of {@code Map.copyOf}. *

* Returns an unmodifiable map containing all entries of the given map. * Subsequent changes to the original map are not reflected in the returned map. * * @throws NullPointerException if original is null or contains any null keys or values. */ public static Map mapCopyOf(Map original) { Map result = new HashMap<>(); original.forEach((k,v) -> putUniqueKey(result, k, v)); return unmodifiableMap(result); } /** * Returns a new unmodifiable list with the same contents as the given collection. * Subsequent changes to the original collection are not reflected in the returned list. * * @param c may be {@code null}, in which case an empty list is returned. */ public static List copyToUnmodifiableList(@Nullable Collection c) { return isNullOrEmpty(c) ? emptyList() : unmodifiableList(new ArrayList<>(c)); } /** * Returns a new unmodifiable set with the same contents as the given collection. * Subsequent changes to the original collection are not reflected in the returned set. * * @param c may be {@code null}, in which case an empty set is returned. */ public static Set copyToUnmodifiableSet(@Nullable Collection c) { return isNullOrEmpty(c) ? emptySet() : unmodifiableSet(new HashSet<>(c)); } public static boolean isNullOrEmpty(@Nullable Collection c) { return c == null || c.isEmpty(); } public static boolean isNullOrEmpty(@Nullable Map m) { return m == null || m.isEmpty(); } public static boolean isNullOrEmpty(@Nullable String s) { return s == null || s.isEmpty(); } /** * Returns an unmodifiable set containing the given items. * * @throws NullPointerException if any item is null * @throws InvalidArgumentException if there are duplicate items */ @SafeVarargs public static Set setOf(T... items) { Set result = new HashSet<>(); for (T item : items) { if (!result.add(requireNonNull(item, "Set may not contain null"))) { throw InvalidArgumentException.fromMessage("Duplicate item: " + item); } } return unmodifiableSet(result); } /** * Returns an unmodifiable list containing the given items. * * @throws NullPointerException if any item is null */ @SafeVarargs public static List listOf(T... items) { List result = new ArrayList<>(items.length); for (T item : items) { result.add(requireNonNull(item, "List may not contain null")); } return unmodifiableList(result); } /** * Returns an unmodifiable empty map. */ public static Map mapOf() { return emptyMap(); } /** * Returns an unmodifiable map containing the given key/value pairs. * * @throws NullPointerException if any key or value is null * @throws InvalidArgumentException if there are duplicate keys */ public static Map mapOf(K key1, V value1) { Map result = new HashMap<>(); putUniqueKey(result, key1, value1); return unmodifiableMap(result); } /** * Returns an unmodifiable map containing the given key/value pairs. * * @throws NullPointerException if any key or value is null * @throws InvalidArgumentException if there are duplicate keys */ public static Map mapOf(K key1, V value1, K key2, V value2) { Map result = new HashMap<>(); putUniqueKey(result, key1, value1); putUniqueKey(result, key2, value2); return unmodifiableMap(result); } /** * Returns an unmodifiable map containing the given key/value pairs. * * @throws NullPointerException if any key or value is null * @throws InvalidArgumentException if there are duplicate keys */ public static Map mapOf(K key1, V value1, K key2, V value2, K key3, V value3) { Map result = new HashMap<>(); putUniqueKey(result, key1, value1); putUniqueKey(result, key2, value2); putUniqueKey(result, key3, value3); return unmodifiableMap(result); } /** * Returns an unmodifiable map containing the given key/value pairs. * * @throws NullPointerException if any key or value is null * @throws InvalidArgumentException if there are duplicate keys */ @SuppressWarnings("Duplicates") public static Map mapOf(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) { Map result = new HashMap<>(); putUniqueKey(result, key1, value1); putUniqueKey(result, key2, value2); putUniqueKey(result, key3, value3); putUniqueKey(result, key4, value4); return unmodifiableMap(result); } /** * Returns an unmodifiable map containing the given key/value pairs. * * @throws NullPointerException if any key or value is null * @throws InvalidArgumentException if there are duplicate keys */ @SuppressWarnings("Duplicates") public static Map mapOf(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4, K key5, V value5) { Map result = new HashMap<>(); putUniqueKey(result, key1, value1); putUniqueKey(result, key2, value2); putUniqueKey(result, key3, value3); putUniqueKey(result, key4, value4); putUniqueKey(result, key5, value5); return unmodifiableMap(result); } private static void putUniqueKey(Map map, K key, V value) { requireNonNull(key, "Key may not be null."); requireNonNull(value, "Value may not be null."); if (map.put(key, value) != null) { throw InvalidArgumentException.fromMessage("Duplicate key: " + key); } } /** * Convenience method equivalent to: *

   * input.stream().map(transformer).collect(toList())
   * 
*/ public static List transform(Iterable input, Function transformer) { List result = new ArrayList<>(); input.forEach(it -> result.add(transformer.apply(it))); return result; } /** * Convenience method equivalent to: *
   * input.stream().map(transformer).collect(toList())
   * 
*/ public static List transform(Iterator input, Function transformer) { List result = new ArrayList<>(); input.forEachRemaining(it -> result.add(transformer.apply(it))); return result; } /** * Convenience method equivalent to: *
   * Arrays.stream(input).map(transformer).collect(toList())
   * 
*/ public static List transform(T1[] input, Function transformer) { return Arrays.stream(input) .map(transformer) .collect(toList()); } /** * Convenience method equivalent to: *
   * input.stream().filter(predicate).collect(toList())
   * 
*/ public static List filter(Iterable input, Predicate predicate) { List result = new ArrayList<>(); input.forEach(it -> { if (predicate.test(it)) { result.add(it); } }); return result; } /** * Convenience method equivalent to: *
   * Arrays.stream(input).filter(predicate).collect(toList())
   * 
*/ public static List filter(T[] input, Predicate predicate) { return Arrays.stream(input) .filter(predicate) .collect(toList()); } public static Map transformValues(Map map, Function transformer) { return map.entrySet() .stream() .collect(toMap(Map.Entry::getKey, entry -> transformer.apply(entry.getValue()))); } public static Map transformValues(Map map, BiFunction transformer) { return map.entrySet() .stream() .collect(toMap(Map.Entry::getKey, entry -> transformer.apply(entry.getKey(), entry.getValue()))); } /** * Returns an unmodifiable list containing all elements of the given iterable. * Subsequent changes to the original iterable are not reflected in the returned list. * * @throws NullPointerException if original is null or contains any nulls */ public static List listCopyOf(Iterable original) { List result = new ArrayList<>(); original.forEach(it -> result.add(requireNonNull(it, "List may not contain null."))); return unmodifiableList(result); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy