14941fb89eb76a5bc1c7d5458370246b32560ab3
[Sone.git] / src / main / java / net / pterodactylus / sone / text / PartContainer.java
1 /*
2  * Sone - PartContainer.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.text;
19
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;
26
27 /**
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.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class PartContainer implements Part, Iterable<Part> {
35
36         /** The parts to render. */
37         private final List<Part> parts = new ArrayList<Part>();
38
39         //
40         // ACCESSORS
41         //
42
43         /**
44          * Adds a part to render.
45          *
46          * @param part
47          *            The part to add
48          */
49         public void add(Part part) {
50                 parts.add(part);
51         }
52
53         /**
54          * Returns the part at the given index.
55          *
56          * @param index
57          *            The index of the part
58          * @return The part
59          */
60         public Part getPart(int index) {
61                 return parts.get(index);
62         }
63
64         /**
65          * Removes the part at the given index.
66          *
67          * @param index
68          *            The index of the part to remove
69          */
70         public void removePart(int index) {
71                 parts.remove(index);
72         }
73
74         /**
75          * Returns the number of parts.
76          *
77          * @return The number of parts
78          */
79         public int size() {
80                 return parts.size();
81         }
82
83         //
84         // PART METHODS
85         //
86
87         @Override
88         public String getText() {
89                 StringBuilder partText = new StringBuilder();
90                 for (Part part : parts) {
91                         partText.append(part.getText());
92                 }
93                 return partText.toString();
94         }
95
96         //
97         // ITERABLE METHODS
98         //
99
100         @Override
101         @SuppressWarnings("synthetic-access")
102         public Iterator<Part> iterator() {
103                 return new Iterator<Part>() {
104
105                         private Deque<Iterator<Part>> partStack = new ArrayDeque<Iterator<Part>>();
106                         private Part nextPart;
107                         private boolean foundNextPart;
108                         private boolean noNextPart;
109
110                         {
111                                 partStack.push(parts.iterator());
112                         }
113
114                         private void findNext() {
115                                 if (foundNextPart) {
116                                         return;
117                                 }
118                                 noNextPart = true;
119                                 while (!partStack.isEmpty()) {
120                                         Iterator<Part> parts = partStack.pop();
121                                         if (parts.hasNext()) {
122                                                 nextPart = parts.next();
123                                                 partStack.push(parts);
124                                                 if (nextPart instanceof PartContainer) {
125                                                         partStack.push(((PartContainer) nextPart).iterator());
126                                                 } else {
127                                                         noNextPart = false;
128                                                         break;
129                                                 }
130                                         }
131                                 }
132                                 foundNextPart = true;
133                         }
134
135                         @Override
136                         public boolean hasNext() {
137                                 findNext();
138                                 return !noNextPart;
139                         }
140
141                         @Override
142                         public Part next() {
143                                 findNext();
144                                 if (noNextPart) {
145                                         throw new NoSuchElementException();
146                                 }
147                                 foundNextPart = false;
148                                 return nextPart;
149                         }
150
151                         @Override
152                         public void remove() {
153                                 /* ignore. */
154                         }
155
156                 };
157         }
158
159 }