Returns a List containing the given objects, returns an empty List, if objects is null. - Android java.util

Android examples for java.util:List Contain

Description

Returns a List containing the given objects, returns an empty List, if objects is null.

Demo Code

/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****//from w  w w .  j  a v  a2s.  com
 *
 * Version: MPL 2.0
 *
 * echocat Locela - API for Java, Copyright (c) 2014-2016 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import static java.util.Collections.*;
import static org.echocat.locela.api.java.utils.ResourceUtils.closeQuietlyIfAutoCloseable;

public class Main{
    /**
     * Returns a {@link List} containing the given <code>objects</code>,
     * returns an empty List, if <code>objects</code> is null.
     */
    @Nonnull
    public static <T> List<T> asList(@Nullable T... objects) {
        final List<T> result;
        if (objects == null) {
            result = new ArrayList<>();
        } else {
            final int initialCapacity = Math.max(16,
                    ((objects.length + 2) / 3) * 4);
            result = new ArrayList<>(initialCapacity);
            result.addAll(new ArrayWrapper<>(objects));
        }
        return result;
    }
    @Nonnull
    public static <T> List<T> asList(@Nullable Iterator<T> iterator) {
        final List<T> result = new ArrayList<>();
        try {
            if (iterator != null) {
                while (iterator.hasNext()) {
                    result.add(iterator.next());
                }
            }
        } finally {
            ResourceUtils.closeQuietlyIfAutoCloseable(iterator);
        }
        return result;
    }
    @Nonnull
    public static <T> List<T> asList(@Nullable Iterable<T> in) {
        final List<T> result;
        if (in instanceof List) {
            result = (List<T>) in;
        } else if (in instanceof Collection) {
            result = new ArrayList<>((Collection<T>) in);
        } else {
            result = new ArrayList<>();
            addAll(result, in);
        }
        return result;
    }
    @Nonnull
    public static <T> List<T> asList(@Nullable Enumeration<T> in) {
        final List<T> result = new ArrayList<>();
        while (in != null && in.hasMoreElements()) {
            result.add(in.nextElement());
        }
        return result;
    }
    @Nonnull
    public static <T, C extends Collection<T>> C addAll(@Nonnull C to,
            @Nullable T... elements) {
        if (elements != null) {
            Collections.addAll(to, elements);
        }
        return to;
    }
    @Nonnull
    public static <T, C extends Collection<T>> C addAll(@Nonnull C to,
            @Nullable Iterable<T> elements) {
        if (elements != null) {
            try {
                addAll(to, elements.iterator());
            } finally {
                ResourceUtils.closeQuietlyIfAutoCloseable(elements);
            }
        }
        return to;
    }
    @Nonnull
    public static <T, C extends Collection<T>> C addAll(@Nonnull C to,
            @Nullable Iterator<T> elements) {
        if (elements != null) {
            try {
                while (elements.hasNext()) {
                    to.add(elements.next());
                }
            } finally {
                ResourceUtils.closeQuietlyIfAutoCloseable(elements);
            }
        }
        return to;
    }
}

Related Tutorials