X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FPartContainer.java;h=14941fb89eb76a5bc1c7d5458370246b32560ab3;hb=83e875756e1a2afdcedba937734500ef1e452069;hp=4993324444a46280500948ad05e8f11dc30b65ab;hpb=0981fcac71392365839d313ae8cb43bf15908349;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/text/PartContainer.java b/src/main/java/net/pterodactylus/sone/text/PartContainer.java
index 4993324..14941fb 100644
--- a/src/main/java/net/pterodactylus/sone/text/PartContainer.java
+++ b/src/main/java/net/pterodactylus/sone/text/PartContainer.java
@@ -1,5 +1,5 @@
/*
- * Sone - PartContainer.java - Copyright © 2010 David Roden
+ * Sone - PartContainer.java - Copyright © 2010â2013 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,11 +17,12 @@
package net.pterodactylus.sone.text;
-import java.io.IOException;
-import java.io.StringWriter;
-import java.io.Writer;
+import java.util.ArrayDeque;
import java.util.ArrayList;
+import java.util.Deque;
+import java.util.Iterator;
import java.util.List;
+import java.util.NoSuchElementException;
/**
* Part implementation that can contain an arbitrary amount of other parts.
@@ -30,7 +31,7 @@ import java.util.List;
*
* @author David âBombeâ Roden
*/
-public class PartContainer implements Part {
+public class PartContainer implements Part, Iterable {
/** The parts to render. */
private final List parts = new ArrayList();
@@ -83,29 +84,76 @@ public class PartContainer implements Part {
// PART METHODS
//
- /**
- * {@inheritDoc}
- */
@Override
- public void render(Writer writer) throws IOException {
+ public String getText() {
+ StringBuilder partText = new StringBuilder();
for (Part part : parts) {
- part.render(writer);
+ partText.append(part.getText());
}
+ return partText.toString();
}
//
- // OBJECT METHODS
+ // ITERABLE METHODS
//
@Override
- public String toString() {
- StringWriter stringWriter = new StringWriter();
- try {
- render(stringWriter);
- } catch (IOException ioe1) {
- /* should never throw, ignore. */
- }
- return stringWriter.toString();
+ @SuppressWarnings("synthetic-access")
+ public Iterator iterator() {
+ return new Iterator() {
+
+ private Deque> partStack = new ArrayDeque>();
+ private Part nextPart;
+ private boolean foundNextPart;
+ private boolean noNextPart;
+
+ {
+ partStack.push(parts.iterator());
+ }
+
+ private void findNext() {
+ if (foundNextPart) {
+ return;
+ }
+ noNextPart = true;
+ while (!partStack.isEmpty()) {
+ Iterator parts = partStack.pop();
+ if (parts.hasNext()) {
+ nextPart = parts.next();
+ partStack.push(parts);
+ if (nextPart instanceof PartContainer) {
+ partStack.push(((PartContainer) nextPart).iterator());
+ } else {
+ noNextPart = false;
+ break;
+ }
+ }
+ }
+ foundNextPart = true;
+ }
+
+ @Override
+ public boolean hasNext() {
+ findNext();
+ return !noNextPart;
+ }
+
+ @Override
+ public Part next() {
+ findNext();
+ if (noNextPart) {
+ throw new NoSuchElementException();
+ }
+ foundNextPart = false;
+ return nextPart;
+ }
+
+ @Override
+ public void remove() {
+ /* ignore. */
+ }
+
+ };
}
}