From 7fbf8f221c31e1dfccaf37defee40c3a23b464fb Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Fri, 21 Aug 2020 15:00:21 +0530 Subject: [PATCH 1/4] Removed code smells and followed naming convention - Local variable names in java must be with the default regular expression ^[a-z][a-zA-Z0-9]*$ https://rules.sonarsource.com/java/tag/convention/RSPEC-117 - The diamond operator ("<>") should be used when the type of the maps are already defined https://rules.sonarsource.com/java/RSPEC-2293 - Package names being camel cased is a code smell --- src/main/java/com/caching/LFUCache.java | 8 ++++---- src/main/java/com/ciphers/CaesarBruteForce.java | 12 ++++++------ .../java/com/conversions/HexadecimalToBinary.java | 6 +++--- .../BinaryTree.java | 2 +- .../DisjointSet.java | 2 +- .../GeneralQueue.java | 2 +- .../{dataStructures => datastructures}/IntQueue.java | 2 +- .../SinglyLinkedList.java | 2 +- .../{dataStructures => datastructures}/Stack.java | 2 +- .../GaleShapley.java | 2 +- .../BinaryTreeTest.java | 2 +- .../DisjointSetTest.java | 2 +- .../GeneralQueueTest.java | 2 +- .../IntQueueTest.java | 2 +- .../SinglyLinkedListTest.java | 2 +- .../StackTest.java | 2 +- .../GaleShapleyTest.java | 2 +- 17 files changed, 27 insertions(+), 27 deletions(-) rename src/main/java/com/{dataStructures => datastructures}/BinaryTree.java (99%) rename src/main/java/com/{dataStructures => datastructures}/DisjointSet.java (99%) rename src/main/java/com/{dataStructures => datastructures}/GeneralQueue.java (99%) rename src/main/java/com/{dataStructures => datastructures}/IntQueue.java (99%) rename src/main/java/com/{dataStructures => datastructures}/SinglyLinkedList.java (98%) rename src/main/java/com/{dataStructures => datastructures}/Stack.java (98%) rename src/main/java/com/matchings/{stableMatching => stablematching}/GaleShapley.java (99%) rename src/test/java/com/{dataStructures => datastructures}/BinaryTreeTest.java (98%) rename src/test/java/com/{dataStructures => datastructures}/DisjointSetTest.java (96%) rename src/test/java/com/{dataStructures => datastructures}/GeneralQueueTest.java (97%) rename src/test/java/com/{dataStructures => datastructures}/IntQueueTest.java (99%) rename src/test/java/com/{dataStructures => datastructures}/SinglyLinkedListTest.java (98%) rename src/test/java/com/{dataStructures => datastructures}/StackTest.java (98%) rename src/test/java/com/matchings/{stableMatching => stablematching}/GaleShapleyTest.java (99%) diff --git a/src/main/java/com/caching/LFUCache.java b/src/main/java/com/caching/LFUCache.java index c61ba6c7..3eb78de8 100644 --- a/src/main/java/com/caching/LFUCache.java +++ b/src/main/java/com/caching/LFUCache.java @@ -75,8 +75,8 @@ class LFUCache { public LFUCache(int capacity) { this.capacity = capacity; size = 0; - freq = new TreeMap(); - map = new HashMap(); + freq = new TreeMap<>(); + map = new HashMap<>(); System.out.println("LFUCache initialised with capacity: " + capacity); } @@ -145,8 +145,8 @@ class LFUCache { dll.deleteNode(dll.tail.pre); if (dll.len == 0 && lowest != 1) freq.remove(lowest); - DLL freq_one = freq.computeIfAbsent(1, k -> new DLL()); - freq_one.addToHead(node); + DLL freqOne = freq.computeIfAbsent(1, k -> new DLL()); + freqOne.addToHead(node); } } diff --git a/src/main/java/com/ciphers/CaesarBruteForce.java b/src/main/java/com/ciphers/CaesarBruteForce.java index ab60fec0..88f71569 100644 --- a/src/main/java/com/ciphers/CaesarBruteForce.java +++ b/src/main/java/com/ciphers/CaesarBruteForce.java @@ -5,25 +5,25 @@ public class CaesarBruteForce { /** * Recursively Brute forces a parsed encrypted text, trying out all shifting keys from 1-26, printing out all decryption attempts * @param message (String) The encrypted text. - * @param Key (int) The key used to decrypt the encrypted text and is increment upon a recursive call. + * @param key (int) The key used to decrypt the encrypted text and is increment upon a recursive call. * @return (String) Concatenated string of all decryption attempts (For unit testing purposes). */ - public String decrypt(String message, int Key) { + public String decrypt(String message, int key) { final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - if (Key > 26){ System.out.println(); return null; } + if (key > 26){ System.out.println(); return null; } StringBuilder plainText = new StringBuilder(); for (char character : message.toUpperCase().toCharArray()) { int index = LETTERS.indexOf(character); if (index != -1) { - index -= Key; + index -= key; //Wrap around index value range[1-26] if (index < 0) { index += LETTERS.length(); } plainText.append(LETTERS.toCharArray()[index]); } else { plainText.append(character); } } - System.out.println(String.format("Current Decryption Key %d : %s", Key, plainText)); - return plainText.append(decrypt(message, Key+1)).toString(); + System.out.println(String.format("Current Decryption Key %d : %s", key, plainText)); + return plainText.append(decrypt(message, key+1)).toString(); } } diff --git a/src/main/java/com/conversions/HexadecimalToBinary.java b/src/main/java/com/conversions/HexadecimalToBinary.java index 6758606e..da3a01ac 100644 --- a/src/main/java/com/conversions/HexadecimalToBinary.java +++ b/src/main/java/com/conversions/HexadecimalToBinary.java @@ -12,7 +12,7 @@ public class HexadecimalToBinary { public String hexToBin (String hexStr) { String binaryString = "", hexaNumbers = "0123456789ABCDEF", - DecimalStr ="", binaryStringBefore ="" , binaryStringAfter = ""; + decimalStr ="", binaryStringBefore ="" , binaryStringAfter = ""; int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0 , decimalNumberAfter = 0; char letter; @@ -48,12 +48,12 @@ public class HexadecimalToBinary { String decimalNumberAfterStr = String.valueOf(decimalNumberAfter); - DecimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr; + decimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr; } - int pointPositionDec = DecimalStr.indexOf("."); + int pointPositionDec = decimalStr.indexOf("."); /** * Check whether the result contains a floating point or not */ diff --git a/src/main/java/com/dataStructures/BinaryTree.java b/src/main/java/com/datastructures/BinaryTree.java similarity index 99% rename from src/main/java/com/dataStructures/BinaryTree.java rename to src/main/java/com/datastructures/BinaryTree.java index 4aae62fe..4270b880 100644 --- a/src/main/java/com/dataStructures/BinaryTree.java +++ b/src/main/java/com/datastructures/BinaryTree.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; /** * Binary tree for general value type, without redundancy diff --git a/src/main/java/com/dataStructures/DisjointSet.java b/src/main/java/com/datastructures/DisjointSet.java similarity index 99% rename from src/main/java/com/dataStructures/DisjointSet.java rename to src/main/java/com/datastructures/DisjointSet.java index 3cb0d698..0e3118f1 100644 --- a/src/main/java/com/dataStructures/DisjointSet.java +++ b/src/main/java/com/datastructures/DisjointSet.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import java.io.Serializable; import java.util.*; diff --git a/src/main/java/com/dataStructures/GeneralQueue.java b/src/main/java/com/datastructures/GeneralQueue.java similarity index 99% rename from src/main/java/com/dataStructures/GeneralQueue.java rename to src/main/java/com/datastructures/GeneralQueue.java index 3d092082..1749bb10 100644 --- a/src/main/java/com/dataStructures/GeneralQueue.java +++ b/src/main/java/com/datastructures/GeneralQueue.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import com.types.Queue; diff --git a/src/main/java/com/dataStructures/IntQueue.java b/src/main/java/com/datastructures/IntQueue.java similarity index 99% rename from src/main/java/com/dataStructures/IntQueue.java rename to src/main/java/com/datastructures/IntQueue.java index d300f383..901609c6 100644 --- a/src/main/java/com/dataStructures/IntQueue.java +++ b/src/main/java/com/datastructures/IntQueue.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; /** * This file contains an implementation of an integer only queue which is extremely quick and diff --git a/src/main/java/com/dataStructures/SinglyLinkedList.java b/src/main/java/com/datastructures/SinglyLinkedList.java similarity index 98% rename from src/main/java/com/dataStructures/SinglyLinkedList.java rename to src/main/java/com/datastructures/SinglyLinkedList.java index 4525e3a7..6d1e5e98 100644 --- a/src/main/java/com/dataStructures/SinglyLinkedList.java +++ b/src/main/java/com/datastructures/SinglyLinkedList.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; diff --git a/src/main/java/com/dataStructures/Stack.java b/src/main/java/com/datastructures/Stack.java similarity index 98% rename from src/main/java/com/dataStructures/Stack.java rename to src/main/java/com/datastructures/Stack.java index 7c562f5b..e71d3cd1 100644 --- a/src/main/java/com/dataStructures/Stack.java +++ b/src/main/java/com/datastructures/Stack.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import java.io.Serializable; import java.util.EmptyStackException; diff --git a/src/main/java/com/matchings/stableMatching/GaleShapley.java b/src/main/java/com/matchings/stablematching/GaleShapley.java similarity index 99% rename from src/main/java/com/matchings/stableMatching/GaleShapley.java rename to src/main/java/com/matchings/stablematching/GaleShapley.java index 1e1e1ab9..f8d94b62 100644 --- a/src/main/java/com/matchings/stableMatching/GaleShapley.java +++ b/src/main/java/com/matchings/stablematching/GaleShapley.java @@ -1,4 +1,4 @@ -package com.matchings.stableMatching; +package com.matchings.stablematching; public class GaleShapley { diff --git a/src/test/java/com/dataStructures/BinaryTreeTest.java b/src/test/java/com/datastructures/BinaryTreeTest.java similarity index 98% rename from src/test/java/com/dataStructures/BinaryTreeTest.java rename to src/test/java/com/datastructures/BinaryTreeTest.java index 1fcfeb7d..a574692e 100644 --- a/src/test/java/com/dataStructures/BinaryTreeTest.java +++ b/src/test/java/com/datastructures/BinaryTreeTest.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/dataStructures/DisjointSetTest.java b/src/test/java/com/datastructures/DisjointSetTest.java similarity index 96% rename from src/test/java/com/dataStructures/DisjointSetTest.java rename to src/test/java/com/datastructures/DisjointSetTest.java index d85871a7..79e65df1 100644 --- a/src/test/java/com/dataStructures/DisjointSetTest.java +++ b/src/test/java/com/datastructures/DisjointSetTest.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/dataStructures/GeneralQueueTest.java b/src/test/java/com/datastructures/GeneralQueueTest.java similarity index 97% rename from src/test/java/com/dataStructures/GeneralQueueTest.java rename to src/test/java/com/datastructures/GeneralQueueTest.java index 3c4c3a7a..d4794d02 100644 --- a/src/test/java/com/dataStructures/GeneralQueueTest.java +++ b/src/test/java/com/datastructures/GeneralQueueTest.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import com.types.Queue; diff --git a/src/test/java/com/dataStructures/IntQueueTest.java b/src/test/java/com/datastructures/IntQueueTest.java similarity index 99% rename from src/test/java/com/dataStructures/IntQueueTest.java rename to src/test/java/com/datastructures/IntQueueTest.java index 4e107c88..f9f87199 100644 --- a/src/test/java/com/dataStructures/IntQueueTest.java +++ b/src/test/java/com/datastructures/IntQueueTest.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import static org.junit.Assert.*; diff --git a/src/test/java/com/dataStructures/SinglyLinkedListTest.java b/src/test/java/com/datastructures/SinglyLinkedListTest.java similarity index 98% rename from src/test/java/com/dataStructures/SinglyLinkedListTest.java rename to src/test/java/com/datastructures/SinglyLinkedListTest.java index a5b87b63..0396bf3d 100644 --- a/src/test/java/com/dataStructures/SinglyLinkedListTest.java +++ b/src/test/java/com/datastructures/SinglyLinkedListTest.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import org.junit.Test; import org.junit.jupiter.api.Assertions; diff --git a/src/test/java/com/dataStructures/StackTest.java b/src/test/java/com/datastructures/StackTest.java similarity index 98% rename from src/test/java/com/dataStructures/StackTest.java rename to src/test/java/com/datastructures/StackTest.java index c6b80f15..4759d17b 100644 --- a/src/test/java/com/dataStructures/StackTest.java +++ b/src/test/java/com/datastructures/StackTest.java @@ -1,4 +1,4 @@ -package com.dataStructures; +package com.datastructures; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/matchings/stableMatching/GaleShapleyTest.java b/src/test/java/com/matchings/stablematching/GaleShapleyTest.java similarity index 99% rename from src/test/java/com/matchings/stableMatching/GaleShapleyTest.java rename to src/test/java/com/matchings/stablematching/GaleShapleyTest.java index d23db538..6506bd07 100644 --- a/src/test/java/com/matchings/stableMatching/GaleShapleyTest.java +++ b/src/test/java/com/matchings/stablematching/GaleShapleyTest.java @@ -1,4 +1,4 @@ -package com.matchings.stableMatching; +package com.matchings.stablematching; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; From 57c1967be0276e4cc96e7a016fd18d903d4a7f9f Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Fri, 21 Aug 2020 15:07:49 +0530 Subject: [PATCH 2/4] Added constant for string literal - code smell String literals should not be duplicated https://rules.sonarsource.com/java/RSPEC-1192 --- src/main/java/com/datastructures/DisjointSet.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/datastructures/DisjointSet.java b/src/main/java/com/datastructures/DisjointSet.java index 0e3118f1..281bbf16 100644 --- a/src/main/java/com/datastructures/DisjointSet.java +++ b/src/main/java/com/datastructures/DisjointSet.java @@ -18,14 +18,15 @@ import java.util.*; */ public class DisjointSet implements Serializable { private static final long serialVersionUID = 3134700471905625636L; + private static final String elementKey = "element"; - private Map> nodeMap = new HashMap<>(); + private final Map> 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 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 leftNode = nodeMap.get(left), rightNode = nodeMap.get(right); From 6aecb37b4954168d6a53c96708b6d44d11ff1f89 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Fri, 21 Aug 2020 15:35:58 +0530 Subject: [PATCH 3/4] Removing unused generic variable - code smell --- src/main/java/com/caching/LRUCache.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/caching/LRUCache.java b/src/main/java/com/caching/LRUCache.java index e043e35d..88e66efb 100644 --- a/src/main/java/com/caching/LRUCache.java +++ b/src/main/java/com/caching/LRUCache.java @@ -63,7 +63,6 @@ public class LRUCache { System.out.println("Cache set to 0 capacity. No elements will be cached"); } - T currentValue = cache.get(key); if (!cache.containsKey(key)) { cache.put(key, value); System.out.println("Adding new key:" + key + " to cache"); From 2d797fb976fb0a69f914b3ca3da0048a34a04d07 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Fri, 21 Aug 2020 15:59:02 +0530 Subject: [PATCH 4/4] Corrected code smells with raw type and dangling else block -Generic types shouldn't be used raw (without type parameters) in variable declarations or return values. Doing so bypasses generic type checking, and defers the catch of unsafe code to runtime. https://rules.sonarsource.com/java/RSPEC-3740 - The dangling else problem appears when nested if/else statements are written without curly braces. In this case, else is associated with the nearest if but that is not always obvious and sometimes the indentation can also be misleading. https://rules.sonarsource.com/java/tag/confusing/RSPEC-5261 --- .../java/com/datastructures/BinaryTree.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/datastructures/BinaryTree.java b/src/main/java/com/datastructures/BinaryTree.java index 4270b880..5bf1702e 100644 --- a/src/main/java/com/datastructures/BinaryTree.java +++ b/src/main/java/com/datastructures/BinaryTree.java @@ -8,7 +8,7 @@ package com.datastructures; public class BinaryTree { private final T data; - private BinaryTree right, // the upper binary tree + private BinaryTree right, // the upper binary tree left; // the lower binary tree public BinaryTree(T data) { @@ -21,35 +21,38 @@ public class BinaryTree { } /** - * inserts a new value in it's correspondant place + * inserts a new value in it's correspondent place * * @param newDataValue value of the new binary tree to add on this tree */ public void insert(T newDataValue) { - this.insert(new BinaryTree(newDataValue)); + this.insert(new BinaryTree<>(newDataValue)); } /** - * inserts a new binary tree in it's correspondant place + * inserts a new binary tree in it's correspondent place * * @param newData new value to add on this tree */ - public void insert(BinaryTree newData) { + public void insert(BinaryTree newData) { int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value - if (cpr < 0) - if (this.left == null) + if (cpr < 0) { + if (this.left == null) { this.setLeft(newData); - else + } else { this.left.insert(newData); - else if (cpr > 0) - if (this.right == null) + } + } else if (cpr > 0) { + if (this.right == null) { this.setRight(newData); - else + } else { this.right.insert(newData); - else + } + } else { System.out.println("Redundant value, not added"); + } } /** @@ -58,8 +61,8 @@ public class BinaryTree { * @param data Searched value * @return Binary tree which contains the value, null if it doesn't exist */ - public BinaryTree search(T data) { - int cpr = data.compareTo(this.data); //new value comparission respect to actual value + public BinaryTree search(T data) { + int cpr = data.compareTo(this.data); //new value comparison respect to actual value if (cpr < 0) { if (this.left == null) @@ -113,19 +116,19 @@ public class BinaryTree { return data; } - public BinaryTree getRight() { + public BinaryTree getRight() { return right; } - public void setRight(BinaryTree right) { + public void setRight(BinaryTree right) { this.right = right; } - public BinaryTree getLeft() { + public BinaryTree getLeft() { return left; } - public void setLeft(BinaryTree left) { + public void setLeft(BinaryTree left) { this.left = left; } }