Java Collection Empty emptyToNull(final Collection value)

Here you can find the source of emptyToNull(final Collection value)

Description

If the given collection is empty null is returned.

License

Educational Community License

Parameter

Parameter Description
value the collection to check

Return

the provided collection or null if the provided collection is empty

Declaration

public static <T> Collection<T> emptyToNull(final Collection<T> value) 

Method Source Code


//package com.java2s;
//License from project: Educational Community License 

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    /**//w  w w .  ja v  a 2s.co  m
     * If the given collection is empty null is returned.
     *
     * @param value the collection to check
     * @return the provided collection or null if the provided collection is empty
     */
    public static <T> Set<T> emptyToNull(final Set<T> value) {
        return value.isEmpty() ? null : value;
    }

    /**
     * If the given collection is empty null is returned.
     *
     * @param value the collection to check
     * @return the provided collection or null if the provided collection is empty
     */
    public static <T, R> Map<T, R> emptyToNull(final Map<T, R> value) {
        return value.isEmpty() ? null : value;
    }

    /**
     * If the given collection is empty null is returned.
     *
     * @param value the collection to check
     * @return the provided collection or null if the provided collection is empty
     */
    public static <T> List<T> emptyToNull(final List<T> value) {
        return value.isEmpty() ? null : value;
    }

    /**
     * If the given collection is empty null is returned.
     *
     * @param value the collection to check
     * @return the provided collection or null if the provided collection is empty
     */
    public static <T> Collection<T> emptyToNull(final Collection<T> value) {
        return value.isEmpty() ? null : value;
    }
}

Related

  1. getSingletonCollectionOrEmptyIfNull(T o)
  2. isAnyEmpty(Collection... collections)
  3. isCollectionEmpty(Collection collection)
  4. isCollectionEmpty(Collection oneCol)