CollectionUtil.java

  1. /**
  2.  *
  3.  * Copyright 2015-2021 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * 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. package org.jivesoftware.smack.util;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;

  27. public class CollectionUtil {

  28.     public static <T> Collection<T> requireNotEmpty(Collection<T> collection, String collectionName) {
  29.         if (collection == null) {
  30.             throw new NullPointerException(collectionName + " must not be null.");
  31.         }
  32.         if (collection.isEmpty()) {
  33.             throw new IllegalArgumentException(collectionName + " must not be empty.");
  34.         }
  35.         return collection;
  36.     }

  37.     public static <T, C extends Collection<T>> List<T> removeUntil(C collection, Predicate<T> predicate) {
  38.         List<T> removedElements = new ArrayList<>(collection.size());
  39.         for (Iterator<T> it = collection.iterator(); it.hasNext();) {
  40.             T t = it.next();
  41.             if (predicate.test(t)) {
  42.                 break;
  43.             }
  44.             removedElements.add(t);
  45.             it.remove();
  46.         }
  47.         return removedElements;
  48.     }

  49.     public interface Predicate<T> {
  50.         boolean test(T t);
  51.     }

  52.     public static <T> ArrayList<T> newListWith(Collection<? extends T> collection) {
  53.         if (collection == null) {
  54.             return null;
  55.         }
  56.         return new ArrayList<>(collection);
  57.     }

  58.     public static <T> List<T> cloneAndSeal(Collection<? extends T> collection) {
  59.         if (collection == null) {
  60.             return Collections.emptyList();
  61.         }

  62.         ArrayList<T> clone = newListWith(collection);
  63.         return Collections.unmodifiableList(clone);
  64.     }

  65.     public static <K, V> Map<K, V> cloneAndSeal(Map<K, V> map) {
  66.         Map<K, V> clone = new HashMap<>(map);
  67.         return Collections.unmodifiableMap(clone);
  68.     }

  69.     public static <T> Set<T> newSetWith(Collection<? extends T> collection) {
  70.         if (collection == null) {
  71.             return null;
  72.         }
  73.         return new HashSet<>(collection);
  74.     }

  75.     public static <T> List<T> emptyOrSingletonListFrom(T element) {
  76.         if (element == null) {
  77.             return Collections.emptyList();
  78.         }
  79.         return Collections.singletonList(element);
  80.     }

  81.     public static <T> Set<T> nullSafeUnmodifiableSet(Set<T> set) {
  82.         if (set == null) {
  83.             return Collections.emptySet();
  84.         }
  85.         return Collections.unmodifiableSet(set);
  86.     }
  87. }