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

View File

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

View File

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

View File

@ -1,8 +1,6 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -15,19 +13,19 @@ public final class MatrixUtil {
private 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; 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); 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); 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)) { if (!hasEqualSizes(matrix1, matrix2)) {
return Optional.empty(); return Optional.empty();
} }
@ -82,78 +80,4 @@ public final class MatrixUtil {
return Optional.of(result); 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; int temp;
while (mid <= h) { while (mid <= h) {
switch (a[mid]) { switch (a[mid]) {
case 0: { case 0:
temp = a[l]; temp = a[l];
a[l] = a[mid]; a[l] = a[mid];
a[mid] = temp; a[mid] = temp;
l++; l++;
mid++; mid++;
break; break;
}
case 1: case 1:
mid++; mid++;
break; break;
case 2: { case 2:
temp = a[mid]; temp = a[mid];
a[mid] = a[h]; a[mid] = a[h];
a[h] = temp; a[h] = temp;
h--; h--;
break; break;
}
default: { default:
throw new IllegalArgumentException("Unexpected value: " + a[mid]); throw new IllegalArgumentException("Unexpected value: " + a[mid]);
} }
}
} }
System.out.println("the Sorted array is "); System.out.println("the Sorted array is ");
for (int i = 0; i < a.length; i++) { for (int i = 0; i < a.length; i++) {

View File

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