Rework the text parser.
[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         @Override
44         public boolean isPlainText() {
45                 return false;
46         }
47
48         @Override
49         public boolean isFreenetLink() {
50                 return false;
51         }
52
53         /**
54          * Adds a part to render.
55          *
56          * @param part
57          *            The part to add
58          */
59         public void add(Part part) {
60                 parts.add(part);
61         }
62
63         /**
64          * Returns the part at the given index.
65          *
66          * @param index
67          *            The index of the part
68          * @return The part
69          */
70         public Part getPart(int index) {
71                 return parts.get(index);
72         }
73
74         /**
75          * Removes the part at the given index.
76          *
77          * @param index
78          *            The index of the part to remove
79          */
80         public void removePart(int index) {
81                 parts.remove(index);
82         }
83
84         /**
85          * Returns the number of parts.
86          *
87          * @return The number of parts
88          */
89         public int size() {
90                 return parts.size();
91         }
92
93         //
94         // PART METHODS
95         //
96
97         @Override
98         public String getText() {
99                 StringBuilder partText = new StringBuilder();
100                 for (Part part : parts) {
101                         partText.append(part.getText());
102                 }
103                 return partText.toString();
104         }
105
106         //
107         // ITERABLE METHODS
108         //
109
110         @Override
111         @SuppressWarnings("synthetic-access")
112         public Iterator<Part> iterator() {
113                 return new Iterator<Part>() {
114
115                         private Deque<Iterator<Part>> partStack = new ArrayDeque<Iterator<Part>>();
116                         private Part nextPart;
117                         private boolean foundNextPart;
118                         private boolean noNextPart;
119
120                         {
121                                 partStack.push(parts.iterator());
122                         }
123
124                         private void findNext() {
125                                 if (foundNextPart) {
126                                         return;
127                                 }
128                                 noNextPart = true;
129                                 while (!partStack.isEmpty()) {
130                                         Iterator<Part> parts = partStack.pop();
131                                         if (parts.hasNext()) {
132                                                 nextPart = parts.next();
133                                                 partStack.push(parts);
134                                                 if (nextPart instanceof PartContainer) {
135                                                         partStack.push(((PartContainer) nextPart).iterator());
136                                                 } else {
137                                                         noNextPart = false;
138                                                         break;
139                                                 }
140                                         }
141                                 }
142                                 foundNextPart = true;
143                         }
144
145                         @Override
146                         public boolean hasNext() {
147                                 findNext();
148                                 return !noNextPart;
149                         }
150
151                         @Override
152                         public Part next() {
153                                 findNext();
154                                 if (noNextPart) {
155                                         throw new NoSuchElementException();
156                                 }
157                                 foundNextPart = false;
158                                 return nextPart;
159                         }
160
161                         @Override
162                         public void remove() {
163                                 /* ignore. */
164                         }
165
166                 };
167         }
168
169 }