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    public static <T> ArrayList<T> newListWith(Collection<? extends T> collection) {
059        if (collection == null) {
060            return null;
061        }
062        return new ArrayList<>(collection);
063    }
064
065    public static <T> List<T> cloneAndSeal(Collection<? extends T> collection) {
066        if (collection == null) {
067            return Collections.emptyList();
068        }
069
070        ArrayList<T> clone = newListWith(collection);
071        return Collections.unmodifiableList(clone);
072    }
073
074    public static <K, V> Map<K, V> cloneAndSeal(Map<K, V> map) {
075        Map<K, V> clone = new HashMap<>(map);
076        return Collections.unmodifiableMap(clone);
077    }
078
079    public static <T> Set<T> newSetWith(Collection<? extends T> collection) {
080        if (collection == null) {
081            return null;
082        }
083        return new HashSet<>(collection);
084    }
085
086    public static <T> List<T> emptyOrSingletonListFrom(T element) {
087        if (element == null) {
088            return Collections.emptyList();
089        }
090        return Collections.singletonList(element);
091    }
092}