1 package org.apache.maven.doxia.index;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23 import java.util.ArrayList;
24 import java.util.Collections;
25
26 import org.codehaus.plexus.util.StringUtils;
27
28
29
30
31
32
33
34 public class IndexEntry
35 {
36
37
38
39 private final IndexEntry parent;
40
41
42
43
44 private String id;
45
46
47
48
49 private String title;
50
51
52
53
54 private List<IndexEntry> childEntries = new ArrayList<>();
55
56
57
58
59 private static final String EOL = System.getProperty( "line.separator" );
60
61
62
63
64
65
66 public IndexEntry( String newId )
67 {
68 this( null, newId );
69 }
70
71
72
73
74
75
76
77 public IndexEntry( IndexEntry newParent, String newId )
78 {
79 this.parent = newParent;
80 this.id = newId;
81
82 if ( parent != null )
83 {
84 parent.childEntries.add( this );
85 }
86 }
87
88
89
90
91
92
93 public IndexEntry getParent()
94 {
95 return parent;
96 }
97
98
99
100
101
102
103 public String getId()
104 {
105 return id;
106 }
107
108
109
110
111
112
113
114 protected void setId( String id )
115 {
116 this.id = id;
117 }
118
119
120
121
122
123
124 public String getTitle()
125 {
126 return title;
127 }
128
129
130
131
132
133
134 public void setTitle( String newTitle )
135 {
136 this.title = newTitle;
137 }
138
139
140
141
142
143
144 public List<IndexEntry> getChildEntries()
145 {
146 return Collections.unmodifiableList( childEntries );
147 }
148
149
150
151
152
153
154 public void setChildEntries( List<IndexEntry> entries )
155 {
156 if ( entries == null )
157 {
158 childEntries = new ArrayList<>();
159 }
160
161 this.childEntries = entries;
162 }
163
164
165
166
167
168
169
170
171
172
173 public IndexEntry getNextEntry()
174 {
175 if ( parent == null )
176 {
177 return null;
178 }
179
180 List<IndexEntry> entries = parent.getChildEntries();
181
182 int index = entries.indexOf( this );
183
184 if ( index + 1 >= entries.size() )
185 {
186 return null;
187 }
188
189 return entries.get( index + 1 );
190 }
191
192
193
194
195
196
197 public IndexEntry getPrevEntry()
198 {
199 if ( parent == null )
200 {
201 return null;
202 }
203
204 List<IndexEntry> entries = parent.getChildEntries();
205
206 int index = entries.indexOf( this );
207
208 if ( index == 0 )
209 {
210 return null;
211 }
212
213 return entries.get( index - 1 );
214 }
215
216
217
218
219
220
221 public IndexEntry getFirstEntry()
222 {
223 List<IndexEntry> entries = getChildEntries();
224
225 if ( entries.size() == 0 )
226 {
227 return null;
228 }
229
230 return entries.get( 0 );
231 }
232
233
234
235
236
237
238 public IndexEntry getLastEntry()
239 {
240 List<IndexEntry> entries = getChildEntries();
241
242 if ( entries.size() == 0 )
243 {
244 return null;
245 }
246
247 return entries.get( entries.size() - 1 );
248 }
249
250
251
252
253
254
255 public IndexEntry getRootEntry()
256 {
257 List<IndexEntry> entries = getChildEntries();
258
259 if ( entries.size() == 0 )
260 {
261 return null;
262 }
263 else if ( entries.size() > 1 )
264 {
265 throw new RuntimeException( "This index has more than one root entry" );
266 }
267 else
268 {
269 return entries.get( 0 );
270 }
271 }
272
273
274
275
276
277
278
279
280 public String toString()
281 {
282 return toString( 0 );
283 }
284
285
286
287
288
289
290
291 public String toString( int depth )
292 {
293 StringBuilder message = new StringBuilder();
294
295 message.append( "Id: " ).append( id );
296
297 if ( StringUtils.isNotEmpty( title ) )
298 {
299 message.append( ", title: " ).append( title );
300 }
301
302 message.append( EOL );
303
304 StringBuilder indent = new StringBuilder();
305
306 for ( int i = 0; i < depth; i++ )
307 {
308 indent.append( " " );
309 }
310
311 for ( IndexEntry entry : getChildEntries() )
312 {
313 message.append( indent ).append( entry.toString( depth + 1 ) );
314 }
315
316 return message.toString();
317 }
318 }