2 * Sone - PartContainer.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.text;
20 import java.util.ArrayDeque;
21 import java.util.ArrayList;
22 import java.util.Deque;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.NoSuchElementException;
28 * Part implementation that can contain an arbitrary amount of other parts.
29 * Parts are added using the {@link #add(Part)} method and will be rendered in
30 * the order they are added.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class PartContainer implements Part, Iterable<Part> {
36 /** The parts to render. */
37 private final List<Part> parts = new ArrayList<Part>();
44 * Adds a part to render.
49 public void add(Part part) {
54 * Returns the part at the given index.
57 * The index of the part
60 public Part getPart(int index) {
61 return parts.get(index);
65 * Removes the part at the given index.
68 * The index of the part to remove
70 public void removePart(int index) {
75 * Returns the number of parts.
77 * @return The number of parts
91 public String getText() {
92 StringBuilder partText = new StringBuilder();
93 for (Part part : parts) {
94 partText.append(part.getText());
96 return partText.toString();
107 @SuppressWarnings("synthetic-access")
108 public Iterator<Part> iterator() {
109 return new Iterator<Part>() {
111 private Deque<Iterator<Part>> partStack = new ArrayDeque<Iterator<Part>>();
112 private Part nextPart;
113 private boolean foundNextPart;
114 private boolean noNextPart;
117 partStack.push(parts.iterator());
120 private void findNext() {
125 while (!partStack.isEmpty()) {
126 Iterator<Part> parts = partStack.pop();
127 if (parts.hasNext()) {
128 nextPart = parts.next();
129 partStack.push(parts);
130 if (nextPart instanceof PartContainer) {
131 partStack.push(((PartContainer) nextPart).iterator());
138 foundNextPart = true;
142 public boolean hasNext() {
151 throw new NoSuchElementException();
153 foundNextPart = false;
158 public void remove() {