Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IndexEntry |
|
| 2.3125;2,312 |
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 | 42 | private List<IndexEntry> childEntries = new ArrayList<IndexEntry>(); |
47 | ||
48 | /** System-dependent EOL. */ | |
49 | 2 | 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 | 8 | this( null, newId ); |
59 | 8 | } |
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 | 42 | { |
69 | 42 | this.parent = newParent; |
70 | 42 | this.id = newId; |
71 | ||
72 | 42 | if ( parent != null ) |
73 | { | |
74 | 34 | parent.childEntries.add( this ); |
75 | } | |
76 | 42 | } |
77 | ||
78 | /** | |
79 | * Returns the parent entry. | |
80 | * | |
81 | * @return the parent entry. | |
82 | */ | |
83 | public IndexEntry getParent() | |
84 | { | |
85 | 12 | return parent; |
86 | } | |
87 | ||
88 | /** | |
89 | * Returns the id. | |
90 | * | |
91 | * @return the id. | |
92 | */ | |
93 | public String getId() | |
94 | { | |
95 | 24 | return id; |
96 | } | |
97 | ||
98 | /** | |
99 | * Set the id. | |
100 | * | |
101 | * @since 1.1.2 | |
102 | */ | |
103 | protected void setId( String id ) | |
104 | { | |
105 | 6 | this.id = id; |
106 | 6 | } |
107 | ||
108 | /** | |
109 | * Returns the title. | |
110 | * | |
111 | * @return the title. | |
112 | */ | |
113 | public String getTitle() | |
114 | { | |
115 | 30 | return title; |
116 | } | |
117 | ||
118 | /** | |
119 | * Sets the title. | |
120 | * | |
121 | * @param newTitle the title. | |
122 | */ | |
123 | public void setTitle( String newTitle ) | |
124 | { | |
125 | 38 | this.title = newTitle; |
126 | 38 | } |
127 | ||
128 | /** | |
129 | * Returns an unmodifiableList of the child entries. | |
130 | * | |
131 | * @return child entries. | |
132 | */ | |
133 | public List<IndexEntry> getChildEntries() | |
134 | { | |
135 | 70 | 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 | 0 | if ( entries == null ) |
146 | { | |
147 | 0 | childEntries = new ArrayList<IndexEntry>(); |
148 | } | |
149 | ||
150 | 0 | this.childEntries = entries; |
151 | 0 | } |
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 | 12 | if ( parent == null ) |
165 | { | |
166 | 6 | return null; |
167 | } | |
168 | ||
169 | 6 | List<IndexEntry> entries = parent.getChildEntries(); |
170 | ||
171 | 6 | int index = entries.indexOf( this ); |
172 | ||
173 | 6 | if ( index + 1 >= entries.size() ) |
174 | { | |
175 | 4 | return null; |
176 | } | |
177 | ||
178 | 2 | 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 | 12 | if ( parent == null ) |
189 | { | |
190 | 6 | return null; |
191 | } | |
192 | ||
193 | 6 | List<IndexEntry> entries = parent.getChildEntries(); |
194 | ||
195 | 6 | int index = entries.indexOf( this ); |
196 | ||
197 | 6 | if ( index == 0 ) |
198 | { | |
199 | 4 | return null; |
200 | } | |
201 | ||
202 | 2 | 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 | 0 | List<IndexEntry> entries = getChildEntries(); |
213 | ||
214 | 0 | if ( entries.size() == 0 ) |
215 | { | |
216 | 0 | return null; |
217 | } | |
218 | ||
219 | 0 | 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 | 0 | List<IndexEntry> entries = getChildEntries(); |
230 | ||
231 | 0 | if ( entries.size() == 0 ) |
232 | { | |
233 | 0 | return null; |
234 | } | |
235 | ||
236 | 0 | 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 | 0 | List<IndexEntry> entries = getChildEntries(); |
247 | ||
248 | 0 | if ( entries.size() == 0 ) |
249 | { | |
250 | 0 | return null; |
251 | } | |
252 | 0 | else if ( entries.size() > 1 ) |
253 | { | |
254 | 0 | throw new RuntimeException( "This index has more than one root entry" ); |
255 | } | |
256 | else | |
257 | { | |
258 | 0 | 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 | 2 | 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 | 2 | StringBuffer message = new StringBuffer(); |
285 | ||
286 | 2 | message.append( "Id: " ).append( id ); |
287 | ||
288 | 2 | if ( StringUtils.isNotEmpty( title ) ) |
289 | { | |
290 | 2 | message.append( ", title: " ).append( title ); |
291 | } | |
292 | ||
293 | 2 | message.append( EOL ); |
294 | ||
295 | 2 | String indent = ""; |
296 | ||
297 | 2 | for ( int i = 0; i < depth; i++ ) |
298 | { | |
299 | 0 | indent += " "; |
300 | } | |
301 | ||
302 | 2 | for ( IndexEntry entry : getChildEntries() ) |
303 | { | |
304 | 0 | message.append( indent ).append( entry.toString( depth + 1 ) ); |
305 | } | |
306 | ||
307 | 2 | return message.toString(); |
308 | } | |
309 | } |