style: enable AvoidNestedBlocks in checkstyle (#5228)

* enable style AvoidNestedBlocks

* refactor after enable style AvoidNestedBlocks

* fix clang

* fix checkstyle

* fix pmd

---------

Co-authored-by: Samuel Facchinello <samuel.facchinello@piksel.com>
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
Samuel Facchinello 2024-06-14 16:57:30 +02:00 committed by GitHub
parent 87b17e0571
commit cdb3affdd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 117 additions and 117 deletions

View File

@ -152,7 +152,7 @@
<!-- Checks for blocks. You know, those {}'s -->
<!-- See https://checkstyle.org/checks/blocks/index.html -->
<!-- TODO <module name="AvoidNestedBlocks"/> -->
<module name="AvoidNestedBlocks"/>
<!-- TODO <module name="EmptyBlock"/> -->
<!-- TODO <module name="LeftCurly"/> -->
<module name="NeedBraces"/>

View File

@ -23,31 +23,30 @@ public final class Main {
choice = scan.nextInt();
switch (choice) {
case 1: {
case 1:
System.out.println("Enter the Key: ");
key = scan.nextInt();
h.insertHash(key);
break;
}
case 2: {
case 2:
System.out.println("Enter the Key delete: ");
key = scan.nextInt();
h.deleteHash(key);
break;
}
case 3: {
case 3:
System.out.println("Print table");
h.displayHashtable();
break;
}
case 4: {
case 4:
scan.close();
return;
}
default: {
default:
throw new IllegalArgumentException("Unexpected value: " + choice);
}
}
}
}
}

View File

@ -27,45 +27,44 @@ public final class MainCuckooHashing {
choice = scan.nextInt();
switch (choice) {
case 1: {
case 1:
System.out.println("Enter the Key: ");
key = scan.nextInt();
h.insertKey2HashTable(key);
break;
}
case 2: {
case 2:
System.out.println("Enter the Key delete: ");
key = scan.nextInt();
h.deleteKeyFromHashTable(key);
break;
}
case 3: {
case 3:
System.out.println("Print table:\n");
h.displayHashtable();
break;
}
case 4: {
case 4:
scan.close();
return;
}
case 5: {
case 5:
System.out.println("Enter the Key to find and print: ");
key = scan.nextInt();
System.out.println("Key: " + key + " is at index: " + h.findKeyInTable(key) + "\n");
break;
}
case 6: {
case 6:
System.out.printf("Load factor is: %.2f%n", h.checkLoadFactor());
break;
}
case 7: {
case 7:
h.reHashTableIncreasesTableSize();
break;
}
default: {
default:
throw new IllegalArgumentException("Unexpected value: " + choice);
}
}
}
}
}

View File

