Skip to content

Commit 52f818f

Browse files
committed
Hit must take a filename
- path, filename, and directory are now immutable, so require initialization
1 parent d87876c commit 52f818f

File tree

2 files changed

+31
-141
lines changed
  • opengrok-indexer/src

2 files changed

+31
-141
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/search/Hit.java

Lines changed: 19 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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 <[email protected]>.
2425
*/
2526
package org.opengrok.indexer.search;
2627

@@ -31,16 +32,16 @@
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
}

opengrok-indexer/src/test/java/org/opengrok/indexer/search/HitTest.java

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2018 Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.search;
2425

25-
import org.junit.Test;
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertNull;
29+
import static org.junit.Assert.assertTrue;
2630

31+
import org.junit.Test;
2732
import java.io.File;
2833

29-
import static org.junit.Assert.*;
30-
3134
/**
3235
* Do basic sanity testing of the Hit class
3336
*
@@ -37,10 +40,8 @@ public class HitTest {
3740

3841
@Test
3942
public void testFilename() {
40-
Hit instance = new Hit();
41-
assertNull(instance.getFilename());
43+
Hit instance = new Hit("a/b/foobar");
4244
String expResult = "foobar";
43-
instance.setFilename(expResult);
4445
assertEquals(expResult, instance.getFilename());
4546
}
4647

@@ -53,7 +54,7 @@ public void testPath() {
5354

5455
@Test
5556
public void testLine() {
56-
Hit instance = new Hit();
57+
Hit instance = new Hit("a/b/c");
5758
assertNull(instance.getLine());
5859
String expResult = "This is a line of text";
5960
instance.setLine(expResult);
@@ -62,33 +63,24 @@ public void testLine() {
6263

6364
@Test
6465
public void testLineno() {
65-
Hit instance = new Hit();
66+
Hit instance = new Hit("a/b");
6667
assertNull(instance.getLineno());
6768
String expResult = "12";
6869
instance.setLineno(expResult);
6970
assertEquals(expResult, instance.getLineno());
7071
}
7172

72-
@Test
73-
public void testCompareTo() {
74-
Hit o1 = new Hit("/foo", null, null, false, false);
75-
Hit o2 = new Hit("/foo", "hi", "there", false, false);
76-
assertEquals(o2.compareTo(o1), o1.compareTo(o2));
77-
o1.setFilename("bar");
78-
assertFalse(o2.compareTo(o1) == o1.compareTo(o2));
79-
}
80-
8173
@Test
8274
public void testBinary() {
83-
Hit instance = new Hit();
75+
Hit instance = new Hit("abc");
8476
assertFalse(instance.isBinary());
8577
instance.setBinary(true);
8678
assertTrue(instance.isBinary());
8779
}
8880

8981
@Test
9082
public void testTag() {
91-
Hit instance = new Hit();
83+
Hit instance = new Hit("def");
9284
assertNull(instance.getTag());
9385
String expResult = "foobar";
9486
instance.setTag(expResult);
@@ -98,27 +90,9 @@ public void testTag() {
9890

9991
@Test
10092
public void testAlt() {
101-
Hit instance = new Hit();
93+
Hit instance = new Hit("ghi/d");
10294
assertFalse(instance.getAlt());
10395
Hit o2 = new Hit(null, null, null, false, true);
10496
assertTrue(o2.getAlt());
10597
}
106-
107-
@Test
108-
public void testEquals() {
109-
Hit o1 = new Hit("/foo", null, null, false, false);
110-
Hit o2 = new Hit("/foo", "hi", "there", false, false);
111-
assertEquals(o2.equals(o1), o1.equals(o2));
112-
o1.setFilename("bar");
113-
assertFalse(o2.equals(o1));
114-
assertFalse(o1.equals(o2));
115-
assertFalse(o1.equals(new Object()));
116-
}
117-
118-
@Test
119-
public void testHashCode() {
120-
String filename = "bar";
121-
Hit instance = new Hit(filename, null, null, false, false);
122-
assertEquals(filename.hashCode(), instance.hashCode());
123-
}
12498
}

0 commit comments

Comments
 (0)