![JAR search and dependency download from the Maven repository](/logo.png)
org.d2ab.collection.ChainedList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sequence Show documentation
Show all versions of sequence Show documentation
A lightweight alternative to Java 8 sequential Stream
The 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.ChainedListIterator;
import org.d2ab.iterator.ChainingIterator;
import java.util.*;
import static org.d2ab.collection.SizedIterable.SizeType.*;
/**
* A {@link List} of multiple {@link List}s strung together in a chain.
*/
public class ChainedList extends AbstractList implements SizedIterable {
private final List> lists;
private final SizeType sizeType;
@SafeVarargs
public static List concat(List... lists) {
return concat(Lists.of(lists));
}
public static List concat(List> lists) {
return new ChainedList<>(lists);
}
private ChainedList(List> lists) {
this.lists = lists;
this.sizeType = Iterables.sizeType(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) {
if (index == 0 && lists.isEmpty())
lists.add(new ArrayList<>());
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 extends T> c) {
if (index == 0 && lists.isEmpty())
lists.add(new ArrayList<>());
for (List list : lists) {
if (index <= list.size())
return list.addAll(index, c);
index -= list.size();
}
throw new IndexOutOfBoundsException();
}
@Override
public int size() {
if (sizeType == INFINITE)
throw new UnsupportedOperationException();
int size = 0;
for (List list : lists)
size += list.size();
return size;
}
@Override
public SizeType sizeType() {
if (sizeType == INFINITE)
throw new UnsupportedOperationException();
SizeType sizeType = this.sizeType != FIXED ? AVAILABLE : FIXED;
for (List list : lists)
sizeType = sizeType.concat(Iterables.sizeType(list));
return sizeType;
}
@Override
public void clear() {
if (sizeType == INFINITE)
throw new UnsupportedOperationException();
for (List l : lists)
l.clear();
}
@Override
public boolean isEmpty() {
if (sizeType == INFINITE)
throw new UnsupportedOperationException();
for (List list : lists)
if (!list.isEmpty())
return false;
return true;
}
@Override
public Spliterator spliterator() {
if (sizeType == INFINITE || sizeType() == INFINITE)
return Spliterators.spliteratorUnknownSize(iterator(), 0);
return Spliterators.spliterator(this, 0);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy