Skip to content

Commit 6135d39

Browse files
committed
For #28, throw NPEs if arguments are null. Remove prior NullEmptyUtil class.
1 parent b517f00 commit 6135d39

File tree

17 files changed

+164
-139
lines changed

17 files changed

+164
-139
lines changed

src/main/java/info/debatty/java/stringsimilarity/Cosine.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance;
2828
import java.util.Map;
2929

30-
import info.debatty.java.utils.NullEmptyUtil;
3130
import net.jcip.annotations.Immutable;
3231

3332
/**
@@ -71,10 +70,16 @@ public Cosine() {
7170
* @return
7271
*/
7372
public final double similarity(final String s1, final String s2) {
74-
Double nullEmptySimilarity = NullEmptyUtil.normalizedSimilarity(s1, s2);
73+
if (s1 == null) {
74+
throw new NullPointerException("s1 must not be null");
75+
}
76+
77+
if (s2 == null) {
78+
throw new NullPointerException("s2 must not be null");
79+
}
7580

76-
if (nullEmptySimilarity != null) {
77-
return nullEmptySimilarity;
81+
if (s1.equals(s2)) {
82+
return 1;
7883
}
7984

8085
if (s1.length() < getK() || s2.length() < getK()) {

src/main/java/info/debatty/java/stringsimilarity/Damerau.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import info.debatty.java.stringsimilarity.interfaces.MetricStringDistance;
2727
import java.util.HashMap;
2828

29-
import info.debatty.java.utils.NullEmptyUtil;
3029
import net.jcip.annotations.Immutable;
3130

3231
/**
@@ -57,10 +56,16 @@ public class Damerau implements MetricStringDistance {
5756
*/
5857
public final double distance(final String s1, final String s2) {
5958

60-
Double nullEmptyDistance = NullEmptyUtil.lengthDistance(s1, s2);
59+
if (s1 == null) {
60+
throw new NullPointerException("s1 must not be null");
61+
}
62+
63+
if (s2 == null) {
64+
throw new NullPointerException("s2 must not be null");
65+
}
6166

62-
if (nullEmptyDistance != null) {
63-
return nullEmptyDistance;
67+
if (s1.equals(s2)) {
68+
return 0;
6469
}
6570

6671
// INFinite distance is the max possible distance

src/main/java/info/debatty/java/stringsimilarity/Jaccard.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.Map;
3232
import java.util.Set;
3333

34-
import info.debatty.java.utils.NullEmptyUtil;
3534
import net.jcip.annotations.Immutable;
3635

3736
/**
@@ -76,10 +75,16 @@ public Jaccard() {
7675
* @return
7776
*/
7877
public final double similarity(final String s1, final String s2) {
79-
Double nullEmptySimilarity = NullEmptyUtil.normalizedSimilarity(s1, s2);
78+
if (s1 == null) {
79+
throw new NullPointerException("s1 must not be null");
80+
}
81+
82+
if (s2 == null) {
83+
throw new NullPointerException("s2 must not be null");
84+
}
8085

81-
if (nullEmptySimilarity != null) {
82-
return nullEmptySimilarity;
86+
if (s1.equals(s2)) {
87+
return 1;
8388
}
8489

8590
Map<String, Integer> profile1 = getProfile(s1);

src/main/java/info/debatty/java/stringsimilarity/JaroWinkler.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance;
55
import java.util.Arrays;
66

7-
import info.debatty.java.utils.NullEmptyUtil;
87
import net.jcip.annotations.Immutable;
98

109
/**
@@ -62,10 +61,16 @@ public final double getThreshold() {
6261
* @return
6362
*/
6463
public final double similarity(final String s1, final String s2) {
65-
Double nullEmptySimilarity = NullEmptyUtil.normalizedSimilarity(s1, s2);
64+
if (s1 == null) {
65+
throw new NullPointerException("s1 must not be null");
66+
}
67+
68+
if (s2 == null) {
69+
throw new NullPointerException("s2 must not be null");
70+
}
6671

67-
if (nullEmptySimilarity != null) {
68-
return nullEmptySimilarity;
72+
if (s1.equals(s2)) {
73+
return 1;
6974
}
7075

7176
int[] mtp = matches(s1, s2);

src/main/java/info/debatty/java/stringsimilarity/Levenshtein.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package info.debatty.java.stringsimilarity;
22

33
import info.debatty.java.stringsimilarity.interfaces.MetricStringDistance;
4-
import info.debatty.java.utils.NullEmptyUtil;
54
import net.jcip.annotations.Immutable;
65

76
/**
@@ -39,10 +38,12 @@ public class Levenshtein implements MetricStringDistance {
3938
* @return
4039
*/
4140
public final double distance(final String s1, final String s2) {
42-
Double nullEmptyDistance = NullEmptyUtil.lengthDistance(s1, s2);
41+
if (s1 == null) {
42+
throw new NullPointerException("s1 must not be null");
43+
}
4344

44-
if (nullEmptyDistance != null) {
45-
return nullEmptyDistance;
45+
if (s2 == null) {
46+
throw new NullPointerException("s2 must not be null");
4647
}
4748

4849
if (s1.equals(s2)) {

src/main/java/info/debatty/java/stringsimilarity/LongestCommonSubsequence.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package info.debatty.java.stringsimilarity;
22

33
import info.debatty.java.stringsimilarity.interfaces.StringDistance;
4-
import info.debatty.java.utils.NullEmptyUtil;
54
import net.jcip.annotations.Immutable;
65

76
/**
@@ -37,10 +36,16 @@ public class LongestCommonSubsequence implements StringDistance {
3736
* |s2| - 2 * |LCS(s1, s2)|
3837
*/
3938
public final double distance(final String s1, final String s2) {
40-
Double nullEmptyDistance = NullEmptyUtil.lengthDistance(s1, s2);
39+
if (s1 == null) {
40+
throw new NullPointerException("s1 must not be null");
41+
}
42+
43+
if (s2 == null) {
44+
throw new NullPointerException("s2 must not be null");
45+
}
4146

42-
if (nullEmptyDistance != null) {
43-
return nullEmptyDistance;
47+
if (s1.equals(s2)) {
48+
return 0;
4449
}
4550

4651
return s1.length() + s2.length() - 2 * length(s1, s2);

src/main/java/info/debatty/java/stringsimilarity/MetricLCS.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import info.debatty.java.stringsimilarity.interfaces.MetricStringDistance;
2828
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance;
29-
import info.debatty.java.utils.NullEmptyUtil;
3029
import net.jcip.annotations.Immutable;
3130

3231
/**
@@ -50,10 +49,16 @@ public class MetricLCS
5049
* @return
5150
*/
5251
public final double distance(final String s1, final String s2) {
53-
Double nullEmptyDistance = NullEmptyUtil.normalizedDistance(s1, s2);
52+
if (s1 == null) {
53+
throw new NullPointerException("s1 must not be null");
54+
}
55+
56+
if (s2 == null) {
57+
throw new NullPointerException("s2 must not be null");
58+
}
5459

55-
if (nullEmptyDistance != null) {
56-
return nullEmptyDistance;
60+
if (s1.equals(s2)) {
61+
return 0;
5762
}
5863

5964
int mLen = Math.max(s1.length(), s2.length());

src/main/java/info/debatty/java/stringsimilarity/NGram.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package info.debatty.java.stringsimilarity;
22

33
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance;
4-
import info.debatty.java.utils.NullEmptyUtil;
54
import net.jcip.annotations.Immutable;
65

76
/**
@@ -43,18 +42,24 @@ public NGram() {
4342
* @return
4443
*/
4544
public final double distance(final String s0, final String s1) {
46-
Double nullEmptyDistance = NullEmptyUtil.normalizedDistance(s0, s1);
45+
if (s0 == null) {
46+
throw new NullPointerException("s0 must not be null");
47+
}
4748

48-
if (nullEmptyDistance != null) {
49-
return nullEmptyDistance;
49+
if (s1 == null) {
50+
throw new NullPointerException("s1 must not be null");
51+
}
52+
53+
if (s0.equals(s1)) {
54+
return 0;
5055
}
5156

5257
final char special = '\n';
5358
final int sl = s0.length();
5459
final int tl = s1.length();
5560

56-
if (s0.equals(s1)) {
57-
return 0;
61+
if (sl == 0 || tl == 0) {
62+
return 1;
5863
}
5964

6065
int cost = 0;

src/main/java/info/debatty/java/stringsimilarity/NormalizedLevenshtein.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringSimilarity;
2727
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance;
28-
import info.debatty.java.utils.NullEmptyUtil;
2928
import net.jcip.annotations.Immutable;
3029

3130
/**
@@ -50,10 +49,16 @@ public class NormalizedLevenshtein implements
5049
*/
5150
public final double distance(final String s1, final String s2) {
5251

53-
Double nullEmptyDistance = NullEmptyUtil.normalizedDistance(s1, s2);
52+
if (s1 == null) {
53+
throw new NullPointerException("s1 must not be null");
54+
}
55+
56+
if (s2 == null) {
57+
throw new NullPointerException("s2 must not be null");
58+
}
5459

55-
if (nullEmptyDistance != null) {
56-
return nullEmptyDistance;
60+
if (s1.equals(s2)) {
61+
return 0;
5762
}
5863

5964
int mLen = Math.max(s1.length(), s2.length());

src/main/java/info/debatty/java/stringsimilarity/OptimalStringAlignment.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package info.debatty.java.stringsimilarity;
2525

2626
import info.debatty.java.stringsimilarity.interfaces.StringDistance;
27-
import info.debatty.java.utils.NullEmptyUtil;
2827
import net.jcip.annotations.Immutable;
2928

3029
/**
@@ -52,10 +51,16 @@ public final class OptimalStringAlignment implements StringDistance {
5251
* @return the OSA distance
5352
*/
5453
public final double distance(final String s1, final String s2) {
55-
Double nullEmptyDistance = NullEmptyUtil.lengthDistance(s1, s2);
54+
if (s1 == null) {
55+
throw new NullPointerException("s1 must not be null");
56+
}
57+
58+
if (s2 == null) {
59+
throw new NullPointerException("s2 must not be null");
60+
}
5661

57-
if (nullEmptyDistance != null) {
58-
return nullEmptyDistance;
62+
if (s1.equals(s2)) {
63+
return 0;
5964
}
6065

6166
int n = s1.length(), m = s2.length();

0 commit comments

Comments
 (0)