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