diff --git a/src/main/java/org/verapdf/pd/PDNameTreeNode.java b/src/main/java/org/verapdf/pd/PDNameTreeNode.java index e50ac69a..36f7dcb3 100644 --- a/src/main/java/org/verapdf/pd/PDNameTreeNode.java +++ b/src/main/java/org/verapdf/pd/PDNameTreeNode.java @@ -26,7 +26,6 @@ import org.verapdf.cos.COSObjType; import org.verapdf.cos.COSObject; import org.verapdf.exceptions.LoopedException; -import org.verapdf.pd.structure.NameTreeIterator; import java.util.*; @@ -165,14 +164,22 @@ private COSObject getObject(String key, Set visitedKeys) { return null; } + private List getObjects() { + List result = new LinkedList<>(getNames().values()); + for (PDNameTreeNode kid : getKids()) { + result.addAll(kid.getObjects()); + } + return result; + } + @Override - public NameTreeIterator iterator() { - return new NameTreeIterator(this); + public Iterator iterator() { + return getObjects().iterator(); } public Long size() { long i = 0; - NameTreeIterator iterator = iterator(); + Iterator iterator = iterator(); for (; iterator.hasNext(); i++) { iterator.next(); } diff --git a/src/main/java/org/verapdf/pd/structure/NameTreeIterator.java b/src/main/java/org/verapdf/pd/structure/NameTreeIterator.java deleted file mode 100644 index b9d81c3c..00000000 --- a/src/main/java/org/verapdf/pd/structure/NameTreeIterator.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.verapdf.pd.structure; - -import org.verapdf.cos.COSObject; -import org.verapdf.pd.PDNameTreeNode; - -import java.util.Iterator; - -public class NameTreeIterator implements Iterator { - - private NameTreeIterator innerCurrentIterator; - private final Iterator namesIterator; - private final Iterator kidsIterator; - - public NameTreeIterator(PDNameTreeNode root) { - namesIterator = root.getNames().values().iterator(); - kidsIterator = root.getKids().iterator(); - nextInnerIterator(); - } - - @Override - public boolean hasNext() { - return (innerCurrentIterator != null && innerCurrentIterator.hasNext()) || kidsIterator.hasNext() || namesIterator.hasNext(); - } - - @Override - public COSObject next() { - if (namesIterator.hasNext()) { - return namesIterator.next(); - } - if (!innerCurrentIterator.hasNext()) { - nextInnerIterator(); - } - return innerCurrentIterator.next(); - } - - private void nextInnerIterator() { - innerCurrentIterator = kidsIterator.hasNext() ? new NameTreeIterator(kidsIterator.next()) : null; - } -} diff --git a/src/main/java/org/verapdf/pd/structure/NumberTreeIterator.java b/src/main/java/org/verapdf/pd/structure/NumberTreeIterator.java deleted file mode 100644 index 2cdcbaaa..00000000 --- a/src/main/java/org/verapdf/pd/structure/NumberTreeIterator.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.verapdf.pd.structure; - -import org.verapdf.cos.COSObject; - -import java.util.Iterator; - -public class NumberTreeIterator implements Iterator { - - private NumberTreeIterator innerCurrentIterator; - private final Iterator numbersIterator; - private final Iterator kidsIterator; - - public NumberTreeIterator(PDNumberTreeNode root) { - numbersIterator = root.getNums().values().iterator(); - kidsIterator = root.getKids().iterator(); - nextInnerIterator(); - } - - @Override - public boolean hasNext() { - return (innerCurrentIterator != null && innerCurrentIterator.hasNext()) || kidsIterator.hasNext() || numbersIterator.hasNext(); - } - - @Override - public COSObject next() { - if (numbersIterator.hasNext()) { - return numbersIterator.next(); - } - if (!innerCurrentIterator.hasNext()) { - nextInnerIterator(); - } - return innerCurrentIterator.next(); - } - - private void nextInnerIterator() { - innerCurrentIterator = kidsIterator.hasNext() ? new NumberTreeIterator(kidsIterator.next()) : null; - } -} diff --git a/src/main/java/org/verapdf/pd/structure/PDNumberTreeNode.java b/src/main/java/org/verapdf/pd/structure/PDNumberTreeNode.java index 91334ae8..b9f03b9f 100644 --- a/src/main/java/org/verapdf/pd/structure/PDNumberTreeNode.java +++ b/src/main/java/org/verapdf/pd/structure/PDNumberTreeNode.java @@ -100,6 +100,14 @@ public Map getNums() { return Collections.emptyMap(); } + private List getObjects() { + List result = new LinkedList<>(getNums().values()); + for (PDNumberTreeNode kid : getKids()) { + result.addAll(kid.getObjects()); + } + return result; + } + /** * Gets object with given key from this node and it's kids recursively. * @@ -157,13 +165,13 @@ private COSObject getObject(Long key, Set visitedKeys) { } @Override - public NumberTreeIterator iterator() { - return new NumberTreeIterator(this); + public Iterator iterator() { + return getObjects().iterator(); } public Long size() { long i = 0; - NumberTreeIterator iterator = iterator(); + Iterator iterator = iterator(); for (; iterator.hasNext(); i++) { iterator.next(); }