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

com.autonomouslogic.commons.collection.ConcatList Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package com.autonomouslogic.commons.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.function.UnaryOperator;

public class ConcatList extends ConcatCollection implements List {
	private final List> lists;

	public ConcatList(List> lists) {
		super(new ArrayList<>(lists));
		this.lists = new ArrayList<>(lists);
	}

	@Override
	public E get(int i) {
		validateIndex(i);
		var min = 0;
		for (List list : lists) {
			var s = list.size();
			var max = min + s;
			if (i < max) {
				return list.get(i - min);
			}
			min = max;
		}
		throw new IllegalStateException();
	}

	@Override
	public E set(int i, E e) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void add(int i, E e) {
		throw new UnsupportedOperationException();
	}

	@Override
	public E remove(int i) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int indexOf(Object o) {
		var offset = 0;
		for (List list : lists) {
			var i = list.indexOf(o);
			if (i >= 0) {
				return offset + i;
			}
			offset += list.size();
		}
		return -1;
	}

	@Override
	public int lastIndexOf(Object o) {
		var n = lists.size();
		for (int l = n - 1; l >= 0; l--) {
			var list = lists.get(l);
			var i = list.lastIndexOf(o);
			if (i >= 0) {
				for (int r = l - 1; r >= 0; r--) {
					i += lists.get(r).size();
				}
				return i;
			}
		}
		return -1;
	}

	@Override
	public ListIterator listIterator() {
		throw new UnsupportedOperationException();
	}

	@Override
	public ListIterator listIterator(int i) {
		throw new UnsupportedOperationException();
	}

	@Override
	public List subList(int i, int i1) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void replaceAll(UnaryOperator operator) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void sort(Comparator c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean addAll(int i, Collection collection) {
		throw new UnsupportedOperationException();
	}

	private void validateIndex(int index) {
		var size = size();
		if (index < 0 || index >= size) {
			throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy