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 1090706 2011-04-09 23:15:28Z hboutemy $
33 */
34 public class IndexEntry
35 {
36 /** The parent entry. */
37 private final IndexEntry parent;
38
39 /** The id of the entry. */
40 private String id;
41
42 /** The entry title. */
43 private String title;
44
45 /** The child entries. */
46 private List<IndexEntry> childEntries = new ArrayList<IndexEntry>();
47
48 /** System-dependent EOL. */
49 private static final String EOL = System.getProperty( "line.separator" );
50
51 /**
52 * Constructor.
53 *
54 * @param newId The id. May be null.
55 */
56 public IndexEntry( String newId )
57 {
58 this( null, newId );
59 }
60
61 /**
62 * Constructor.
63 *
64 * @param newParent The parent. May be null.
65 * @param newId The id. May be null.
66 */
67 public IndexEntry( IndexEntry newParent, String newId )
68 {
69 this.parent = newParent;
70 this.id = newId;
71
72 if ( parent != null )
73 {
74 parent.childEntries.add( this );
75 }
76 }
77
78 /**
79 * Returns the parent entry.
80 *
81 * @return the parent entry.
82 */
83 public IndexEntry getParent()
84 {
85 return parent;
86 }
87
88 /**
89 * Returns the id.
90 *
91 * @return the id.
92 */
93 public String getId()
94 {
95 return id;
96 }
97
98 /**
99 * Set the id.
100 *
101 * @since 1.1.2
102 */
103 protected void setId( String id )
104 {
105 this.id = id;
106 }
107
108 /**
109 * Returns the title.
110 *
111 * @return the title.
112 */
113 public String getTitle()
114 {
115 return title;
116 }
117
118 /**
119 * Sets the title.
120 *
121 * @param newTitle the title.
122 */
123 public void setTitle( String newTitle )
124 {
125 this.title = newTitle;
126 }
127
128 /**
129 * Returns an unmodifiableList of the child entries.
130 *
131 * @return child entries.
132 */
133 public List<IndexEntry> getChildEntries()
134 {
135 return Collections.unmodifiableList( childEntries );
136 }
137
138 /**
139 * Sets the child entries or creates a new ArrayList if entries == null.
140 *
141 * @param entries the entries.
142 */
143 public void setChildEntries( List<IndexEntry> entries )
144 {
145 if ( entries == null )
146 {
147 childEntries = new ArrayList<IndexEntry>();
148 }
149
150 this.childEntries = entries;
151 }
152
153 // -----------------------------------------------------------------------
154 // Utils
155 // -----------------------------------------------------------------------
156
157 /**
158 * Returns the next entry.
159 *
160 * @return the next entry, or null if there is none.
161 */
162 public IndexEntry getNextEntry()
163 {
164 if ( parent == null )
165 {
166 return null;
167 }
168
169 List<IndexEntry> entries = parent.getChildEntries();
170
171 int index = entries.indexOf( this );
172
173 if ( index + 1 >= entries.size() )
174 {
175 return null;
176 }
177
178 return entries.get( index + 1 );
179 }
180
181 /**
182 * Returns the previous entry.
183 *
184 * @return the previous entry, or null if there is none.
185 */
186 public IndexEntry getPrevEntry()
187 {
188 if ( parent == null )
189 {
190 return null;
191 }
192
193 List<IndexEntry> entries = parent.getChildEntries();
194
195 int index = entries.indexOf( this );
196
197 if ( index == 0 )
198 {
199 return null;
200 }
201
202 return entries.get( index - 1 );
203 }
204
205 /**
206 * Returns the first entry.
207 *
208 * @return the first entry, or null if there is none.
209 */
210 public IndexEntry getFirstEntry()
211 {
212 List<IndexEntry> entries = getChildEntries();
213
214 if ( entries.size() == 0 )
215 {
216 return null;
217 }
218
219 return entries.get( 0 );
220 }
221
222 /**
223 * Returns the last entry.
224 *
225 * @return the last entry, or null if there is none.
226 */
227 public IndexEntry getLastEntry()
228 {
229 List<IndexEntry> entries = getChildEntries();
230
231 if ( entries.size() == 0 )
232 {
233 return null;
234 }
235
236 return entries.get( entries.size() - 1 );
237 }
238
239 /**
240 * Returns the root entry.
241 *
242 * @return the root entry, or null if there is none.
243 */
244 public IndexEntry getRootEntry()
245 {
246 List<IndexEntry> entries = getChildEntries();
247
248 if ( entries.size() == 0 )
249 {
250 return null;
251 }
252 else if ( entries.size() > 1 )
253 {
254 throw new RuntimeException( "This index has more than one root entry" );
255 }
256 else
257 {
258 return entries.get( 0 );
259 }
260 }
261
262 // -----------------------------------------------------------------------
263 // Object Overrides
264 // -----------------------------------------------------------------------
265
266 /**
267 * {@inheritDoc}
268 *
269 * Returns a string representation of the object.
270 */
271 public String toString()
272 {
273 return toString( 0 );
274 }
275
276 /**
277 * Returns a string representation of all objects to the given depth.
278 *
279 * @param depth The depth to descent to.
280 * @return A string.
281 */
282 public String toString( int depth )
283 {
284 StringBuffer message = new StringBuffer();
285
286 message.append( "Id: " ).append( id );
287
288 if ( StringUtils.isNotEmpty( title ) )
289 {
290 message.append( ", title: " ).append( title );
291 }
292
293 message.append( EOL );
294
295 String indent = "";
296
297 for ( int i = 0; i < depth; i++ )
298 {
299 indent += " ";
300 }
301
302 for ( IndexEntry entry : getChildEntries() )
303 {
304 message.append( indent ).append( entry.toString( depth + 1 ) );
305 }
306
307 return message.toString();
308 }
309 }