Here you can find the source of iterableAsList(Iterable
Parameter | Description |
---|---|
iterable | a parameter |
public static <E> List<E> iterableAsList(Iterable<E> iterable)
//package com.java2s; /******************************************************************************* * Copyright 2011 Danny Kunz/* www .java 2s. c o m*/ * * 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. ******************************************************************************/ import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { /** * Creates a new {@link List} from a given {@link Iterable} * * @param iterable * @return */ public static <E> List<E> iterableAsList(Iterable<E> iterable) { return iterable == null ? new ArrayList<E>() : iteratorAsList(iterable.iterator()); } /** * Creates a new {@link List} from a given {@link Iterator} * * @param iterator * @return */ public static <E> List<E> iteratorAsList(Iterator<E> iterator) { // List<E> retlist = new ArrayList<E>(); // if (iterator != null) { while (iterator.hasNext()) { retlist.add(iterator.next()); } } // return retlist; } /** * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will * contain all elements of the given {@link List} and additionally the further given element. <br> * <br> * This function will return always an instance, even if the given list is null. * * @param list * @param element * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null */ public static <E> List<E> add(List<? extends E> list, E element) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // retlist.add(element); // return retlist; } /** * Similar to {@link #add(List, Object)} allowing to specify an index position * * @param list * @param index * @param element * @return the given {@link List} instance or a new one */ public static <E> List<E> add(List<? extends E> list, int index, E element) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // while (list.size() < index) { list.add(null); } // retlist.add(index, element); // return retlist; } /** * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will * contain all elements of the given {@link List} and additionally all further given elements. <br> * <br> * This function will return always a instance, even if the given list is null. * * @param list * @param elements * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null */ public static <E> List<E> add(List<? extends E> list, E... elements) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // if (elements != null) { for (E element : elements) { retlist.add(element); } } // return retlist; } /** * Similar to {@link #add(List, Object...)} allowing to specify an index position * * @param list * @param index * @param elements * @return the given {@link List} instance or a new one */ public static <E> List<E> add(List<? extends E> list, int index, E... elements) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // while (list.size() < index) { list.add(null); } // int relative = 0; if (elements != null) { for (E element : elements) { retlist.add(index + relative, element); relative++; } } // return retlist; } }