Skip to content

Commit 87b17e0

Browse files
samuelfacSamuel Facchinello
and
Samuel Facchinello
authored
style: enable NeedBraces in checkstyle (#5227)
* enable style NeedBraces * style: enable NeedBraces in checkstyle --------- Co-authored-by: Samuel Facchinello <[email protected]>
1 parent 51fcc66 commit 87b17e0

File tree

68 files changed

+627
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+627
-259
lines changed

checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
<!-- TODO <module name="AvoidNestedBlocks"/> -->
156156
<!-- TODO <module name="EmptyBlock"/> -->
157157
<!-- TODO <module name="LeftCurly"/> -->
158-
<!-- TODO <module name="NeedBraces"/> -->
158+
<module name="NeedBraces"/>
159159
<!-- TODO <module name="RightCurly"/> -->
160160

161161
<!-- Checks for common coding problems -->

src/main/java/com/thealgorithms/backtracking/Combination.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
4343
* @param <T> the type of elements in the array.
4444
*/
4545
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
46-
if (index + length - currSet.size() > arr.length) return;
46+
if (index + length - currSet.size() > arr.length) {
47+
return;
48+
}
4749
if (length - 1 == currSet.size()) {
4850
for (int i = index; i < arr.length; i++) {
4951
currSet.add(arr[i]);

src/main/java/com/thealgorithms/backtracking/MColoring.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ static int possiblePaint(ArrayList<Node> nodes, int n, int m) {
5959
// If number of colors used exceeds m,
6060
// return 0
6161
maxColors = Math.max(maxColors, Math.max(nodes.get(top).color, nodes.get(it).color));
62-
if (maxColors > m) return 0;
62+
if (maxColors > m) {
63+
return 0;
64+
}
6365

6466
// If the adjacent node is not visited,
6567
// mark it visited and push it in queue

src/main/java/com/thealgorithms/backtracking/WordSearch.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ private boolean doDFS(int x, int y, int nextIdx) {
5151
int yi = y + dy[i];
5252
if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) {
5353
boolean exists = doDFS(xi, yi, nextIdx + 1);
54-
if (exists) return true;
54+
if (exists) {
55+
return true;
56+
}
5557
}
5658
}
5759
visited[x][y] = false;
@@ -66,7 +68,9 @@ public boolean exist(char[][] board, String word) {
6668
if (board[i][j] == word.charAt(0)) {
6769
visited = new boolean[board.length][board[0].length];
6870
boolean exists = doDFS(i, j, 1);
69-
if (exists) return true;
71+
if (exists) {
72+
return true;
73+
}
7074
}
7175
}
7276
}

src/main/java/com/thealgorithms/ciphers/Blowfish.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,9 @@ private String hexToBin(String hex) {
11041104
private String binToHex(String binary) {
11051105
long num = Long.parseUnsignedLong(binary, 2);
11061106
String hex = Long.toHexString(num);
1107-
while (hex.length() < (binary.length() / 4)) hex = "0" + hex;
1107+
while (hex.length() < (binary.length() / 4)) {
1108+
hex = "0" + hex;
1109+
}
11081110

11091111
return hex;
11101112
}
@@ -1120,7 +1122,9 @@ private String xor(String a, String b) {
11201122
a = hexToBin(a);
11211123
b = hexToBin(b);
11221124
String ans = "";
1123-
for (int i = 0; i < a.length(); i++) ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
1125+
for (int i = 0; i < a.length(); i++) {
1126+
ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
1127+
}
11241128
ans = binToHex(ans);
11251129
return ans;
11261130
}
@@ -1202,7 +1206,9 @@ String encrypt(String plainText, String key) {
12021206
// generating key
12031207
keyGenerate(key);
12041208

1205-
for (int i = 0; i < 16; i++) plainText = round(i, plainText);
1209+
for (int i = 0; i < 16; i++) {
1210+
plainText = round(i, plainText);
1211+
}
12061212

12071213
// postprocessing
12081214
String right = plainText.substring(0, 8);
@@ -1224,7 +1230,9 @@ String decrypt(String cipherText, String key) {
12241230
// generating key
12251231
keyGenerate(key);
12261232

1227-
for (int i = 17; i > 1; i--) cipherText = round(i, cipherText);
1233+
for (int i = 17; i > 1; i--) {
1234+
cipherText = round(i, cipherText);
1235+
}
12281236

12291237
// postprocessing
12301238
String right = cipherText.substring(0, 8);

src/main/java/com/thealgorithms/ciphers/a5/A5KeyStreamGenerator.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public void reInitialize() {
3131
}
3232

3333
public BitSet getNextKeyStream() {
34-
for (int cycle = 1; cycle <= INITIAL_CLOCKING_CYCLES; ++cycle) this.clock();
34+
for (int cycle = 1; cycle <= INITIAL_CLOCKING_CYCLES; ++cycle) {
35+
this.clock();
36+
}
3537

3638
BitSet result = new BitSet(KEY_STREAM_LENGTH);
3739
for (int cycle = 1; cycle <= KEY_STREAM_LENGTH; ++cycle) {

src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public boolean clock() {
1919
boolean result = false;
2020
for (var register : registers) {
2121
result ^= register.getLastBit();
22-
if (register.getClockBit() == majorityBit) register.clock();
22+
if (register.getClockBit() == majorityBit) {
23+
register.clock();
24+
}
2325
}
2426
return result;
2527
}

src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ public boolean isFull() {
2424
}
2525

2626
public Item get() {
27-
if (isEmpty()) return null;
27+
if (isEmpty()) {
28+
return null;
29+
}
2830

2931
Item item = buffer[getPointer.getAndIncrement()];
3032
size.decrementAndGet();
3133
return item;
3234
}
3335

3436
public boolean put(Item item) {
35-
if (isFull()) return false;
37+
if (isFull()) {
38+
return false;
39+
}
3640

3741
buffer[putPointer.getAndIncrement()] = item;
3842
size.incrementAndGet();
@@ -49,7 +53,9 @@ private static class CircularPointer {
4953
}
5054

5155
public int getAndIncrement() {
52-
if (pointer == max) pointer = 0;
56+
if (pointer == max) {
57+
pointer = 0;
58+
}
5359
int tmp = pointer;
5460
pointer++;
5561
return tmp;

src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public void findStronglyConnectedComponents(int v, List<List<Integer>> transpose
127127
private void dfs(int node, int[] vis, List<List<Integer>> list) {
128128
vis[node] = 1;
129129
for (Integer neighbour : list.get(node)) {
130-
if (vis[neighbour] == 0) dfs(neighbour, vis, list);
130+
if (vis[neighbour] == 0) {
131+
dfs(neighbour, vis, list);
132+
}
131133
}
132134
stack.push(node);
133135
}
@@ -136,7 +138,9 @@ private void dfs(int node, int[] vis, List<List<Integer>> list) {
136138
private void dfs2(int node, int[] vis, List<List<Integer>> list) {
137139
vis[node] = 1;
138140
for (Integer neighbour : list.get(node)) {
139-
if (vis[neighbour] == 0) dfs2(neighbour, vis, list);
141+
if (vis[neighbour] == 0) {
142+
dfs2(neighbour, vis, list);
143+
}
140144
}
141145
scc.add(node);
142146
}

src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ public List<List<Integer>> stronglyConnectedComponents(int v, List<List<Integer>
8282
Stack<Integer> st = new Stack<Integer>();
8383

8484
for (int i = 0; i < v; i++) {
85-
if (insertionTime[i] == -1) stronglyConnCompsUtil(i, lowTime, insertionTime, isInStack, st, graph);
85+
if (insertionTime[i] == -1) {
86+
stronglyConnCompsUtil(i, lowTime, insertionTime, isInStack, st, graph);
87+
}
8688
}
8789

8890
return sccList;

src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMap.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ private Node findEnd(Node n) {
6868
public Node findKey(int key) {
6969
if (!isEmpty()) {
7070
Node temp = first;
71-
if (temp.getKey() == key) return temp;
71+
if (temp.getKey() == key) {
72+
return temp;
73+
}
7274

7375
while ((temp = temp.getNext()) != null) {
74-
if (temp.getKey() == key) return temp;
76+
if (temp.getKey() == key) {
77+
return temp;
78+
}
7579
}
7680
}
7781
return null;

src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapCuckooHashing.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ public int findKeyInTable(int key) {
188188
throw new IllegalArgumentException("Table is empty");
189189
}
190190

191-
if (Objects.equals(buckets[hash], wrappedInt)) return hash;
191+
if (Objects.equals(buckets[hash], wrappedInt)) {
192+
return hash;
193+
}
192194

193195
hash = hashFunction2(key);
194-
if (!Objects.equals(buckets[hash], wrappedInt))
196+
if (!Objects.equals(buckets[hash], wrappedInt)) {
195197
throw new IllegalArgumentException("Key " + key + " not found in table");
196-
else {
198+
} else {
197199
return hash;
198200
}
199201
}

src/main/java/com/thealgorithms/datastructures/heaps/FibonacciHeap.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ private void updateMin(HeapNode posMin) {
231231
private void cascadingCuts(HeapNode curr) {
232232
if (!curr.isMarked()) { // stop the recursion
233233
curr.mark();
234-
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
234+
if (!curr.isRoot()) {
235+
this.markedHeapNoodesCounter++;
236+
}
235237
} else {
236238
if (curr.isRoot()) {
237239
return;

src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ public void merge(LeftistHeap h1) {
5656

5757
// Function merge with two Nodes a and b
5858
public Node merge(Node a, Node b) {
59-
if (a == null) return b;
59+
if (a == null) {
60+
return b;
61+
}
6062

61-
if (b == null) return a;
63+
if (b == null) {
64+
return a;
65+
}
6266

6367
// Violates leftist property, so must do a swap
6468
if (a.element > b.element) {
@@ -93,7 +97,9 @@ public void insert(int a) {
9397
// Returns and removes the minimum element in the heap
9498
public int extractMin() {
9599
// If is empty return -1
96-
if (isEmpty()) return -1;
100+
if (isEmpty()) {
101+
return -1;
102+
}
97103

98104
int min = root.element;
99105
root = merge(root.left, root.right);
@@ -109,7 +115,9 @@ public ArrayList<Integer> inOrder() {
109115

110116
// Auxiliary function for in_order
111117
private void inOrderAux(Node n, ArrayList<Integer> lst) {
112-
if (n == null) return;
118+
if (n == null) {
119+
return;
120+
}
113121
inOrderAux(n.left, lst);
114122
lst.add(n.element);
115123
inOrderAux(n.right, lst);

src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ public final void insertElement(HeapElement element) {
9898

9999
@Override
100100
public void deleteElement(int elementIndex) {
101-
if (maxHeap.isEmpty()) try {
101+
if (maxHeap.isEmpty()) {
102+
try {
102103
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
103104
} catch (EmptyHeapException e) {
104105
e.printStackTrace();
105106
}
107+
}
106108
if ((elementIndex > maxHeap.size()) || (elementIndex <= 0)) {
107109
throw new IndexOutOfBoundsException("Index out of heap range");
108110
}

src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ public final void insertElement(HeapElement element) {
9292

9393
@Override
9494
public void deleteElement(int elementIndex) {
95-
if (minHeap.isEmpty()) try {
95+
if (minHeap.isEmpty()) {
96+
try {
9697
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
9798
} catch (EmptyHeapException e) {
9899
e.printStackTrace();
99100
}
101+
}
100102
if ((elementIndex > minHeap.size()) || (elementIndex <= 0)) {
101103
throw new IndexOutOfBoundsException("Index out of heap range");
102104
}

src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public boolean isEmpty() {
2323
public boolean isFull() {
2424
if (topOfQueue + 1 == beginningOfQueue) {
2525
return true;
26-
} else
26+
} else {
2727
return topOfQueue == size - 1 && beginningOfQueue == 0;
28+
}
2829
}
2930

3031
public void enQueue(int value) {

src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ public T peekRear() {
125125
*/
126126

127127
public T peek(int pos) {
128-
if (pos > size) throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos));
128+
if (pos > size) {
129+
throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos));
130+
}
129131
Node<T> node = front;
130-
while (pos-- > 0) node = node.next;
132+
while (pos-- > 0) {
133+
node = node.next;
134+
}
131135
return node.data;
132136
}
133137

@@ -170,14 +174,18 @@ public int size() {
170174
* Clear all nodes in queue
171175
*/
172176
public void clear() {
173-
while (size > 0) dequeue();
177+
while (size > 0) {
178+
dequeue();
179+
}
174180
}
175181

176182
@Override
177183
public String toString() {
178184
StringJoiner join = new StringJoiner(", "); // separator of ', '
179185
Node<T> travel = front;
180-
while ((travel = travel.next) != null) join.add(String.valueOf(travel.data));
186+
while ((travel = travel.next) != null) {
187+
join.add(String.valueOf(travel.data));
188+
}
181189
return '[' + join.toString() + ']';
182190
}
183191

src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ private void sink(int pos) {
8787
while (2 * pos <= nItems) {
8888
int current = 2 * pos; // Jump to the positon of child node
8989
// Compare both the children for the greater one
90-
if (current < nItems && queueArray[current] < queueArray[current + 1]) current++;
90+
if (current < nItems && queueArray[current] < queueArray[current + 1]) {
91+
current++;
92+
}
9193
// If the parent node is greater, sink operation is complete. Break the loop
92-
if (queueArray[pos] >= queueArray[current]) break;
94+
if (queueArray[pos] >= queueArray[current]) {
95+
break;
96+
}
9397

9498
// If not exchange the value of parent with child
9599
int temp = queueArray[pos];

0 commit comments

Comments
 (0)