org.arp.javautil.collections.CompositeList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javautil Show documentation
Show all versions of javautil Show documentation
JavaUtil is a utility library to speed the development of
Java software. We developed it over multiple years during
our internal development efforts, and it has reached a
point of stability where we have decided to make it
available to the outside world. The JavaUtil package aims
to fill in the gaps of the Apache Commons and similar
utility libraries out there. Features include convenience
classes for string, collections, arrays, dates, iterators,
colors, logging, unit testing, a little bit of basic
statistics, database queries, caching and more.
/*
* #%L
* JavaUtil
* %%
* Copyright (C) 2012 - 2013 Emory University
* %%
* 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.
* #L%
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.arp.javautil.collections;
import java.io.Serializable;
import java.util.AbstractSequentialList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
/**
*
* @author Andrew Post
*/
public class CompositeList extends AbstractSequentialList implements Serializable {
private final Collection> collOfLists;
public CompositeList() {
this(new ArrayList>(0));
}
public CompositeList(Collection> collOfLists) {
if (collOfLists != null) {
for (List list : collOfLists) {
if (list == null) {
throw new IllegalArgumentException(
"Cannot have any null lists");
}
}
this.collOfLists = new ArrayList<>(collOfLists);
} else {
this.collOfLists = new ArrayList<>();
}
}
public void addList(List list) {
this.collOfLists.add(list);
}
@Override
public ListIterator listIterator(int i) {
return new CompositeListIterator(i);
}
@Override
public int size() {
int result = 0;
for (List list : this.collOfLists) {
result += list.size();
}
return result;
}
private class CompositeListIterator implements ListIterator {
private List> itrs;
private int itrsIndex;
private int itrsSize;
private ListIterator currentItr;
private int index;
private CompositeListIterator(int i) {
this.itrs = new ArrayList<>(CompositeList.this.collOfLists.size());
for (List list : CompositeList.this.collOfLists) {
itrs.add(list.listIterator());
}
this.index = 0;
if (!itrs.isEmpty()) {
this.currentItr = itrs.get(0);
}
this.itrsIndex = 0;
this.itrsSize = this.itrs.size();
while (this.index < i) {
next();
}
}
@Override
public boolean hasNext() {
boolean result = this.currentItr != null ? this.currentItr.hasNext() : false;
int i = this.itrsIndex;
while (!result && i < this.itrsSize - 1) {
ListIterator next = this.itrs.get(++i);
result = next.hasNext();
}
return result;
}
@Override
@SuppressWarnings("unchecked")
public E next() {
boolean hasNext = this.currentItr != null ? this.currentItr.hasNext() : false;
while (!hasNext && this.itrsIndex < this.itrsSize - 1) {
this.currentItr = this.itrs.get(++this.itrsIndex);
hasNext = this.currentItr.hasNext();
}
E nextElt = this.currentItr.next();
this.index++;
return nextElt;
}
@Override
public boolean hasPrevious() {
if (this.currentItr == null) {
return false;
}
boolean result = this.currentItr.hasPrevious();
ListIterator prev;
int i = this.itrsIndex;
while (!result && i > 0) {
prev = this.itrs.get(--i);
result = prev.hasPrevious();
}
return result;
}
@Override
public E previous() {
boolean hasPrev = this.currentItr != null ? this.currentItr.hasPrevious() : false;
while (!hasPrev && this.itrsIndex > 0) {
this.currentItr = this.itrs.get(--this.itrsIndex);
hasPrev = this.currentItr.hasPrevious();
}
E prevElt = this.currentItr.previous();
this.index--;
return prevElt;
}
@Override
public int nextIndex() {
return this.currentItr != null ? this.index + 1 : 0;
}
@Override
public int previousIndex() {
return this.index - 1;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void set(E e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void add(E e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy