xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/search/Hit.java (revision c6f0939b1c668e9f8e1e276424439c3106b3a029)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package org.opengrok.indexer.search;
24 
25 import java.io.File;
26 
27 /**
28  * The hit class represents a single search hit.
29  *
30  * @author Trond Norbye
31  */
32 public class Hit implements Comparable<Hit> {
33     /**
34      * Holds value of property filename.
35      */
36     private String filename;
37 
38     /**
39      * Holds value of property directory.
40      */
41     private String directory;
42 
43     /**
44      * Holds value of property line.
45      */
46     private String line;
47 
48     /**
49      * Holds value of property line no.
50      */
51     private String lineno;
52 
53     /**
54      * Holds value of property binary.
55      */
56     private boolean binary;
57 
58     /**
59      * Holds value of property alt used to highlight alternating files.
60      */
61     private final boolean alt;
62 
63     /**
64      * path relative to source root.
65      */
66     private String path;
67 
68     /**
69      * Creates a new instance of Hit.
70      */
Hit()71     public Hit() {
72         this(null, null, null, false, false);
73     }
74 
75     /**
76      * Creates a new instance of Hit.
77      *
78      * @param filename The name of the file this hit represents
79      * @param line The line containing the match
80      * @param lineno The line number in the file the match was found
81      * @param binary If this is a binary file or not
82      * @param alt Is this the "alternate" file
83      */
Hit(String filename, String line, String lineno, boolean binary, boolean alt)84     public Hit(String filename, String line, String lineno, boolean binary, boolean alt) {
85         if (filename != null) {
86             File file = new File(filename);
87             this.path = filename;
88             this.filename = file.getName();
89             this.directory = file.getParent();
90             if (directory == null) {
91                 directory = "";
92             }
93         }
94         this.line = line;
95         this.lineno = lineno;
96         this.binary = binary;
97         this.alt = alt;
98     }
99 
100     /**
101      * Getter for property filename.
102      *
103      * @return Value of property filename.
104      */
getFilename()105     public String getFilename() {
106         return this.filename;
107     }
108 
109     /**
110      * Getter for property path.
111      *
112      * @return Value of property path.
113      */
getPath()114     public String getPath() {
115         return this.path;
116     }
117 
118     /**
119      * Getter for property directory.
120      *
121      * @return Value of property directory
122      */
getDirectory()123     public String getDirectory() {
124         return this.directory;
125     }
126 
127     /**
128      * Setter for property filename.
129      *
130      * @param filename New value of property filename.
131      */
setFilename(String filename)132     public void setFilename(String filename) {
133         this.filename = filename;
134     }
135 
136     /**
137      * Getter for property line.
138      *
139      * @return Value of property line.
140      */
getLine()141     public String getLine() {
142         return this.line;
143     }
144 
145     /**
146      * Setter for property line.
147      *
148      * @param line New value of property line.
149      */
setLine(String line)150     public void setLine(String line) {
151         this.line = line;
152     }
153 
154     /**
155      * Getter for property line no.
156      *
157      * @return Value of property line no.
158      */
getLineno()159     public String getLineno() {
160         return this.lineno;
161     }
162 
163     /**
164      * Setter for property line no.
165      *
166      * @param lineno New value of property line no.
167      */
setLineno(String lineno)168     public void setLineno(String lineno) {
169         this.lineno = lineno;
170     }
171 
172     /**
173      * Compare this object to another hit (in order to implement the comparable interface).
174      *
175      * @param o The object to compare this object with
176      *
177      * @return the result of a toString().compareTo() of the filename
178      */
179     @Override
compareTo(Hit o)180     public int compareTo(Hit o) throws ClassCastException {
181         return filename.compareTo(o.filename);
182     }
183 
184     /**
185      * Getter for property binary.
186      *
187      * @return Value of property binary.
188      */
isBinary()189     public boolean isBinary() {
190         return this.binary;
191     }
192 
193     /**
194      * Setter for property binary.
195      *
196      * @param binary New value of property binary.
197      */
setBinary(boolean binary)198     public void setBinary(boolean binary) {
199         this.binary = binary;
200     }
201 
202     /**
203      * Holds value of property tag.
204      */
205     private String tag;
206 
207     /**
208      * Getter for property tag.
209      * @return Value of property tag.
210      */
getTag()211     public String getTag() {
212 
213         return this.tag;
214     }
215 
216     /**
217      * Setter for property tag.
218      * @param tag New value of property tag.
219      */
setTag(String tag)220     public void setTag(String tag) {
221 
222         this.tag = tag;
223     }
224 
225     /**
226      * Should this be alternate file?
227      * @return true if this is the "alternate" file
228      */
getAlt()229     public boolean getAlt() {
230         return alt;
231     }
232 
233     /**
234      * Check if two objects are equal. Only consider the {@code filename} field
235      * to match the return value of the {@link #compareTo(Hit)} method.
236      * @param o the object to compare with
237      * @return true if the filenames are equal
238      */
239     @Override
equals(Object o)240     public boolean equals(Object o) {
241         if (o instanceof Hit) {
242             return compareTo((Hit) o) == 0;
243         }
244         return false;
245     }
246 
247     @Override
hashCode()248     public int hashCode() {
249         return filename.hashCode();
250     }
251 }
252