2121 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2222 * portions copyright 2005 Trond Norbye. All rights reserved.
2323 * Use is subject to license terms.
24+ * Portions Copyright (c) 2019, Chris Fraire <cfraire@me.com>.
2425 */
2526package org .opengrok .indexer .search ;
2627
3132 *
3233 * @author Trond Norbye
3334 */
34- public class Hit implements Comparable < Hit > {
35+ public class Hit {
3536 /**
3637 * Holds value of property filename.
3738 */
38- private String filename ;
39+ private final String filename ;
3940
4041 /**
4142 * Holds value of property directory.
4243 */
43- private String directory ;
44+ private final String directory ;
4445
4546 /**
4647 * Holds value of property line.
@@ -65,17 +66,19 @@ public class Hit implements Comparable<Hit> {
6566 /**
6667 * path relative to source root.
6768 */
68- private String path ;
69+ private final String path ;
6970
7071 /**
71- * Creates a new instance of Hit.
72+ * Creates a new, possibly-defined instance.
73+ *
74+ * @param filename The name of the file this hit represents
7275 */
73- public Hit () {
74- this (null , null , null , false , false );
76+ public Hit (String filename ) {
77+ this (filename , null , null , false , false );
7578 }
7679
7780 /**
78- * Creates a new instance of Hit .
81+ * Creates a new, possibly-defined instance .
7982 *
8083 * @param filename The name of the file this hit represents
8184 * @param line The line containing the match
@@ -88,115 +91,55 @@ public Hit(String filename, String line, String lineno, boolean binary, boolean
8891 File file = new File (filename );
8992 this .path = filename ;
9093 this .filename = file .getName ();
91- this . directory = file .getParent ();
92- if (directory == null ) {
94+ final String parent = file .getParent ();
95+ if (parent == null ) {
9396 directory = "" ;
97+ } else {
98+ directory = parent ;
9499 }
100+ } else {
101+ this .path = "" ;
102+ this .filename = "" ;
103+ this .directory = "" ;
95104 }
96105 this .line = line ;
97106 this .lineno = lineno ;
98107 this .binary = binary ;
99108 this .alt = alt ;
100109 }
101110
102- /**
103- * Getter for property filename.
104- *
105- * @return Value of property filename.
106- */
107111 public String getFilename () {
108112 return this .filename ;
109113 }
110114
111- /**
112- * Getter for property path.
113- *
114- * @return Value of property path.
115- */
116115 public String getPath () {
117116 return this .path ;
118117 }
119118
120- /**
121- * Getter for property directory.
122- *
123- * @return Value of property directory
124- */
125119 public String getDirectory () {
126120 return this .directory ;
127121 }
128122
129- /**
130- * Setter for property filename.
131- *
132- * @param filename New value of property filename.
133- */
134- public void setFilename (String filename ) {
135- this .filename = filename ;
136- }
137-
138- /**
139- * Getter for property line.
140- *
141- * @return Value of property line.
142- */
143123 public String getLine () {
144124 return this .line ;
145125 }
146126
147- /**
148- * Setter for property line.
149- *
150- * @param line New value of property line.
151- */
152127 public void setLine (String line ) {
153128 this .line = line ;
154129 }
155130
156- /**
157- * Getter for property line no.
158- *
159- * @return Value of property line no.
160- */
161131 public String getLineno () {
162132 return this .lineno ;
163133 }
164134
165- /**
166- * Setter for property line no.
167- *
168- * @param lineno New value of property line no.
169- */
170135 public void setLineno (String lineno ) {
171136 this .lineno = lineno ;
172137 }
173138
174- /**
175- * Compare this object to another hit (in order to implement the comparable interface).
176- *
177- * @param o The object to compare this object with
178- *
179- * @return the result of a toString().compareTo() of the filename
180- */
181- @ Override
182- public int compareTo (Hit o ) throws ClassCastException {
183- return filename .compareTo (o .filename );
184- }
185-
186- /**
187- * Getter for property binary.
188- *
189- * @return Value of property binary.
190- */
191139 public boolean isBinary () {
192140 return this .binary ;
193141 }
194142
195- /**
196- * Setter for property binary.
197- *
198- * @param binary New value of property binary.
199- */
200143 public void setBinary (boolean binary ) {
201144 this .binary = binary ;
202145 }
@@ -206,19 +149,11 @@ public void setBinary(boolean binary) {
206149 */
207150 private String tag ;
208151
209- /**
210- * Getter for property tag.
211- * @return Value of property tag.
212- */
213152 public String getTag () {
214153
215154 return this .tag ;
216155 }
217156
218- /**
219- * Setter for property tag.
220- * @param tag New value of property tag.
221- */
222157 public void setTag (String tag ) {
223158
224159 this .tag = tag ;
@@ -231,23 +166,4 @@ public void setTag(String tag) {
231166 public boolean getAlt () {
232167 return alt ;
233168 }
234-
235- /**
236- * Check if two objects are equal. Only consider the {@code filename} field
237- * to match the return value of the {@link #compareTo(Hit)} method.
238- * @param o the object to compare with
239- * @return true if the filenames are equal
240- */
241- @ Override
242- public boolean equals (Object o ) {
243- if (o instanceof Hit ) {
244- return compareTo ((Hit ) o ) == 0 ;
245- }
246- return false ;
247- }
248-
249- @ Override
250- public int hashCode () {
251- return filename .hashCode ();
252- }
253169}
0 commit comments