@ -1,8 +1,6 @@
package com.thealgorithms.maths;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.IntStream;
@ -15,19 +13,19 @@ public final class MatrixUtil {
private MatrixUtil() {
}
public static boolean isValid(final BigDecimal[][] matrix) {
private static boolean isValid(final BigDecimal[][] matrix) {
return matrix != null && matrix.length > 0 && matrix[0].length > 0;
}
public static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
private static boolean hasEqualSizes(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
return (isValid(matrix1) && isValid(matrix2) && matrix1.length == matrix2.length && matrix1[0].length == matrix2[0].length);
}
public static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
private static boolean canMultiply(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2) {
return (isValid(matrix1) && isValid(matrix2) && matrix1[0].length == matrix2.length);
}
public static Optional<BigDecimal[][]> operate(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2, final BiFunction<BigDecimal, BigDecimal, BigDecimal> operation) {
private static Optional<BigDecimal[][]> operate(final BigDecimal[][] matrix1, final BigDecimal[][] matrix2, final BiFunction<BigDecimal, BigDecimal, BigDecimal> operation) {
if (!hasEqualSizes(matrix1, matrix2)) {
return Optional.empty();
}
@ -82,78 +80,4 @@ public final class MatrixUtil {
return Optional.of(result);
}
public static void assertThat(final BigDecimal[][] actual, final BigDecimal[][] expected) {
if (!Objects.deepEquals(actual, expected)) {
throw new AssertionError(String.format("expected=%s but was actual=%s", Arrays.deepToString(expected), Arrays.deepToString(actual)));
}
}
public static void main(final String[] args) {
{
final BigDecimal[][] matrix1 = {
{new BigDecimal(3), new BigDecimal(2)},
{new BigDecimal(0), new BigDecimal(1)},
};
final BigDecimal[][] matrix2 = {
{new BigDecimal(1), new BigDecimal(3)},
{new BigDecimal(2), new BigDecimal(0)},
};
final BigDecimal[][] actual = add(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
final BigDecimal[][] expected = {
{new BigDecimal(4), new BigDecimal(5)},
{new BigDecimal(2), new BigDecimal(1)},
};
assertThat(actual, expected);
}
{
final BigDecimal[][] matrix1 = {
{new BigDecimal(1), new BigDecimal(4)},
{new BigDecimal(5), new BigDecimal(6)},
};
final BigDecimal[][] matrix2 = {
{new BigDecimal(2), new BigDecimal(0)},
{new BigDecimal(-2), new BigDecimal(-3)},
};
final BigDecimal[][] actual = subtract(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
final BigDecimal[][] expected = {
{new BigDecimal(-1), new BigDecimal(4)},
{new BigDecimal(7), new BigDecimal(9)},
};
assertThat(actual, expected);
}
{
final BigDecimal[][] matrix1 = {
{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)},
{new BigDecimal(4), new BigDecimal(5), new BigDecimal(6)},
{new BigDecimal(7), new BigDecimal(8), new BigDecimal(9)},
};
final BigDecimal[][] matrix2 = {
{new BigDecimal(1), new BigDecimal(2)},
{new BigDecimal(3), new BigDecimal(4)},
{new BigDecimal(5), new BigDecimal(6)},
};
final BigDecimal[][] actual = multiply(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
final BigDecimal[][] expected = {
{new BigDecimal(22), new BigDecimal(28)},
{new BigDecimal(49), new BigDecimal(64)},
{new BigDecimal(76), new BigDecimal(100)},
};
assertThat(actual, expected);
}
}
}

View File

@ -33,28 +33,27 @@ public final class Sort012D {
int temp;
while (mid <= h) {
switch (a[mid]) {
case 0: {
case 0:
temp = a[l];
a[l] = a[mid];
a[mid] = temp;
l++;
mid++;
break;
}
case 1:
mid++;
break;
case 2: {
case 2:
temp = a[mid];
a[mid] = a[h];
a[h] = temp;
h--;
break;
}
default: {
default:
throw new IllegalArgumentException("Unexpected value: " + a[mid]);
}
}
}
System.out.println("the Sorted array is ");
for (int i = 0; i < a.length; i++) {

View File

@ -13,24 +13,24 @@ public final class DNFSort {
int temp;
while (mid <= high) {
switch (a[mid]) {
case 0: {
case 0:
temp = a[low];
a[low] = a[mid];
a[mid] = temp;
low++;
mid++;
break;
}
case 1:
mid++;
break;
case 2: {
case 2:
temp = a[mid];
a[mid] = a[high];
a[high] = temp;
high--;
break;
}
default:
throw new IllegalArgumentException("Unexpected value: " + a[mid]);
}

View File

@ -0,0 +1,79 @@
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigDecimal;
import java.util.Objects;
import org.junit.jupiter.api.Test;
class MatrixUtilTest {
@Test
void add() {
final BigDecimal[][] matrix1 = {
{new BigDecimal(3), new BigDecimal(2)},
{BigDecimal.ZERO, BigDecimal.ONE},
};
final BigDecimal[][] matrix2 = {
{BigDecimal.ONE, new BigDecimal(3)},
{new BigDecimal(2), BigDecimal.ZERO},
};
final BigDecimal[][] actual = MatrixUtil.add(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
final BigDecimal[][] expected = {
{new BigDecimal(4), new BigDecimal(5)},
{new BigDecimal(2), BigDecimal.ONE},
};
assertTrue(Objects.deepEquals(actual, expected));
}
@Test
void subtract() {
final BigDecimal[][] matrix1 = {
{BigDecimal.ONE, new BigDecimal(4)},
{new BigDecimal(5), new BigDecimal(6)},
};
final BigDecimal[][] matrix2 = {
{new BigDecimal(2), BigDecimal.ZERO},
{new BigDecimal(-2), new BigDecimal(-3)},
};
final BigDecimal[][] actual = MatrixUtil.subtract(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
final BigDecimal[][] expected = {
{new BigDecimal(-1), new BigDecimal(4)},
{new BigDecimal(7), new BigDecimal(9)},
};
assertTrue(Objects.deepEquals(actual, expected));
}
@Test
void multiply() {
final BigDecimal[][] matrix1 = {
{BigDecimal.ONE, new BigDecimal(2), new BigDecimal(3)},
{new BigDecimal(4), new BigDecimal(5), new BigDecimal(6)},
{new BigDecimal(7), new BigDecimal(8), new BigDecimal(9)},
};
final BigDecimal[][] matrix2 = {
{BigDecimal.ONE, new BigDecimal(2)},
{new BigDecimal(3), new BigDecimal(4)},
{new BigDecimal(5), new BigDecimal(6)},
};
final BigDecimal[][] actual = MatrixUtil.multiply(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
final BigDecimal[][] expected = {
{new BigDecimal(22), new BigDecimal(28)},
{new BigDecimal(49), new BigDecimal(64)},
{new BigDecimal(76), new BigDecimal(100)},
};
assertTrue(Objects.deepEquals(actual, expected));
}
}