com.tinspx.util.collect.NotNull.java Source code

Java tutorial

Introduction

Here is the source code for com.tinspx.util.collect.NotNull.java

Source

/* Copyright (C) 2013-2014 Ian Teune <ian.teune@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tinspx.util.collect;

import com.google.common.base.Predicates;
import com.google.common.collect.Multiset;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * Provides views of {@code Collection}s that do not allow {@code null} elements
 * to be added. This is equivalent to using {@link Predicated} and
 * {@link Predicates#notNull()}, except that the collections may not contain any
 * existing null elements.
 *
 * @author Ian
 */
public class NotNull {
    private NotNull() {
    }

    /**
     * Wraps {@code collection} as a {@code Collection} that doe not allow
     * {@code null} elements to be added.
     * 
     * @throws NullPointerException if any existing element in
     * {@code collection} is {@code null}
     */
    public static <E> Collection<E> collection(Collection<E> collection) {
        CollectUtils.checkAllNotNull(collection);
        return Predicated.collection(collection, Predicates.notNull());
    }

    /**
     * Wraps {@code set} as a {@code Set} that doe not allow {@code null}
     * elements to be added.
     *
     * @throws NullPointerException if any existing element in {@code set} is
     * {@code null}
     */
    public static <E> Set<E> set(Set<E> set) {
        CollectUtils.checkAllNotNull(set);
        return Predicated.set(set, Predicates.notNull());
    }

    /**
     * Wraps {@code list} as a {@code List} that doe not allow {@code null}
     * elements to be added.
     *
     * @throws NullPointerException if any existing element in {@code list} is
     * {@code null}
     */
    public static <E> List<E> list(List<E> list) {
        CollectUtils.checkAllNotNull(list);
        return Predicated.list(list, Predicates.notNull());
    }

    /**
     * Wraps {@code multiset} as a {@code Multiset} that doe not allow
     * {@code null} elements to be added.
     *
     * @throws NullPointerException if any existing element in {@code multiset}
     * is {@code null}
     */
    public static <E> Multiset<E> multiset(Multiset<E> multiset) {
        CollectUtils.checkAllNotNull(multiset.elementSet());
        return Predicated.multiset(multiset, Predicates.notNull());
    }
}