summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionIterator.java
blob: 7e523bcd780bb3d4a19949f14ad6d04e48846810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.keuin.kbackupfabric.backup.incremental;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

public class ObjectCollectionIterator implements Iterator<ObjectElement> {
    private Iterator<ObjectElement> currentIterator;
    private final List<ObjectCollection2> cols = new LinkedList<>();

    public ObjectCollectionIterator(ObjectCollection2 collection) {
        cols.addAll(collection.getSubCollectionSet());
        currentIterator = collection.getElementSet().iterator();
    }

    @Override
    public boolean hasNext() {
        if (currentIterator != null) {
            if (currentIterator.hasNext())
                return true;
            else {
                currentIterator = null;
                return hasNext();
            }
        } else {
            if (cols.isEmpty())
                return false;
            else {
                ObjectCollection2 consumedCollection = cols.remove(0);
                cols.addAll(consumedCollection.getSubCollectionSet());
                currentIterator = consumedCollection.getElementSet().iterator();
                return hasNext();
            }
        }
    }

    @Override
    public ObjectElement next() {
        if (hasNext()) {
            return currentIterator.next();
        } else {
            throw new NoSuchElementException();
        }
    }

}