Java List Cast cast(final Collection list)

Here you can find the source of cast(final Collection list)

Description

cast

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <L> List<L> cast(final Collection<? extends Object> list) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2014 Alexander Kerner. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*  w  w  w.  j  av  a  2 s.  c o  m*/
 ******************************************************************************/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.List;

public class Main {
    @SuppressWarnings("unchecked")
    public static <L> List<L> cast(final Collection<? extends Object> list) {
        if (list instanceof List) {
            return (List<L>) list;
        } else {
            return (List<L>) newList(list);
        }
    }

    public static <T> List<T> newList() {
        return new ArrayList<T>();
    }

    public static <T> List<T> newList(final Collection<? extends T> elements) {
        return new ArrayList<T>(elements);
    }

    public static <T> List<T> newList(final Collection<? extends T>... elements) {
        final List<T> result = newList();
        for (final Collection<? extends T> c : elements) {
            result.addAll(c);
        }
        return result;
    }

    public static <T> List<T> newList(final T... elements) {
        return Arrays.asList(elements);
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> asList(final Collection<? extends T> collection) {
        List<T> result;
        if (collection instanceof List) {
            result = (List<T>) collection;
        } else {
            result = newList(collection);
        }
        return result;
    }
}

Related

  1. cast(Class clazz, List list)
  2. cast(final Class cls, final Object[] list)
  3. cast(final List list)
  4. cast(List list)
  5. cast(List list)
  6. cast(List list)