Skip to content

style: enable NeedBraces in checkstyle #5227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
<!-- TODO <module name="AvoidNestedBlocks"/> -->
<!-- TODO <module name="EmptyBlock"/> -->
<!-- TODO <module name="LeftCurly"/> -->
<!-- TODO <module name="NeedBraces"/> -->
<module name="NeedBraces"/>
<!-- TODO <module name="RightCurly"/> -->

<!-- Checks for common coding problems -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
* @param <T> the type of elements in the array.
*/
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
if (index + length - currSet.size() > arr.length) return;
if (index + length - currSet.size() > arr.length) {
return;
}
if (length - 1 == currSet.size()) {
for (int i = index; i < arr.length; i++) {
currSet.add(arr[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ static int possiblePaint(ArrayList<Node> nodes, int n, int m) {
// If number of colors used exceeds m,
// return 0
maxColors = Math.max(maxColors, Math.max(nodes.get(top).color, nodes.get(it).color));
if (maxColors > m) return 0;
if (maxColors > m) {
return 0;
}

// If the adjacent node is not visited,
// mark it visited and push it in queue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ private boolean doDFS(int x, int y, int nextIdx) {
int yi = y + dy[i];
if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) {
boolean exists = doDFS(xi, yi, nextIdx + 1);
if (exists) return true;
if (exists) {
return true;
}
}
}
visited[x][y] = false;
Expand All @@ -66,7 +68,9 @@ public boolean exist(char[][] board, String word) {
if (board[i][j] == word.charAt(0)) {
visited = new boolean[board.length][board[0].length];
boolean exists = doDFS(i, j, 1);
if (exists) return true;
if (exists) {
return true;
}
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/thealgorithms/ciphers/Blowfish.java
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,9 @@ private String hexToBin(String hex) {
private String binToHex(String binary) {
long num = Long.parseUnsignedLong(binary, 2);
String hex = Long.toHexString(num);
while (hex.length() < (binary.length() / 4)) hex = "0" + hex;
while (hex.length() < (binary.length() / 4)) {
hex = "0" + hex;
}

return hex;
}
Expand All @@ -1120,7 +1122,9 @@ private String xor(String a, String b) {
a = hexToBin(a);
b = hexToBin(b);
String ans = "";
for (int i = 0; i < a.length(); i++) ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
for (int i = 0; i < a.length(); i++) {
ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
}
ans = binToHex(ans);
return ans;
}
Expand Down Expand Up @@ -1202,7 +1206,9 @@ String encrypt(String plainText, String key) {
// generating key
keyGenerate(key);

for (int i = 0; i < 16; i++) plainText = round(i, plainText);
for (int i = 0; i < 16; i++) {
plainText = round(i, plainText);
}

// postprocessing
String right = plainText.substring(0, 8);
Expand All @@ -1224,7 +1230,9 @@ String decrypt(String cipherText, String key) {
// generating key
keyGenerate(key);

for (int i = 17; i > 1; i--) cipherText = round(i, cipherText);
for (int i = 17; i > 1; i--) {
cipherText = round(i, cipherText);
}

// postprocessing
String right = cipherText.substring(0, 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public void reInitialize() {
}

public BitSet getNextKeyStream() {
for (int cycle = 1; cycle <= INITIAL_CLOCKING_CYCLES; ++cycle) this.clock();
for (int cycle = 1; cycle <= INITIAL_CLOCKING_CYCLES; ++cycle) {
this.clock();
}

BitSet result = new BitSet(KEY_STREAM_LENGTH);
for (int cycle = 1; cycle <= KEY_STREAM_LENGTH; ++cycle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public boolean clock() {
boolean result = false;
for (var register : registers) {
result ^= register.getLastBit();
if (register.getClockBit() == majorityBit) register.clock();
if (register.getClockBit() == majorityBit) {
register.clock();
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ public boolean isFull() {
}

public Item get() {
if (isEmpty()) return null;
if (isEmpty()) {
return null;
}

Item item = buffer[getPointer.getAndIncrement()];
size.decrementAndGet();
return item;
}

public boolean put(Item item) {
if (isFull()) return false;
if (isFull()) {
return false;
}

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

public int getAndIncrement() {
if (pointer == max) pointer = 0;
if (pointer == max) {
pointer = 0;
}
int tmp = pointer;
pointer++;
return tmp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public void findStronglyConnectedComponents(int v, List<List<Integer>> transpose
private void dfs(int node, int[] vis, List<List<Integer>> list) {
vis[node] = 1;
for (Integer neighbour : list.get(node)) {
if (vis[neighbour] == 0) dfs(neighbour, vis, list);
if (vis[neighbour] == 0) {
dfs(neighbour, vis, list);
}
}
stack.push(node);
}
Expand All @@ -136,7 +138,9 @@ private void dfs(int node, int[] vis, List<List<Integer>> list) {
private void dfs2(int node, int[] vis, List<List<Integer>> list) {
vis[node] = 1;
for (Integer neighbour : list.get(node)) {
if (vis[neighbour] == 0) dfs2(neighbour, vis, list);
if (vis[neighbour] == 0) {
dfs2(neighbour, vis, list);
}
}
scc.add(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public List<List<Integer>> stronglyConnectedComponents(int v, List<List<Integer>
Stack<Integer> st = new Stack<Integer>();

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

return sccList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ private Node findEnd(Node n) {
public Node findKey(int key) {
if (!isEmpty()) {
Node temp = first;
if (temp.getKey() == key) return temp;
if (temp.getKey() == key) {
return temp;
}

while ((temp = temp.getNext()) != null) {
if (temp.getKey() == key) return temp;
if (temp.getKey() == key) {
return temp;
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,14 @@ public int findKeyInTable(int key) {
throw new IllegalArgumentException("Table is empty");
}

if (Objects.equals(buckets[hash], wrappedInt)) return hash;
if (Objects.equals(buckets[hash], wrappedInt)) {
return hash;
}

hash = hashFunction2(key);
if (!Objects.equals(buckets[hash], wrappedInt))
if (!Objects.equals(buckets[hash], wrappedInt)) {
throw new IllegalArgumentException("Key " + key + " not found in table");
else {
} else {
return hash;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ private void updateMin(HeapNode posMin) {
private void cascadingCuts(HeapNode curr) {
if (!curr.isMarked()) { // stop the recursion
curr.mark();
if (!curr.isRoot()) this.markedHeapNoodesCounter++;
if (!curr.isRoot()) {
this.markedHeapNoodesCounter++;
}
} else {
if (curr.isRoot()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ public void merge(LeftistHeap h1) {

// Function merge with two Nodes a and b
public Node merge(Node a, Node b) {
if (a == null) return b;
if (a == null) {
return b;
}

if (b == null) return a;
if (b == null) {
return a;
}

// Violates leftist property, so must do a swap
if (a.element > b.element) {
Expand Down Expand Up @@ -93,7 +97,9 @@ public void insert(int a) {
// Returns and removes the minimum element in the heap
public int extractMin() {
// If is empty return -1
if (isEmpty()) return -1;
if (isEmpty()) {
return -1;
}

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

// Auxiliary function for in_order
private void inOrderAux(Node n, ArrayList<Integer> lst) {
if (n == null) return;
if (n == null) {
return;
}
inOrderAux(n.left, lst);
lst.add(n.element);
inOrderAux(n.right, lst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ public final void insertElement(HeapElement element) {

@Override
public void deleteElement(int elementIndex) {
if (maxHeap.isEmpty()) try {
if (maxHeap.isEmpty()) {
try {
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
} catch (EmptyHeapException e) {
e.printStackTrace();
}
}
if ((elementIndex > maxHeap.size()) || (elementIndex <= 0)) {
throw new IndexOutOfBoundsException("Index out of heap range");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ public final void insertElement(HeapElement element) {

@Override
public void deleteElement(int elementIndex) {
if (minHeap.isEmpty()) try {
if (minHeap.isEmpty()) {
try {
throw new EmptyHeapException("Attempt to delete an element from an empty heap");
} catch (EmptyHeapException e) {
e.printStackTrace();
}
}
if ((elementIndex > minHeap.size()) || (elementIndex <= 0)) {
throw new IndexOutOfBoundsException("Index out of heap range");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public boolean isEmpty() {
public boolean isFull() {
if (topOfQueue + 1 == beginningOfQueue) {
return true;
} else
} else {
return topOfQueue == size - 1 && beginningOfQueue == 0;
}
}

public void enQueue(int value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ public T peekRear() {
*/

public T peek(int pos) {
if (pos > size) throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos));
if (pos > size) {
throw new IndexOutOfBoundsException("Position %s out of range!".formatted(pos));
}
Node<T> node = front;
while (pos-- > 0) node = node.next;
while (pos-- > 0) {
node = node.next;
}
return node.data;
}

Expand Down Expand Up @@ -170,14 +174,18 @@ public int size() {
* Clear all nodes in queue
*/
public void clear() {
while (size > 0) dequeue();
while (size > 0) {
dequeue();
}
}

@Override
public String toString() {
StringJoiner join = new StringJoiner(", "); // separator of ', '
Node<T> travel = front;
while ((travel = travel.next) != null) join.add(String.valueOf(travel.data));
while ((travel = travel.next) != null) {
join.add(String.valueOf(travel.data));
}
return '[' + join.toString() + ']';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ private void sink(int pos) {
while (2 * pos <= nItems) {
int current = 2 * pos; // Jump to the positon of child node
// Compare both the children for the greater one
if (current < nItems && queueArray[current] < queueArray[current + 1]) current++;
if (current < nItems && queueArray[current] < queueArray[current + 1]) {
current++;
}
// If the parent node is greater, sink operation is complete. Break the loop
if (queueArray[pos] >= queueArray[current]) break;
if (queueArray[pos] >= queueArray[current]) {
break;
}

// If not exchange the value of parent with child
int temp = queueArray[pos];
Expand Down
Loading