001/** 002 * 003 * Copyright 2015-2021 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.HashMap; 023import java.util.HashSet; 024import java.util.Iterator; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028 029public class CollectionUtil { 030 031 public static <T> Collection<T> requireNotEmpty(Collection<T> collection, String collectionName) { 032 if (collection == null) { 033 throw new NullPointerException(collectionName + " must not be null."); 034 } 035 if (collection.isEmpty()) { 036 throw new IllegalArgumentException(collectionName + " must not be empty."); 037 } 038 return collection; 039 } 040 041 public static <T, C extends Collection<T>> List<T> removeUntil(C collection, Predicate<T> predicate) { 042 List<T> removedElements = new ArrayList<>(collection.size()); 043 for (Iterator<T> it = collection.iterator(); it.hasNext();) { 044 T t = it.next(); 045 if (predicate.test(t)) { 046 break; 047 } 048 removedElements.add(t); 049 it.remove(); 050 } 051 return removedElements; 052 } 053 054 public interface Predicate<T> { 055 boolean test(T t); 056 } 057 058 @SuppressWarnings("NonApiType") 059 public static <T> ArrayList<T> newListWith(Collection<? extends T> collection) { 060 if (collection == null) { 061 return null; 062 } 063 return new ArrayList<>(collection); 064 } 065 066 public static <T> List<T> cloneAndSeal(Collection<? extends T> collection) { 067 if (collection == null) { 068 return Collections.emptyList(); 069 } 070 071 ArrayList<T> clone = newListWith(collection); 072 return Collections.unmodifiableList(clone); 073 } 074 075 public static <K, V> Map<K, V> cloneAndSeal(Map<K, V> map) { 076 Map<K, V> clone = new HashMap<>(map); 077 return Collections.unmodifiableMap(clone); 078 } 079 080 public static <T> Set<T> newSetWith(Collection<? extends T> collection) { 081 if (collection == null) { 082 return null; 083 } 084 return new HashSet<>(collection); 085 } 086 087 public static <T> List<T> emptyOrSingletonListFrom(T element) { 088 if (element == null) { 089 return Collections.emptyList(); 090 } 091 return Collections.singletonList(element); 092 } 093 094 public static <T> Set<T> nullSafeUnmodifiableSet(Set<T> set) { 095 if (set == null) { 096 return Collections.emptySet(); 097 } 098 return Collections.unmodifiableSet(set); 099 } 100}