View Javadoc
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&oslash;l</a>
32   * @version $Id$
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<>();
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      * @param id the id
112      * @since 1.1.2
113      */
114     protected void setId( String id )
115     {
116         this.id = id;
117     }
118 
119     /**
120      * Returns the title.
121      *
122      * @return the title.
123      */
124     public String getTitle()
125     {
126         return title;
127     }
128 
129     /**
130      * Sets the title.
131      *
132      * @param newTitle the title.
133      */
134     public void setTitle( String newTitle )
135     {
136         this.title = newTitle;
137     }
138 
139     /**
140      * Returns an unmodifiableList of the child entries.
141      *
142      * @return child entries.
143      */
144     public List<IndexEntry> getChildEntries()
145     {
146         return Collections.unmodifiableList( childEntries );
147     }
148 
149     /**
150      * Sets the child entries or creates a new ArrayList if entries == null.
151      *
152      * @param entries the entries.
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     // Utils
166     // -----------------------------------------------------------------------
167 
168     /**
169      * Returns the next entry.
170      *
171      * @return the next entry, or null if there is none.
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      * Returns the previous entry.
194      *
195      * @return the previous entry, or null if there is none.
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      * Returns the first entry.
218      *
219      * @return the first entry, or null if there is none.
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      * Returns the last entry.
235      *
236      * @return the last entry, or null if there is none.
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      * Returns the root entry.
252      *
253      * @return the root entry, or null if there is none.
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     // Object Overrides
275     // -----------------------------------------------------------------------
276 
277     /**
278      * Returns a string representation of the object.
279      */
280     public String toString()
281     {
282         return toString( 0 );
283     }
284 
285     /**
286      * Returns a string representation of all objects to the given depth.
287      *
288      * @param depth The depth to descent to.
289      * @return A string.
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 }