All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.d2ab.collection.IterableList Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
/*
 * Copyright 2016 Daniel Skogquist Åborg
 *
 * 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.
 */

package org.d2ab.collection;

import org.d2ab.iterator.Iterators;

import java.util.*;

/**
 * A sequential {@link List} view of an {@link Iterable}.
 *
 * @since 1.2
 */
public class IterableList extends AbstractSequentialList {
	private Iterable iterable;

	public IterableList(Iterable iterable) {
		this.iterable = iterable;
	}

	/**
	 * Create a {@code List} view of the given {@link Iterable}, which is updated in real time as the
	 * {@link Iterable} changes. If a {@link List} is given it is returned unchanged. The list does not implement
	 * {@link RandomAccess} unless the given {@link Iterable} does, and is best accessed in sequence. The list does
	 * not support modification except removal, by {@link Iterator#remove()} if implemented in the {@link Iterable}.
	 */
	public static  List from(Iterable iterable) {
		if (iterable instanceof List)
			return (List) iterable;

		return new IterableList<>(iterable);
	}

	@Override
	public Iterator iterator() {
		return iterable.iterator();
	}

	@Override
	public ListIterator listIterator(int index) {
		Iterator iterator = iterator();
		ListIterator listIterator = new ListIterator() {
			private final List previous = new LinkedList<>();

			int cursor;

			@Override
			public boolean hasNext() {
				return cursor < previous.size() || iterator.hasNext();
			}

			@Override
			public T next() {
				if (cursor < previous.size())
					return previous.get(cursor++);

				cursor++;

				T next = iterator.next();
				previous.add(next);
				return next;
			}

			@Override
			public boolean hasPrevious() {
				return cursor > 0;
			}

			@Override
			public T previous() {
				return previous.get(--cursor);
			}

			@Override
			public int nextIndex() {
				return cursor;
			}

			@Override
			public int previousIndex() {
				return cursor - 1;
			}

			@Override
			public void remove() {
				if (cursor < previous.size())
					throw new IllegalStateException("Cannot remove after previous");
				iterator.remove();
				previous.remove(--cursor);
			}

			@Override
			public void set(T t) {
				throw new UnsupportedOperationException();
			}

			@Override
			public void add(T t) {
				throw new UnsupportedOperationException();
			}
		};
		Iterators.skip(listIterator, index);
		return listIterator;
	}

	@Override
	public int size() {
		return (int) Iterators.count(iterator());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy