Merge branch 'release-0.9.6'
[Sone.git] / src / main / java / net / pterodactylus / sone / text / PartContainer.java
index d52658e..3444d19 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - PartContainer.java - Copyright © 2010 David Roden
+ * Sone - PartContainer.java - Copyright © 2010–2016 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
 
 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;
+import java.util.Objects;
+
+import javax.annotation.Nonnull;
 
 /**
  * Part implementation that can contain an arbitrary amount of other parts.
@@ -30,85 +34,95 @@ import java.util.List;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class PartContainer implements Part {
+public class PartContainer implements Part, Iterable<Part> {
 
-       /** The parts to render. */
        private final List<Part> parts = new ArrayList<Part>();
 
-       //
-       // ACCESSORS
-       //
-
-       /**
-        * Adds a part to render.
-        *
-        * @param part
-        *            The part to add
-        */
-       public void add(Part part) {
-               parts.add(part);
+       public void add(@Nonnull Part part) {
+               parts.add(Objects.requireNonNull(part));
        }
 
-       /**
-        * Returns the part at the given index.
-        *
-        * @param index
-        *            The index of the part
-        * @return The part
-        */
+       @Nonnull
        public Part getPart(int index) {
                return parts.get(index);
        }
 
-       /**
-        * Removes the part at the given index.
-        *
-        * @param index
-        *            The index of the part to remove
-        */
        public void removePart(int index) {
                parts.remove(index);
        }
 
-       /**
-        * Returns the number of parts.
-        *
-        * @return The number of parts
-        */
        public int size() {
                return parts.size();
        }
 
-       //
-       // PART METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
        @Override
-       public void render(Writer writer) throws IOException {
+       @Nonnull
+       public String getText() {
+               StringBuilder partText = new StringBuilder();
                for (Part part : parts) {
-                       part.render(writer);
+                       partText.append(part.getText());
                }
+               return partText.toString();
        }
 
-       //
-       // OBJECT METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
        @Override
-       public String toString() {
-               StringWriter stringWriter = new StringWriter();
-               try {
-                       render(stringWriter);
-               } catch (IOException ioe1) {
-                       /* should never throw, ignore. */
-               }
-               return stringWriter.toString();
+       @Nonnull
+       @SuppressWarnings("synthetic-access")
+       public Iterator<Part> iterator() {
+               return new Iterator<Part>() {
+
+                       private Deque<Iterator<Part>> partStack = new ArrayDeque<Iterator<Part>>();
+                       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<Part> 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. */
+                       }
+
+               };
        }
 
 }