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

org.d2ab.collection.ChainedList 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.ChainingIterator;
import org.d2ab.iterator.list.ChainedListIterator;

import java.util.*;

import static java.util.Collections.singletonList;

/**
 * A {@link List} of multiple {@link List}s strung together in a chain.
 */
public class ChainedList extends AbstractList {
	private final List> lists;

	@SuppressWarnings("unchecked")
	public static  List empty() {
		return from();
	}

	@SuppressWarnings("unchecked")
	public static  List of(T item) {
		return from(singletonList(item));
	}

	@SuppressWarnings("unchecked")
	public static  List of(T... items) {
		return from(Arrays.asList(items));
	}

	@SuppressWarnings("unchecked")
	public static  List from(List... lists) {
		return from(Arrays.asList(lists));
	}

	@SuppressWarnings("unchecked")
	public static  List from(List> lists) {
		return new ChainedList<>(lists);
	}

	private ChainedList(List> lists) {
		this.lists = lists;
	}

	@Override
	public Iterator iterator() {
		return new ChainingIterator<>(lists);
	}

	@Override
	public ListIterator listIterator(int index) {
		return new ChainedListIterator<>(lists, index);
	}

	@Override
	public T get(int index) {
		for (List list : lists) {
			if (list.size() > index)
				return list.get(index);
			index -= list.size();
		}
		throw new IndexOutOfBoundsException();
	}

	@Override
	public T set(int index, T element) {
		for (List list : lists) {
			if (list.size() > index)
				return list.set(index, element);
			index -= list.size();
		}
		throw new IndexOutOfBoundsException();
	}

	@Override
	public void add(int index, T element) {
		for (List list : lists) {
			if (index <= list.size()) {
				list.add(index, element);
				return;
			}
			index -= list.size();
		}
		throw new IndexOutOfBoundsException();
	}

	@Override
	public T remove(int index) {
		for (List list : lists) {
			if (list.size() > index)
				return list.remove(index);
			index -= list.size();
		}
		throw new IndexOutOfBoundsException();
	}

	@Override
	public boolean addAll(int index, Collection c) {
		for (List list : lists) {
			if (index <= list.size())
				return list.addAll(index, c);
			index -= list.size();
		}
		throw new IndexOutOfBoundsException();
	}

	@Override
	public int size() {
		int size = 0;
		for (List list : lists)
			size += list.size();
		return size;
	}

	@Override
	public void clear() {
		lists.forEach(List::clear);
	}

	@Override
	public boolean isEmpty() {
		for (List list : lists)
			if (!list.isEmpty())
				return false;
		return true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy