// Copyright 2016 The Domain Registry Authors. All Rights Reserved. // // 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.google.domain.registry.util; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.collect.Iterables.isEmpty; import static com.google.common.collect.Iterables.partition; import com.google.common.collect.HashMultiset; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Multimap; import com.google.common.collect.Multisets; import com.google.common.collect.Sets; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import java.util.SortedMap; import javax.annotation.Nullable; /** Utility methods related to collections. */ public class CollectionUtils { /** Checks if an iterable is null or empty. */ public static boolean isNullOrEmpty(@Nullable Iterable potentiallyNull) { return potentiallyNull == null || isEmpty(potentiallyNull); } /** Checks if a map is null or empty. */ public static boolean isNullOrEmpty(@Nullable Map potentiallyNull) { return potentiallyNull == null || potentiallyNull.isEmpty(); } /** Turns a null set into an empty set. JAXB leaves lots of null sets lying around. */ public static Set nullToEmpty(@Nullable Set potentiallyNull) { return firstNonNull(potentiallyNull, ImmutableSet.of()); } /** Turns a null list into an empty list. */ public static List nullToEmpty(@Nullable List potentiallyNull) { return firstNonNull(potentiallyNull, ImmutableList.of()); } /** Turns a null map into an empty map. */ public static Map nullToEmpty(@Nullable Map potentiallyNull) { return firstNonNull(potentiallyNull, ImmutableMap.of()); } /** Turns a null multimap into an empty multimap. */ public static Multimap nullToEmpty(@Nullable Multimap potentiallyNull) { return firstNonNull(potentiallyNull, ImmutableMultimap.of()); } /** Turns a null sorted map into an empty sorted map.. */ public static SortedMap nullToEmpty(@Nullable SortedMap potentiallyNull) { return firstNonNull(potentiallyNull, ImmutableSortedMap.of()); } /** Defensive copy helper for {@link Set}. */ public static ImmutableSet nullSafeImmutableCopy(Set data) { return data == null ? null : ImmutableSet.copyOf(data); } /** Defensive copy helper for {@link List}. */ public static ImmutableList nullSafeImmutableCopy(List data) { return data == null ? null : ImmutableList.copyOf(data); } /** Defensive copy helper for {@link Set}. */ public static ImmutableSet nullToEmptyImmutableCopy(Set data) { return data == null ? ImmutableSet.of() : ImmutableSet.copyOf(data); } /** Defensive copy helper for {@link Set}. */ public static > ImmutableSortedSet nullToEmptyImmutableSortedCopy(Set data) { return data == null ? ImmutableSortedSet.of() : ImmutableSortedSet.copyOf(data); } /** Defensive copy helper for {@link SortedMap}. */ public static ImmutableSortedMap nullToEmptyImmutableCopy(SortedMap data) { return data == null ? ImmutableSortedMap.of() : ImmutableSortedMap.copyOfSorted(data); } /** Defensive copy helper for {@link List}. */ public static ImmutableList nullToEmptyImmutableCopy(List data) { return data == null ? ImmutableList.of() : ImmutableList.copyOf(data); } /** Defensive copy helper for {@link Map}. */ public static ImmutableMap nullToEmptyImmutableCopy(Map data) { return data == null ? ImmutableMap.of() : ImmutableMap.copyOf(data); } /** * Turns an empty collection into a null collection. *

* This is unwise in the general case (nulls are bad; empties are good) but occasionally needed to * cause JAXB not to emit a field, or to avoid saving something to datastore. The method name * includes "force" to indicate that you should think twice before using it. */ @Nullable public static > C forceEmptyToNull(@Nullable C potentiallyEmpty) { return potentiallyEmpty == null || potentiallyEmpty.isEmpty() ? null : potentiallyEmpty; } /** Copy an {@link ImmutableSet} and add members. */ @SafeVarargs public static ImmutableSet union(Set set, T... newMembers) { return Sets.union(set, ImmutableSet.copyOf(newMembers)).immutableCopy(); } /** Copy an {@link ImmutableSet} and remove members. */ @SafeVarargs public static ImmutableSet difference(Set set, T... toRemove) { return Sets.difference(set, ImmutableSet.copyOf(toRemove)).immutableCopy(); } /** Returns any duplicates in an iterable. */ public static Set findDuplicates(Iterable iterable) { return Multisets.difference( HashMultiset.create(iterable), HashMultiset.create(ImmutableSet.copyOf(iterable))).elementSet(); } /** Partitions a Map into a Collection of Maps, each of max size n. */ public static ImmutableList> partitionMap(Map map, int size) { ImmutableList.Builder> shards = new ImmutableList.Builder<>(); for (Iterable> entriesShard : partition(map.entrySet(), size)) { shards.add(ImmutableMap.copyOf(entriesShard)); } return shards.build(); } }