Added constant for string literal - code smell

String literals should not be duplicated
https://rules.sonarsource.com/java/RSPEC-1192
This commit is contained in:
Abhijay Kumar 2020-08-21 15:07:49 +05:30
parent 7fbf8f221c
commit 57c1967be0

View File

@ -18,14 +18,15 @@ import java.util.*;
*/
public class DisjointSet<T> implements Serializable {
private static final long serialVersionUID = 3134700471905625636L;
private static final String elementKey = "element";
private Map<T, Node<T>> nodeMap = new HashMap<>();
private final Map<T, Node<T>> nodeMap = new HashMap<>();
/**
* Add an element to the disjoint-set forests as a set.
*/
public void makeSet(T element) {
checkNotNull(element, "element");
checkNotNull(element, elementKey);
nodeMap.putIfAbsent(element, new Node<>());
}
@ -36,8 +37,8 @@ public class DisjointSet<T> implements Serializable {
* Rank is an upper bound on the height of node.
*/
public void union(T left, T right) {
checkNotNull(left, "element");
checkNotNull(right, "element");
checkNotNull(left, elementKey);
checkNotNull(right, elementKey);
Node<T> leftNode = nodeMap.get(left),
rightNode = nodeMap.get(right);