1 package org.apache.maven.doxia.index;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * <p>IndexEntry class.</p>
30 *
31 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
32 * @version $Id: IndexEntry.java 1438266 2013-01-24 23:34:44Z olamy $
33 */
34 public class IndexEntry
35 {
36 /**
37 * The parent entry.
38 */
39 private final IndexEntry parent;
40
41 /**
42 * The id of the entry.
43 */
44 private String id;
45
46 /**
47 * The entry title.
48 */
49 private String title;
50
51 /**
52 * The child entries.
53 */
54 private List<IndexEntry> childEntries = new ArrayList<IndexEntry>();
55
56 /**
57 * System-dependent EOL.
58 */
59 private static final String EOL = System.getProperty( "line.separator" );
60
61 /**
62 * Constructor.
63 *
64 * @param newId The id. May be null.
65 */
66 public IndexEntry( String newId )
67 {
68 this( null, newId );
69 }
70
71 /**
72 * Constructor.
73 *
74 * @param newParent The parent. May be null.
75 * @param newId The id. May be null.
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 * Returns the parent entry.
90 *
91 * @return the parent entry.
92 */
93 public IndexEntry getParent()
94 {
95 return parent;
96 }
97
98 /**
99 * Returns the id.
100 *
101 * @return the id.
102 */
103 public String getId()
104 {
105 return id;
106 }
107
108 /**
109 * Set the id.
110 *
111 * @since 1.1.2
112 */
113 protected void setId( String id )
114 {
115 this.id = id;
116 }
117
118 /**
119 * Returns the title.
120 *
121 * @return the title.
122 */
123 public String getTitle()
124 {
125 return title;
126 }
127
128 /**
129 * Sets the title.
130 *
131 * @param newTitle the title.
132 */
133 public void setTitle( String newTitle )
134 {
135 this.title = newTitle;
136 }
137
138 /**
139 * Returns an unmodifiableList of the child entries.
140 *
141 * @return child entries.
142 */
143 public List<IndexEntry> getChildEntries()
144 {
145 return Collections.unmodifiableList( childEntries );
146 }
147
148 /**
149 * Sets the child entries or creates a new ArrayList if entries == null.
150 *
151 * @param entries the entries.
152 */
153 public void setChildEntries( List<IndexEntry> entries )
154 {
155 if ( entries == null )
156 {
157 childEntries = new ArrayList<IndexEntry>();
158 }
159
160 this.childEntries = entries;
161 }
162
163 // -----------------------------------------------------------------------
164 // Utils
165 // -----------------------------------------------------------------------
166
167 /**
168 * Returns the next entry.
169 *
170 * @return the next entry, or null if there is none.
171 */
172 public IndexEntry getNextEntry()
173 {
174 if ( parent == null )
175 {
176 return null;
177 }
178
179 List<IndexEntry> entries = parent.getChildEntries();
180
181 int index = entries.indexOf( this );
182
183 if ( index + 1 >= entries.size() )
184 {
185 return null;
186 }
187
188 return entries.get( index + 1 );
189 }
190
191 /**
192 * Returns the previous entry.
193 *
194 * @return the previous entry, or null if there is none.
195 */
196 public IndexEntry getPrevEntry()
197 {
198 if ( parent == null )
199 {
200 return null;
201 }
202
203 List<IndexEntry> entries = parent.getChildEntries();
204
205 int index = entries.indexOf( this );
206
207 if ( index == 0 )
208 {
209 return null;
210 }
211
212 return entries.get( index - 1 );
213 }
214
215 /**
216 * Returns the first entry.
217 *
218 * @return the first entry, or null if there is none.
219 */
220 public IndexEntry getFirstEntry()
221 {
222 List<IndexEntry> entries = getChildEntries();
223
224 if ( entries.size() == 0 )
225 {
226 return null;
227 }
228
229 return entries.get( 0 );
230 }
231
232 /**
233 * Returns the last entry.
234 *
235 * @return the last entry, or null if there is none.
236 */
237 public IndexEntry getLastEntry()
238 {
239 List<IndexEntry> entries = getChildEntries();
240
241 if ( entries.size() == 0 )
242 {
243 return null;
244 }
245
246 return entries.get( entries.size() - 1 );
247 }
248
249 /**
250 * Returns the root entry.
251 *
252 * @return the root entry, or null if there is none.
253 */
254 public IndexEntry getRootEntry()
255 {
256 List<IndexEntry> entries = getChildEntries();
257
258 if ( entries.size() == 0 )
259 {
260 return null;
261 }
262 else if ( entries.size() > 1 )
263 {
264 throw new RuntimeException( "This index has more than one root entry" );
265 }
266 else
267 {
268 return entries.get( 0 );
269 }
270 }
271
272 // -----------------------------------------------------------------------
273 // Object Overrides
274 // -----------------------------------------------------------------------
275
276 /**
277 * {@inheritDoc}
278 * <p/>
279 * Returns a string representation of the object.
280 */
281 public String toString()
282 {
283 return toString( 0 );
284 }
285
286 /**
287 * Returns a string representation of all objects to the given depth.
288 *
289 * @param depth The depth to descent to.
290 * @return A string.
291 */
292 public String toString( int depth )
293 {
294 StringBuilder message = new StringBuilder();
295
296 message.append( "Id: " ).append( id );
297
298 if ( StringUtils.isNotEmpty( title ) )
299 {
300 message.append( ", title: " ).append( title );
301 }
302
303 message.append( EOL );
304
305 StringBuilder indent = new StringBuilder( "" );
306
307 for ( int i = 0; i < depth; i++ )
308 {
309 indent.append( " " );
310 }
311
312 for ( IndexEntry entry : getChildEntries() )
313 {
314 message.append( indent ).append( entry.toString( depth + 1 ) );
315 }
316
317 return message.toString();
318 }
319 }