Просмотр исходного кода

Avoid null as a return value for TokenHashMap.get().

Timur Sağlam 5 лет назад
Родитель
Сommit
1b8ebb6504

+ 4 - 3
jplag.frontend-utils/src/main/java/jplag/TokenHashMap.java

@@ -2,6 +2,7 @@ package jplag;
 
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -28,14 +29,14 @@ public class TokenHashMap {
      * Returns all stored numbers for a key. Note that all keys with identical <code>(key % prime)</code> are mapped to the
      * same values (see {@link TokenHashMap}).
      * @param key is the specific key.
-     * @return the stored numbers or null if nothing is stored.
+     * @return the stored numbers or an empty list if nothing is stored.
      */
     public final List<Integer> get(int key) {
         int actualKey = key % primeNumber;
         if (mappedEntries.containsKey(actualKey)) {
             return new ArrayList<>(mappedEntries.get(actualKey));
         }
-        return null;
+        return Collections.emptyList();
     }
 
     /**
@@ -44,7 +45,7 @@ public class TokenHashMap {
      * @param key is the specific key.
      * @param value is the number to store.
      */
-    public final void add(int key, int value) { // TODO TS: Should be renamed to put
+    public final void put(int key, int value) {
         int actualKey = key % primeNumber;
         if (mappedEntries.containsKey(actualKey)) {
             mappedEntries.get(actualKey).add(value);

+ 4 - 4
jplag/src/main/java/jplag/GreedyStringTiling.java

@@ -57,7 +57,7 @@ public class GreedyStringTiling implements TokenConstants {
             for (int i = 0; i < loops; i++) {
                 if (hashedLength >= hashLength) {
                     tokenList.getToken(i).hash = hash;
-                    tokenList.tokenHashes.add(hash, i);   // add into hashtable
+                    tokenList.tokenHashes.put(hash, i);   // add into hashtable
                 } else {
                     tokenList.getToken(i).hash = -1;
                 }
@@ -152,11 +152,11 @@ public class GreedyStringTiling implements TokenConstants {
             maxmatch = minimalTokenMatch;
             matches.clear();
             for (int x = 0; x < first.size() - maxmatch; x++) {
-                List<Integer> elementsOfSecond = second.tokenHashes.get(first.getToken(x).hash);
-                if (first.getToken(x).marked || first.getToken(x).hash == -1 || elementsOfSecond == null) {
+                List<Integer> hashedTokens = second.tokenHashes.get(first.getToken(x).hash);
+                if (first.getToken(x).marked || first.getToken(x).hash == -1) {
                     continue;
                 }
-                inner: for (Integer y : elementsOfSecond) {
+                inner: for (Integer y : hashedTokens) {
                     if (second.getToken(y).marked || maxmatch >= second.size() - y) { // >= because of pivots!
                         continue;
                     }