Skip to content

Commit 452fa56

Browse files
Merge pull request TheAlgorithms#1434 from TheAlgorithms/revert-1432-revert-1429-Development
Revert "Revert "Fixed code smells after running sonarqube on the project""
2 parents dcdd17d + 87485b0 commit 452fa56

18 files changed

+53
-50
lines changed

src/main/java/com/caching/LFUCache.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public void deleteNode(Node node) {
7575
public LFUCache(int capacity) {
7676
this.capacity = capacity;
7777
size = 0;
78-
freq = new TreeMap<Integer, DLL>();
79-
map = new HashMap<Integer, Node>();
78+
freq = new TreeMap<>();
79+
map = new HashMap<>();
8080
System.out.println("LFUCache initialised with capacity: " + capacity);
8181
}
8282

@@ -145,8 +145,8 @@ public void put(int key, T value) {
145145
dll.deleteNode(dll.tail.pre);
146146
if (dll.len == 0 && lowest != 1)
147147
freq.remove(lowest);
148-
DLL freq_one = freq.computeIfAbsent(1, k -> new DLL());
149-
freq_one.addToHead(node);
148+
DLL freqOne = freq.computeIfAbsent(1, k -> new DLL());
149+
freqOne.addToHead(node);
150150
}
151151
}
152152

src/main/java/com/caching/LRUCache.java

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public void put(int key, T value) {
6363
System.out.println("Cache set to 0 capacity. No elements will be cached");
6464
}
6565

