|
|
@@ -10,13 +10,13 @@ import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Klasa reprezentująca złączenie wielu list jako jedną kolekcję.
|
|
|
- * @param <T>
|
|
|
+ * @param <T>
|
|
|
* @author ranides
|
|
|
*/
|
|
|
public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
-
|
|
|
+
|
|
|
private final List<List<T>> content;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Tworzy nowe złączenie, do którego można doklejać nowe listy za pomocą
|
|
|
* metody {@link #join(List) join(List)}
|
|
|
@@ -24,7 +24,7 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
public JoinList() {
|
|
|
content = new LinkedList<List<T>>();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Tworzy nowe złączenie na podstawie podanej listy list.
|
|
|
* @param values
|
|
|
@@ -63,7 +63,7 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
}
|
|
|
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + end );
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public Iterator<T> iterator() {
|
|
|
return new JoinIterator();
|
|
|
@@ -78,31 +78,32 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
public ListIterator<T> listIterator(int index) {
|
|
|
return new JoinIterator(index);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Dodaje do złączenia listę.
|
|
|
* @param list
|
|
|
*/
|
|
|
- public void join(List<T> list) {
|
|
|
+ public JoinList<T> join(List<T> list) {
|
|
|
content.add(list);
|
|
|
+ return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private final class JoinIterator implements ListIterator<T> {
|
|
|
-
|
|
|
+
|
|
|
private int segment = 0;
|
|
|
private ListIterator<T> iterator;
|
|
|
-
|
|
|
+
|
|
|
private int index = 0;
|
|
|
private boolean hasNext = false;
|
|
|
private boolean hasPrev = false;
|
|
|
private T next;
|
|
|
private T prev;
|
|
|
-
|
|
|
+
|
|
|
public JoinIterator(int index) {
|
|
|
this();
|
|
|
while(index > 0) { next(); }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public JoinIterator() {
|
|
|
segment = 0;
|
|
|
iterator = content.get(segment).listIterator();
|
|
|
@@ -113,14 +114,14 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
if( hasNext ) {
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if( iterator!=null && iterator.hasNext() ) {
|
|
|
next = iterator.next();
|
|
|
hasNext = true;
|
|
|
hasPrev = false;
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
while( segment+1 < content.size() ) {
|
|
|
hasPrev = false;
|
|
|
iterator = content.get( ++segment ).listIterator();
|
|
|
@@ -130,7 +131,7 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
hasNext = false;
|
|
|
return false;
|
|
|
}
|
|
|
@@ -141,7 +142,7 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
hasNext = false;
|
|
|
index++;
|
|
|
return next;
|
|
|
- }
|
|
|
+ }
|
|
|
throw new NoSuchElementException();
|
|
|
}
|
|
|
|
|
|
@@ -150,14 +151,14 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
if( hasPrev ) {
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if( iterator!=null && iterator.hasPrevious() ) {
|
|
|
prev = iterator.previous();
|
|
|
hasPrev = true;
|
|
|
hasNext = false;
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
while( segment > 0 ) {
|
|
|
hasNext = false;
|
|
|
List<T> list = content.get(--segment);
|
|
|
@@ -168,7 +169,7 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
hasPrev = false;
|
|
|
return false;
|
|
|
}
|
|
|
@@ -179,7 +180,7 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
hasPrev = false;
|
|
|
index--;
|
|
|
return prev;
|
|
|
- }
|
|
|
+ }
|
|
|
throw new NoSuchElementException();
|
|
|
}
|
|
|
|
|
|
@@ -207,6 +208,6 @@ public class JoinList<T> extends AbstractSequentialList<T> {
|
|
|
public void add(T value) {
|
|
|
throw new UnsupportedOperationException("Immutable list.");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
}
|