View Javadoc
1   package org.codehaus.mojo.taglist.beans;
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.io.File;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  /**
30   * Report of the scan for a specific tag.
31   * 
32   * @author <a href="mailto:bellingard.NO-SPAM@gmail.com">Fabrice Bellingard </a>
33   */
34  public class TagReport
35      implements Comparable
36  {
37  
38      /**
39       * Tag Class display name.
40       */
41      private String displayName;
42      
43      /**
44       * Tag Class HTML safe link name.
45       */
46      private String linkName;
47      
48      /**
49       * An array containing the tag string that make the tag class.
50       */
51      private ArrayList tagStrings = new ArrayList();
52  
53      /**
54       * Map containing File objects as keys, and FileReport object as values.
55       */
56      private Map fileReportsMap;
57  
58      /**
59       * Number of tags found in the code.
60       */
61      private int tagCount;
62  
63      /**
64       * Constructor.
65       * 
66       * @param displayName the tag class's name.
67       * @param linkName a HTML safe link name for this report.
68       */
69      public TagReport( final String displayName, final String linkName )
70      {
71          this.displayName = displayName;
72          this.fileReportsMap = new HashMap();
73          this.linkName = linkName;
74          tagCount = -1;
75      }
76  
77      /**
78       * Returns the FileReport object corresponding to this file. If it does not exist yet, it will be created.
79       * 
80       * @param file the file being analyzed.
81       * @param encoding the character encoding of the file
82       * @return a FileReport object for this file.
83       */
84      public FileReport getFileReport( File file, String encoding )
85      {
86          Object report = fileReportsMap.get( file );
87          if ( report instanceof FileReport )
88          {
89              return (FileReport) report;
90          }
91          else
92          {
93              FileReport newFileReport = new FileReport( file, encoding );
94              fileReportsMap.put( file, newFileReport );
95              return newFileReport;
96          }
97      }
98  
99      /**
100      * Returns the collection of file reports for the tag.
101      * 
102      * @return a Collection of FileReport objects.
103      */
104     public Collection getFileReports()
105     {
106         return fileReportsMap.values();
107     }
108 
109     /**
110      * Returns the name of the tag class that was looked for.
111      * 
112      * @return the name of the tag class.
113      */
114     public String getTagName()
115     {
116         return displayName;
117     }
118     
119     /**
120      * Returns a HTML safe link name for this tag report.
121      * 
122      * @return a HTML safe link name.
123      */
124     public String getHTMLSafeLinkName()
125     {
126         return linkName;
127     }
128 
129     /**
130      * Gives the number of comments found for that tag.
131      * 
132      * @return the number of comments.
133      */
134     public int getTagCount()
135     {
136         if ( tagCount > -1 )
137         {
138             return tagCount;
139         }
140         // tagCount was not computed yet
141         tagCount = 0;
142         for ( Iterator iter = fileReportsMap.values().iterator(); iter.hasNext(); )
143         {
144             FileReport fileReport = (FileReport) iter.next();
145             tagCount += fileReport.getLineIndexes().size();
146         }
147         return tagCount;
148     }
149 
150     /**
151      * {@inheritDoc}
152      * 
153      * @see Comparable#compareTo(Object)
154      */
155     public int compareTo( Object o )
156     {
157         if ( o instanceof TagReport )
158         {
159             TagReport tagReport = (TagReport) o;
160             return this.getTagName().compareTo( tagReport.getTagName() );
161         }
162         else
163         {
164             return 0;
165         }
166     }
167     
168     /** Add a tag string to this tag class. 
169      *  Each tag class contains 1 or more tag strings that are used
170      *  for matching 'todo' strings in the scanned code. 
171      *  
172      * @param tagString the tag string to add.
173      */
174     public void addTagString ( final String tagString )
175     {
176         if ( tagString != null )
177         {
178             tagStrings.add( tagString );
179         }
180     }
181     
182     /** Get a list of tag strings used by this tag report.
183      * 
184      * @return a list of tag strings.
185      */
186     public String [] getTagStrings ()
187     {
188         
189         String [] strings = null;
190         
191         if ( tagStrings.size() > 0 )
192         {
193             strings = new String [tagStrings.size()];
194 
195             for ( int i = 0; i < tagStrings.size(); ++i )
196             {
197                 strings[i] = (String) tagStrings.get( i );
198             }
199         }
200         
201         return ( strings );
202         
203     }
204     
205     /**
206      * {@inheritDoc}
207      *
208      * @see Object#equals(Object)
209      */
210     public boolean equals( Object r )
211     {
212         // In Java 5 the PriorityQueue.remove method uses the 
213         // compareTo method, while in Java 6 it uses the equals method.
214         return ( this.compareTo( r ) == 0 );
215     }
216     
217     /**
218      * {@inheritDoc}
219      *
220      * @see Object#hashCode()
221      */
222     public int hashCode() 
223     {
224         assert false : "hashCode not designed";
225         return 1; // any arbitrary constant will do 
226     }
227 
228 }