66-
T currentValue = cache.get(key);
6766
if (!cache.containsKey(key)) {
6867
cache.put(key, value);
6968
System.out.println("Adding new key:" + key + " to cache");

src/main/java/com/ciphers/CaesarBruteForce.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ public class CaesarBruteForce {
55
/**
66
* Recursively Brute forces a parsed encrypted text, trying out all shifting keys from 1-26, printing out all decryption attempts
77
* @param message (String) The encrypted text.
8-
* @param Key (int) The key used to decrypt the encrypted text and is increment upon a recursive call.
8+
* @param key (int) The key used to decrypt the encrypted text and is increment upon a recursive call.
99
* @return (String) Concatenated string of all decryption attempts (For unit testing purposes).
1010
*/
11-
public String decrypt(String message, int Key) {
11+
public String decrypt(String message, int key) {
1212
final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
13-
if (Key > 26){ System.out.println(); return null; }
13+
if (key > 26){ System.out.println(); return null; }
1414

1515
StringBuilder plainText = new StringBuilder();
1616
for (char character : message.toUpperCase().toCharArray()) {
1717
int index = LETTERS.indexOf(character);
1818

1919
if (index != -1) {
20-
index -= Key;
20+
index -= key;
2121
//Wrap around index value range[1-26]
2222
if (index < 0) { index += LETTERS.length(); }
2323
plainText.append(LETTERS.toCharArray()[index]);
2424
} else { plainText.append(character); }
2525
}
26-
System.out.println(String.format("Current Decryption Key %d : %s", Key, plainText));
27-
return plainText.append(decrypt(message, Key+1)).toString();
26+
System.out.println(String.format("Current Decryption Key %d : %s", key, plainText));
27+
return plainText.append(decrypt(message, key+1)).toString();
2828
}
2929
}

src/main/java/com/conversions/HexadecimalToBinary.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class HexadecimalToBinary {
1212
public String hexToBin (String hexStr) {
1313

1414
String binaryString = "", hexaNumbers = "0123456789ABCDEF",
15-
DecimalStr ="", binaryStringBefore ="" , binaryStringAfter = "";
15+
decimalStr ="", binaryStringBefore ="" , binaryStringAfter = "";
1616
int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0
1717
, decimalNumberAfter = 0;
1818
char letter;
@@ -48,12 +48,12 @@ public String hexToBin (String hexStr) {
4848

4949
String decimalNumberAfterStr = String.valueOf(decimalNumberAfter);
5050

51-
DecimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr;
51+
decimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr;
5252
}
5353

5454

5555

56-
int pointPositionDec = DecimalStr.indexOf(".");
56+
int pointPositionDec = decimalStr.indexOf(".");
5757
/**
5858
* Check whether the result contains a floating point or not
5959
*/

src/main/java/com/dataStructures/BinaryTree.java renamed to src/main/java/com/datastructures/BinaryTree.java

+22-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
/**
44
* Binary tree for general value type, without redundancy
@@ -8,7 +8,7 @@
88

99
public class BinaryTree<T extends Comparable> {
1010
private final T data;
11-
private BinaryTree right, // the upper binary tree
11+
private BinaryTree<T> right, // the upper binary tree
1212
left; // the lower binary tree
1313

1414
public BinaryTree(T data) {
@@ -21,35 +21,38 @@ public String toString() {
2121
}
2222

2323
/**
24-
* inserts a new value in it's correspondant place
24+
* inserts a new value in it's correspondent place
2525
*
2626
* @param newDataValue value of the new binary tree to add on this tree
2727
*/
2828
public void insert(T newDataValue) {
29-
this.insert(new BinaryTree(newDataValue));
29+
this.insert(new BinaryTree<>(newDataValue));
3030
}
3131

3232
/**
33-
* inserts a new binary tree in it's correspondant place
33+
* inserts a new binary tree in it's correspondent place
3434
*
3535
* @param newData new value to add on this tree
3636
*/
37-
public void insert(BinaryTree newData) {
37+
public void insert(BinaryTree<T> newData) {
3838

3939
int cpr = newData.data.compareTo(this.data); //new value comparission respect to actual value
4040

41-
if (cpr < 0)
42-
if (this.left == null)
41+
if (cpr < 0) {
42+
if (this.left == null) {
4343
this.setLeft(newData);
44-
else
44+
} else {
4545
this.left.insert(newData);
46-
else if (cpr > 0)
47-
if (this.right == null)
46+
}
47+
} else if (cpr > 0) {
48+
if (this.right == null) {
4849
this.setRight(newData);
49-
else
50+
} else {
5051
this.right.insert(newData);
51-
else
52+
}
53+
} else {
5254
System.out.println("Redundant value, not added");
55+
}
5356
}
5457

5558
/**
@@ -58,8 +61,8 @@ else if (cpr > 0)
5861
* @param data Searched value
5962
* @return Binary tree which contains the value, null if it doesn't exist
6063
*/
61-
public BinaryTree search(T data) {
62-
int cpr = data.compareTo(this.data); //new value comparission respect to actual value
64+
public BinaryTree<T> search(T data) {
65+
int cpr = data.compareTo(this.data); //new value comparison respect to actual value
6366

6467
if (cpr < 0) {
6568
if (this.left == null)
@@ -113,19 +116,19 @@ public T getData() {
113116
return data;
114117
}
115118

116-
public BinaryTree getRight() {
119+
public BinaryTree<T> getRight() {
117120
return right;
118121
}
119122

120-
public void setRight(BinaryTree right) {
123+
public void setRight(BinaryTree<T> right) {
121124
this.right = right;
122125
}
123126

124-
public BinaryTree getLeft() {
127+
public BinaryTree<T> getLeft() {
125128
return left;
126129
}
127130

128-
public void setLeft(BinaryTree left) {
131+
public void setLeft(BinaryTree<T> left) {
129132
this.left = left;
130133
}
131134
}

src/main/java/com/dataStructures/DisjointSet.java renamed to src/main/java/com/datastructures/DisjointSet.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import java.io.Serializable;
44
import java.util.*;
@@ -18,14 +18,15 @@
1818
*/
1919
public class DisjointSet<T> implements Serializable {
2020
private static final long serialVersionUID = 3134700471905625636L;
21+
private static final String elementKey = "element";
2122

22-
private Map<T, Node<T>> nodeMap = new HashMap<>();
23+
private final Map<T, Node<T>> nodeMap = new HashMap<>();
2324

2425
/**
2526
* Add an element to the disjoint-set forests as a set.
2627
*/
2728
public void makeSet(T element) {
28-
checkNotNull(element, "element");
29+
checkNotNull(element, elementKey);
2930
nodeMap.putIfAbsent(element, new Node<>());
3031
}
3132

@@ -36,8 +37,8 @@ public void makeSet(T element) {
3637
* Rank is an upper bound on the height of node.
3738
*/
3839
public void union(T left, T right) {
39-
checkNotNull(left, "element");
40-
checkNotNull(right, "element");
40+
checkNotNull(left, elementKey);
41+
checkNotNull(right, elementKey);
4142

4243
Node<T> leftNode = nodeMap.get(left),
4344
rightNode = nodeMap.get(right);

src/main/java/com/dataStructures/GeneralQueue.java renamed to src/main/java/com/datastructures/GeneralQueue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import com.types.Queue;
44

src/main/java/com/dataStructures/IntQueue.java renamed to src/main/java/com/datastructures/IntQueue.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
/**
44
* This file contains an implementation of an integer only queue which is extremely quick and

src/main/java/com/dataStructures/SinglyLinkedList.java renamed to src/main/java/com/datastructures/SinglyLinkedList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33

44

src/main/java/com/dataStructures/Stack.java renamed to src/main/java/com/datastructures/Stack.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import java.io.Serializable;
44
import java.util.EmptyStackException;

src/main/java/com/matchings/stableMatching/GaleShapley.java renamed to src/main/java/com/matchings/stablematching/GaleShapley.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.matchings.stableMatching;
1+
package com.matchings.stablematching;
22

33
public class GaleShapley {
44

src/test/java/com/dataStructures/BinaryTreeTest.java renamed to src/test/java/com/datastructures/BinaryTreeTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;

src/test/java/com/dataStructures/DisjointSetTest.java renamed to src/test/java/com/datastructures/DisjointSetTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;

src/test/java/com/dataStructures/GeneralQueueTest.java renamed to src/test/java/com/datastructures/GeneralQueueTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33

44
import com.types.Queue;

src/test/java/com/dataStructures/IntQueueTest.java renamed to src/test/java/com/datastructures/IntQueueTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import static org.junit.Assert.*;
44

src/test/java/com/dataStructures/SinglyLinkedListTest.java renamed to src/test/java/com/datastructures/SinglyLinkedListTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import org.junit.Test;
44
import org.junit.jupiter.api.Assertions;

src/test/java/com/dataStructures/StackTest.java renamed to src/test/java/com/datastructures/StackTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.dataStructures;
1+
package com.datastructures;
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;

src/test/java/com/matchings/stableMatching/GaleShapleyTest.java renamed to src/test/java/com/matchings/stablematching/GaleShapleyTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.matchings.stableMatching;
1+
package com.matchings.stablematching;
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)