From 2639adcf97e16cd3928617bbcc5d6397fda797ae Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 9 Oct 2022 16:03:57 +0800 Subject: [PATCH 1/6] separated tests to another project --- .../pitzzahh/computing/CalculatorTest.java | 69 ---- .../sumOfAllNumbers/SumOfAllNumbersTest.java | 15 - .../sorting/quickSort/QuickSortTest.java | 39 -- .../pitzzahh/utilities/FileUtilTest.java | 41 -- .../pitzzahh/utilities/SecurityUtilTest.java | 19 - .../github/pitzzahh/utilities/UtilTest.java | 132 ------ .../utilities/classes/PersonTest.java | 12 - .../utilities/validation/ValidatorTest.java | 376 ------------------ 8 files changed, 703 deletions(-) delete mode 100644 src/test/java/io/github/pitzzahh/computing/CalculatorTest.java delete mode 100644 src/test/java/io/github/pitzzahh/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbersTest.java delete mode 100644 src/test/java/io/github/pitzzahh/computing/algorithms/sorting/quickSort/QuickSortTest.java delete mode 100644 src/test/java/io/github/pitzzahh/utilities/FileUtilTest.java delete mode 100644 src/test/java/io/github/pitzzahh/utilities/SecurityUtilTest.java delete mode 100644 src/test/java/io/github/pitzzahh/utilities/UtilTest.java delete mode 100644 src/test/java/io/github/pitzzahh/utilities/classes/PersonTest.java delete mode 100644 src/test/java/io/github/pitzzahh/utilities/validation/ValidatorTest.java diff --git a/src/test/java/io/github/pitzzahh/computing/CalculatorTest.java b/src/test/java/io/github/pitzzahh/computing/CalculatorTest.java deleted file mode 100644 index 7c2148c..0000000 --- a/src/test/java/io/github/pitzzahh/computing/CalculatorTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package io.github.pitzzahh.computing; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import io.github.pitzzahh.utilities.classes.enums.Operation; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import java.math.BigDecimal; - -class CalculatorTest { - - @Nested - class Arithmetic { - @Test - void multiply() { - // given - var calculator = new Calculator(); - // when - var result = calculator.calculate(4, 2, Operation.MULTIPLICATION); - var expected = BigDecimal.valueOf(8); - // then - assertEquals(expected, result); - } - - @Test - void divide() { - // given - var calculator = new Calculator(); - // when - var result = calculator.calculate(5, 5, Operation.DIVISION); - var expected = BigDecimal.valueOf(1); - // then - assertEquals(expected, result); - } - - @Test - void add() { - // given - var calculator = new Calculator(); - // when - var result = calculator.calculate(4,2, Operation.ADDITION); - var expected = BigDecimal.valueOf(6); - // then - assertEquals(expected, result); - } - - @Test - void subtract() { - // given - var calculator = new Calculator(); - // when - var result = calculator.calculate(4, 2, Operation.SUBTRACTION); - var expected = BigDecimal.valueOf(2); - // then - assertEquals(expected, result); - } - - @Test - void mod() { - // given - var calculator = new Calculator(); - // when - var result = calculator.calculate(4, 2, Operation.MODULO); - var expected = BigDecimal.ZERO; - // then - assertEquals(expected, result); - } - } - -} \ No newline at end of file diff --git a/src/test/java/io/github/pitzzahh/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbersTest.java b/src/test/java/io/github/pitzzahh/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbersTest.java deleted file mode 100644 index 1cbbab7..0000000 --- a/src/test/java/io/github/pitzzahh/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbersTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.github.pitzzahh.computing.algorithms.recursion.sumOfAllNumbers; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -class SumOfAllNumbersTest { - - @Test - void shouldPassIfSumOfASequenceOfNumbersAreEqual() { - var sumOfAllNumbers = new SumOfAllNumbers<>(3); // sum numbers from 1 to 3: 1 + 2 + 3 = 6 - var sum = sumOfAllNumbers.getSumOfAllNumbers(); - assertEquals(6, sum.intValue()); - } - -} \ No newline at end of file diff --git a/src/test/java/io/github/pitzzahh/computing/algorithms/sorting/quickSort/QuickSortTest.java b/src/test/java/io/github/pitzzahh/computing/algorithms/sorting/quickSort/QuickSortTest.java deleted file mode 100644 index 1582a96..0000000 --- a/src/test/java/io/github/pitzzahh/computing/algorithms/sorting/quickSort/QuickSortTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.github.pitzzahh.computing.algorithms.sorting.quickSort; - -import java.util.Arrays; - -import io.github.pitzzahh.computing.algorithms.sorting.QuickSort; -import org.junit.jupiter.api.Test; - -class QuickSortTest { - - @Test - void shouldSortAByteArray() { - // given - var array = new Byte[] { 3, 1, 2 }; - // when - QuickSort.sort(array); - // then - System.out.println(Arrays.toString(array)); - } - - @Test - void shouldSortAIntegerArray() { - // given - var array = new Integer[] { 3, 1, 2 }; - // when - QuickSort.sort(array); - // then - System.out.println(Arrays.toString(array)); - } - - @Test - void shouldSortADoubleArray() { - // given - var array = new Double[] { 3.5, 1.5, 2.5 }; - // when - QuickSort.sort(array); - // then - System.out.println(Arrays.toString(array)); - } -} \ No newline at end of file diff --git a/src/test/java/io/github/pitzzahh/utilities/FileUtilTest.java b/src/test/java/io/github/pitzzahh/utilities/FileUtilTest.java deleted file mode 100644 index e25ddd6..0000000 --- a/src/test/java/io/github/pitzzahh/utilities/FileUtilTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.github.pitzzahh.utilities; - -import java.io.File; -import java.io.IOException; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.TestMethodOrder; -import static org.junit.jupiter.api.Assertions.assertTrue; - -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) -class FileUtilTest { - - private final File file = new File("file.txt"); - - @Test - @Order(1) - void shouldWriteToATextFile() throws IOException { - // given - var message = "123123123"; - // when - FileUtil.writeToATextFile(message, file, false); - // then - - } - - @Test - @Order(2) - void printFileContents() throws IOException { - FileUtil.printFileContents(file); - } - - @Test - @Order(3) - void removeFileOnExit() { - // when - var result = file.delete(); - // then - assertTrue(result); - } -} \ No newline at end of file diff --git a/src/test/java/io/github/pitzzahh/utilities/SecurityUtilTest.java b/src/test/java/io/github/pitzzahh/utilities/SecurityUtilTest.java deleted file mode 100644 index 36bbafe..0000000 --- a/src/test/java/io/github/pitzzahh/utilities/SecurityUtilTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.github.pitzzahh.utilities; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -class SecurityUtilTest { - - @Test - void shouldDecrypt() { - // given - var text = "200263444"; - // when - var encrypted = SecurityUtil.encrypt(text); - var decryped = SecurityUtil.decrypt(encrypted); - // then - assertEquals("200263444", decryped); - } - -} \ No newline at end of file diff --git a/src/test/java/io/github/pitzzahh/utilities/UtilTest.java b/src/test/java/io/github/pitzzahh/utilities/UtilTest.java deleted file mode 100644 index ed82008..0000000 --- a/src/test/java/io/github/pitzzahh/utilities/UtilTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package io.github.pitzzahh.utilities; - -import java.math.BigDecimal; -import java.util.List; -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -class UtilTest { - - @Test - void shouldPassIfByteArrayAreAllTheSameNumber1() { - var arr = new Byte[] {1, 1, 1, 1}; - assertTrue(Util.areAllTheSame(arr)); - } - @Test - void shouldPassIfByteArrayAreAllTheSameNumber2() { - var arr = new Byte[] {2, 2, 2, 2}; - assertTrue(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfByteArrayAreAllTheSameNumber3() { - var arr = new Byte[] {1, 1, 1, 1}; - assertTrue(Util.areAllTheSame(arr, () -> 1)); - } - @Test - void shouldPassIfByteArrayAreAllTheSameNumber4() { - var arr = new Byte[] {2, 2, 2, 2}; - assertTrue(Util.areAllTheSame(arr, () -> 2)); - } - - @Test - void shouldPassIfByteArrayElementsAreNotTheSame() { - var arr = new Byte[] {1, 2, 3, 1}; - assertFalse(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfByteArrayElementsAreNotTheSame1() { - var arr = new Byte[] {1, 2, 3, 1}; - assertFalse(Util.areAllTheSame(arr, () -> 1)); - } - - @Test - void shouldPassIfShortArrayAreAllTheSameNumber1() { - var arr = new Short[] {1, 1, 1, 1}; - assertTrue(Util.areAllTheSame(arr)); - } - @Test - void shouldPassIfShortArrayAreAllTheSameNumber2() { - var arr = new Short[] {2, 2, 2, 2}; - assertTrue(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfShortArrayElementsAreNotTheSame() { - var arr = new Short[] {1, 2, 3, 1}; - assertFalse(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfIntArrayAreAllTheSameNumber1() { - var arr = new Integer[] {1, 1, 1, 1}; - assertTrue(Util.areAllTheSame(arr)); - } - @Test - void shouldPassIfIntArrayAreAllTheSameNumber2() { - var arr = new Integer[] {2, 2, 2, 2}; - assertTrue(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfIntArrayElementsAreNotTheSame() { - var arr = new Integer[] {1, 2, 3, 1}; - assertFalse(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfBooleanArrayElementsAreAllTheSame1() { - var arr = new Boolean[] { true, true, true, true }; - assertTrue(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfBooleanArrayElementsAreAllTheSame2() { - var arr = new Boolean[] { false, false, false, false }; - assertTrue(Util.areAllTheSame(arr)); - } - - @Test - void shouldPassIfBooleanArrayElementsAreNotTheSame() { - // given - var arr = new Boolean[] { true, false, true, false }; - // when - var result = Util.areAllTheSame(arr); - // then - assertFalse(result); - } - - @Test - void shouldPassIfBooleanArrayElementsAreAllTheSame3() { - var arr = new Boolean[] { false, false, false, false }; - assertTrue(Util.areAllTheSame(arr, () -> false)); - } - - @Test - void shouldPassIfBooleanArrayElementsAreNotTheSame3() { - // given - var arr = new Boolean[] { true, false, true, true }; - // when - var result = Util.areAllTheSame(arr, () -> true); - // then - assertFalse(result); - } - - @Test - void shouldPassIfSumIsTheSame() { - // given - var list = List.of(10, 20, 30, 40); - // when - var result = Util.sum(list, Util.allowAll()); - var expected = BigDecimal.valueOf(100); - // then - assertEquals(expected, result); - } - - @Test - void convertListToString() { - String s = Util.convertToString(List.of('P', 'E', 'T', 'E', 'R')); - assertEquals("PETER", s); - } -} \ No newline at end of file diff --git a/src/test/java/io/github/pitzzahh/utilities/classes/PersonTest.java b/src/test/java/io/github/pitzzahh/utilities/classes/PersonTest.java deleted file mode 100644 index 66f28d7..0000000 --- a/src/test/java/io/github/pitzzahh/utilities/classes/PersonTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.github.pitzzahh.utilities.classes; - -import org.junit.jupiter.api.Test; - -class PersonTest { - - @Test - void canCreateEmptyPerson() { - Person person = Person.builder().build(); - System.out.println("person = " + person); - } -} \ No newline at end of file diff --git a/src/test/java/io/github/pitzzahh/utilities/validation/ValidatorTest.java b/src/test/java/io/github/pitzzahh/utilities/validation/ValidatorTest.java deleted file mode 100644 index 770eb6e..0000000 --- a/src/test/java/io/github/pitzzahh/utilities/validation/ValidatorTest.java +++ /dev/null @@ -1,376 +0,0 @@ -package io.github.pitzzahh.utilities.validation; - -import io.github.pitzzahh.utilities.classes.enums.Gender; -import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; - -class ValidatorTest { - - @Test - void shouldPassIfIdIsValid() { - // given - var id = "122021"; - // when - var result = Validator.isIdValid().test(id); - // then - assertTrue(result); - } - - @Test - void shouldPassIfIdIsInvalid() { - // given - var id = "123"; - // when - var result = Validator.isIdValid().test(id); - // then - assertFalse(result); - } - - @Test - void shouldPassIfIdIsStringAndInvalid() { - // given - var id = "pizza"; - // when - var result = Validator.isIdValid().test(id); - // then - assertFalse(result); - } - - @Test - void shouldPassIfAgeIsValid() { - // given - var age = "12"; - // when - var result = Validator.isAgeValid().test(age); - // then - assertTrue(result); - } - - @Test - void shouldPassIfAgeIsZeroAndInvalid() { - // given - var age = "0"; - // when - var result = Validator.isAgeValid().test(age); - // then - assertFalse(result); - } - - @Test - void shouldPassIfAgeIsAStringAndInvalidAndThrowsNumberFormatException() { - // given - var age = "pizza"; - // then - assertThrows(NumberFormatException.class, () -> { - var result = Validator.isAgeValid().test(age); - assertFalse(result); - }); - } - - @Test - void shouldPassIfGenderIsValidMale() { - // given - var gender = Gender.MALE.toString(); - // when - var result = Validator.isGenderValid().test(gender); - // then - assertTrue(result); - } - - @Test - void shouldPassIfGenderIsValidFemale() { - // given - var gender = Gender.FEMALE; - // when - var result = Validator.isGenderValid().test(gender.name()); - // then - assertTrue(result); - } - - @Test - void shouldPassIfGenderIsValidLGBT() { - // given - var gender = Gender.LGBT; - // when - var result = Validator.isGenderValid().test(gender.name()); - // then - assertTrue(result); - } - - @Test - void shouldPassIfGenderIsValidAndPreferedNotToSay() { - // given - var gender = Gender.PREFER_NOT_TO_SAY; - // when - var result = Validator.isGenderValid().test(gender.name()); - // then - assertTrue(result); - } - - @Test - void shouldPassIfGenderIsInvalidAndThrowsIllegalArgumentException() { - // when - assertThrows(IllegalArgumentException.class, () -> { - // given - var gender = Gender.valueOf("other gender"); - // then - var result = Validator.isGenderValid().test(gender.name()); - // when - assertFalse(result); - }); - } - - @Test - void shouldPassIfStringIsASingleCharacterInTheAlphabetInLoweCase() { - // given - var letters = "abcdefghijklmnopqrstuvwxyz"; - // when - for (int i = 0; i < letters.length(); i++) { - // then - var result = Validator.isLetter().test(String.valueOf(letters.charAt(i))); - assertTrue(result); - } - } - - @Test - void shouldPassIfStringIsASingleCharacterInTheAlphabetInUpperCase() { - // given - var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - // when - for (int i = 0; i < letters.length(); i++) { - // then - var result = Validator.isLetter().test(String.valueOf(letters.charAt(i))); - assertTrue(result); - } - } - - @Test - void shouldPassIfStringNotASingleCharacterInTheAlphabet() { - // given - var letters = "!@#$%^&*()_+-=:;\"'<,>./?`~"; - // when - for (int i = 0; i < letters.length(); i++) { - // then - var result = Validator.isLetter().test(String.valueOf(letters.charAt(i))); - assertFalse(result); - } - } - - @Test - void shouldPassIfStringIsUpperCaseOrLowerCaseY() { - // given - var letters = "Yy"; - // when - for (int i = 0; i < letters.length(); i++) { - // then - var result = Validator.isYes().test(String.valueOf(letters.charAt(i))); - assertTrue(result); - } - } - - @Test - void shouldPassIfStringIsNotAnUpperCaseOrLowerCaseY() { - // given - var letters = "Pp"; - // when - for (int i = 0; i < letters.length(); i++) { - // then - var result = Validator.isYes().test(String.valueOf(letters.charAt(i))); - assertFalse(result); - } - } - - @Test - void shouldPassIfStringIsUpperCaseOrLowerCaseN() { - // given - var letters = "Nn"; - // when - for (int i = 0; i < letters.length(); i++) { - // then - var result = Validator.isNo().test(String.valueOf(letters.charAt(i))); - assertTrue(result); - } - } - - @Test - void shouldPassIfStringIsNotAnUpperCaseOrLowerCaseN() { - // given - var letters = "Aa"; - // when - for (int i = 0; i < letters.length(); i++) { - // then - var result = Validator.isNo().test(String.valueOf(letters.charAt(i))); - assertFalse(result); - } - } - - @Test - void shouldPassIfNumberIsAWholeNumber() { - // given - var number = "123"; - // when - var result = Validator.isWholeNumber().test(number); - // then - assertTrue(result); - } - - @Test - void shouldPassIfNumberIsNotAWholeNumber() { - // given - var number = "123.123"; - // when - var result = Validator.isWholeNumber().test(number); - // then - assertFalse(result); - } - - @Test - void shouldPassIfNumberIsADecimalNumber() { - // given - var number = "123.123"; - // when - var result = Validator.isDecimalNumber().test(number); - // then - assertTrue(result); - } - - @Test - void shouldPassIfNumberIsNotADecimalNumber() { - // given - var number = "123"; - // when - var result = Validator.isDecimalNumber().test(number); - // then - assertFalse(result); - } - - @Test - void shouldPassIfBirthDateIsValid() { - // given - var year = "2002"; - var month = "8"; - var day = "24"; - // when - var result = Validator.isBirthDateValid().test(year + "-" + month + "-" + day); - // then - assertTrue(result); - } - - @Test - void shouldPassIfBirthDateIsInValidBecauseYearIsOnlyThreeCharacters() { - // given - var year = "123"; - var month = "8"; - var day = "24"; - // when - var result = Validator.isBirthDateValid().test(year + "-" + month + "-" + day); - // then - assertFalse(result); - } - - @Test - void shouldPassIfBirthDateIsInValidBecauseYearIsInvalid() { - // given - var year = "1010"; - var month = "8"; - var day = "24"; - // when - var result = Validator.isBirthDateValid().test(year + "-" + month + "-" + day); - // then - assertFalse(result); - } - - @Test - void shouldPassIfBirthDateIsInValidBecauseMonthIsInvalid() { - // given - var year = "2002"; - var month = "0"; - var day = "24"; - // when - var result = Validator.isBirthDateValid().test(year + "-" + month + "-" + day); - // then - assertFalse(result); - } - - @Test - void shouldPassIfBirthDateIsInValidBecauseDayIsInvalid() { - // given - var year = "2002"; - var month = "8"; - var day = "32"; - // when - var result = Validator.isBirthDateValid().test(year + "-" + month + "-" + day); - // then - assertFalse(result); - } - - @Test - void shouldPassIfPasswordIsValid() { - // given - var password = "!Password@123"; - // when - var result = Validator.isPasswordValid().test(password); - // then - assertTrue(result); - } - - @Test - void shouldPassIfPasswordIsInvalidBecauseNoSpecialCharacter() { - // given - var password = "Password123"; - // when - var result = Validator.isPasswordValid().test(password); - // then - assertFalse(result); - } - - @Test - void shouldPassIfPasswordIsInvalidBecauseNoUpperCaseLetter() { - // given - var password = "!password@123"; - // when - var result = Validator.isPasswordValid().test(password); - // then - assertFalse(result); - } - - @Test - void shouldPassIfPasswordIsInvalidBecauseNoNumber() { - // given - var password = "!Password@"; - // when - var result = Validator.isPasswordValid().test(password); - // then - assertFalse(result); - } - - @Test - void shouldPassIfBothValidationIsTrue() { - // given - var yes = "Y"; - // when - var result = Validator.isLetter().and(Validator.isYes()).test(yes); - // then - assertTrue(result); - } - - @Test - void shouldPassIfAtleastOneValidationIsTrue() { - // given - var yes = "K"; - // when - var result = Validator.isLetter().or(Validator.isNo()).test(yes); - // then - assertTrue(result); - } - - @Test - void shouldPassIfBothValidationIsFalse() { - // given - var yes = "KN"; - // when - var result = Validator.isLetter().or(Validator.isNo()).test(yes); - // then - assertFalse(result); - } -} \ No newline at end of file From f740d047f8603c0627a655bc3a1658906d71d0f8 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 9 Oct 2022 16:07:56 +0800 Subject: [PATCH 2/6] moved to sub package util --- .../io/github/pitzzahh/{ => util}/computing/Calculator.java | 5 +++-- .../io/github/pitzzahh/{ => util}/computing/Collatz.java | 4 ++-- .../io/github/pitzzahh/{ => util}/computing/ICalculator.java | 5 +++-- .../{ => util}/computing/algorithms/LinearSearch.java | 2 +- .../{ => util}/computing/algorithms/SearchingAlgorithm.java | 2 +- .../computing/algorithms/recursion/factorial/Factorial.java | 2 +- .../computing/algorithms/recursion/fibonacci/Fibonacci.java | 2 +- .../recursion/sumOfAllNumbers/SumOfAllNumbers.java | 2 +- .../{ => util}/computing/algorithms/sorting/QuickSort.java | 2 +- .../io/github/pitzzahh/{ => util}/utilities/FileUtil.java | 4 +--- .../java/io/github/pitzzahh/{ => util}/utilities/Print.java | 2 +- .../github/pitzzahh/{ => util}/utilities/SecurityUtil.java | 2 +- .../java/io/github/pitzzahh/{ => util}/utilities/Util.java | 2 +- .../github/pitzzahh/{ => util}/utilities/classes/Person.java | 5 +++-- .../pitzzahh/{ => util}/utilities/classes/TextColors.java | 2 +- .../{ => util}/utilities/classes/enums/Difficulty.java | 2 +- .../pitzzahh/{ => util}/utilities/classes/enums/Gender.java | 2 +- .../{ => util}/utilities/classes/enums/Operation.java | 2 +- .../{ => util}/utilities/classes/enums/Quadrant.java | 2 +- .../pitzzahh/{ => util}/utilities/classes/enums/Role.java | 2 +- .../pitzzahh/{ => util}/utilities/classes/enums/Status.java | 2 +- .../pitzzahh/{ => util}/utilities/validation/Validator.java | 5 +++-- 22 files changed, 31 insertions(+), 29 deletions(-) rename src/main/java/io/github/pitzzahh/{ => util}/computing/Calculator.java (97%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/Collatz.java (93%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/ICalculator.java (84%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/algorithms/LinearSearch.java (94%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/algorithms/SearchingAlgorithm.java (90%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/algorithms/recursion/factorial/Factorial.java (94%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/algorithms/recursion/fibonacci/Fibonacci.java (95%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbers.java (93%) rename src/main/java/io/github/pitzzahh/{ => util}/computing/algorithms/sorting/QuickSort.java (97%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/FileUtil.java (96%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/Print.java (96%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/SecurityUtil.java (99%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/Util.java (99%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/Person.java (98%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/TextColors.java (99%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/enums/Difficulty.java (80%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/enums/Gender.java (86%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/enums/Operation.java (94%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/enums/Quadrant.java (88%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/enums/Role.java (92%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/classes/enums/Status.java (84%) rename src/main/java/io/github/pitzzahh/{ => util}/utilities/validation/Validator.java (97%) diff --git a/src/main/java/io/github/pitzzahh/computing/Calculator.java b/src/main/java/io/github/pitzzahh/util/computing/Calculator.java similarity index 97% rename from src/main/java/io/github/pitzzahh/computing/Calculator.java rename to src/main/java/io/github/pitzzahh/util/computing/Calculator.java index 93114db..99e8a09 100644 --- a/src/main/java/io/github/pitzzahh/computing/Calculator.java +++ b/src/main/java/io/github/pitzzahh/util/computing/Calculator.java @@ -1,10 +1,11 @@ -package io.github.pitzzahh.computing; +package io.github.pitzzahh.util.computing; import java.util.Objects; import java.math.BigDecimal; import java.math.MathContext; import static java.math.BigDecimal.valueOf; -import io.github.pitzzahh.utilities.classes.enums.Operation; + +import io.github.pitzzahh.util.utilities.classes.enums.Operation; /** * Calculator class used to perform basic calculation. diff --git a/src/main/java/io/github/pitzzahh/computing/Collatz.java b/src/main/java/io/github/pitzzahh/util/computing/Collatz.java similarity index 93% rename from src/main/java/io/github/pitzzahh/computing/Collatz.java rename to src/main/java/io/github/pitzzahh/util/computing/Collatz.java index 6479ae2..83e686d 100644 --- a/src/main/java/io/github/pitzzahh/computing/Collatz.java +++ b/src/main/java/io/github/pitzzahh/util/computing/Collatz.java @@ -1,8 +1,8 @@ -package io.github.pitzzahh.computing; +package io.github.pitzzahh.util.computing; import java.util.stream.IntStream; import java.util.concurrent.TimeUnit; -import io.github.pitzzahh.utilities.validation.Validator; +import io.github.pitzzahh.util.utilities.validation.Validator; /** * Class used in performing collatz conjecture calculation. diff --git a/src/main/java/io/github/pitzzahh/computing/ICalculator.java b/src/main/java/io/github/pitzzahh/util/computing/ICalculator.java similarity index 84% rename from src/main/java/io/github/pitzzahh/computing/ICalculator.java rename to src/main/java/io/github/pitzzahh/util/computing/ICalculator.java index 1ec385a..020d491 100644 --- a/src/main/java/io/github/pitzzahh/computing/ICalculator.java +++ b/src/main/java/io/github/pitzzahh/util/computing/ICalculator.java @@ -1,6 +1,7 @@ -package io.github.pitzzahh.computing; +package io.github.pitzzahh.util.computing; + +import io.github.pitzzahh.util.utilities.classes.enums.Operation; -import io.github.pitzzahh.utilities.classes.enums.Operation; import java.math.BigDecimal; /** diff --git a/src/main/java/io/github/pitzzahh/computing/algorithms/LinearSearch.java b/src/main/java/io/github/pitzzahh/util/computing/algorithms/LinearSearch.java similarity index 94% rename from src/main/java/io/github/pitzzahh/computing/algorithms/LinearSearch.java rename to src/main/java/io/github/pitzzahh/util/computing/algorithms/LinearSearch.java index 733bdb7..0f844bd 100644 --- a/src/main/java/io/github/pitzzahh/computing/algorithms/LinearSearch.java +++ b/src/main/java/io/github/pitzzahh/util/computing/algorithms/LinearSearch.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.computing.algorithms; +package io.github.pitzzahh.util.computing.algorithms; import java.util.stream.IntStream; diff --git a/src/main/java/io/github/pitzzahh/computing/algorithms/SearchingAlgorithm.java b/src/main/java/io/github/pitzzahh/util/computing/algorithms/SearchingAlgorithm.java similarity index 90% rename from src/main/java/io/github/pitzzahh/computing/algorithms/SearchingAlgorithm.java rename to src/main/java/io/github/pitzzahh/util/computing/algorithms/SearchingAlgorithm.java index 99ad79b..ecec7b2 100644 --- a/src/main/java/io/github/pitzzahh/computing/algorithms/SearchingAlgorithm.java +++ b/src/main/java/io/github/pitzzahh/util/computing/algorithms/SearchingAlgorithm.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.computing.algorithms; +package io.github.pitzzahh.util.computing.algorithms; /** * Interface used by searching algorithms. diff --git a/src/main/java/io/github/pitzzahh/computing/algorithms/recursion/factorial/Factorial.java b/src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/factorial/Factorial.java similarity index 94% rename from src/main/java/io/github/pitzzahh/computing/algorithms/recursion/factorial/Factorial.java rename to src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/factorial/Factorial.java index bfa924a..f85d8f9 100644 --- a/src/main/java/io/github/pitzzahh/computing/algorithms/recursion/factorial/Factorial.java +++ b/src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/factorial/Factorial.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.computing.algorithms.recursion.factorial; +package io.github.pitzzahh.util.computing.algorithms.recursion.factorial; import java.text.NumberFormat; diff --git a/src/main/java/io/github/pitzzahh/computing/algorithms/recursion/fibonacci/Fibonacci.java b/src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/fibonacci/Fibonacci.java similarity index 95% rename from src/main/java/io/github/pitzzahh/computing/algorithms/recursion/fibonacci/Fibonacci.java rename to src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/fibonacci/Fibonacci.java index 017ac52..d4e82dd 100644 --- a/src/main/java/io/github/pitzzahh/computing/algorithms/recursion/fibonacci/Fibonacci.java +++ b/src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/fibonacci/Fibonacci.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.computing.algorithms.recursion.fibonacci; +package io.github.pitzzahh.util.computing.algorithms.recursion.fibonacci; import java.math.BigInteger; import java.text.NumberFormat; diff --git a/src/main/java/io/github/pitzzahh/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbers.java b/src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbers.java similarity index 93% rename from src/main/java/io/github/pitzzahh/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbers.java rename to src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbers.java index 87a2a44..9ab00af 100644 --- a/src/main/java/io/github/pitzzahh/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbers.java +++ b/src/main/java/io/github/pitzzahh/util/computing/algorithms/recursion/sumOfAllNumbers/SumOfAllNumbers.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.computing.algorithms.recursion.sumOfAllNumbers; +package io.github.pitzzahh.util.computing.algorithms.recursion.sumOfAllNumbers; import java.math.BigInteger; import java.util.stream.IntStream; diff --git a/src/main/java/io/github/pitzzahh/computing/algorithms/sorting/QuickSort.java b/src/main/java/io/github/pitzzahh/util/computing/algorithms/sorting/QuickSort.java similarity index 97% rename from src/main/java/io/github/pitzzahh/computing/algorithms/sorting/QuickSort.java rename to src/main/java/io/github/pitzzahh/util/computing/algorithms/sorting/QuickSort.java index df47903..2ed90c8 100644 --- a/src/main/java/io/github/pitzzahh/computing/algorithms/sorting/QuickSort.java +++ b/src/main/java/io/github/pitzzahh/util/computing/algorithms/sorting/QuickSort.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.computing.algorithms.sorting; +package io.github.pitzzahh.util.computing.algorithms.sorting; import java.util.Random; diff --git a/src/main/java/io/github/pitzzahh/utilities/FileUtil.java b/src/main/java/io/github/pitzzahh/util/utilities/FileUtil.java similarity index 96% rename from src/main/java/io/github/pitzzahh/utilities/FileUtil.java rename to src/main/java/io/github/pitzzahh/util/utilities/FileUtil.java index f9a68a0..21c609c 100644 --- a/src/main/java/io/github/pitzzahh/utilities/FileUtil.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/FileUtil.java @@ -1,14 +1,12 @@ -package io.github.pitzzahh.utilities; +package io.github.pitzzahh.util.utilities; import java.io.*; import java.util.Arrays; -import java.util.Collection; import java.util.List; import java.util.Objects; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Collectors; -import java.util.stream.IntStream; /** * Class used to some file operations. diff --git a/src/main/java/io/github/pitzzahh/utilities/Print.java b/src/main/java/io/github/pitzzahh/util/utilities/Print.java similarity index 96% rename from src/main/java/io/github/pitzzahh/utilities/Print.java rename to src/main/java/io/github/pitzzahh/util/utilities/Print.java index 514286c..303fd89 100644 --- a/src/main/java/io/github/pitzzahh/utilities/Print.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/Print.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities; +package io.github.pitzzahh.util.utilities; /** * Class used to shorten printing functions. diff --git a/src/main/java/io/github/pitzzahh/utilities/SecurityUtil.java b/src/main/java/io/github/pitzzahh/util/utilities/SecurityUtil.java similarity index 99% rename from src/main/java/io/github/pitzzahh/utilities/SecurityUtil.java rename to src/main/java/io/github/pitzzahh/util/utilities/SecurityUtil.java index 22c4a49..4eb4baa 100644 --- a/src/main/java/io/github/pitzzahh/utilities/SecurityUtil.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/SecurityUtil.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities; +package io.github.pitzzahh.util.utilities; import java.io.*; import java.util.*; diff --git a/src/main/java/io/github/pitzzahh/utilities/Util.java b/src/main/java/io/github/pitzzahh/util/utilities/Util.java similarity index 99% rename from src/main/java/io/github/pitzzahh/utilities/Util.java rename to src/main/java/io/github/pitzzahh/util/utilities/Util.java index 70c4f1e..7cdb52a 100644 --- a/src/main/java/io/github/pitzzahh/utilities/Util.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/Util.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities; +package io.github.pitzzahh.util.utilities; import java.util.List; import java.util.ArrayList; diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/Person.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/Person.java similarity index 98% rename from src/main/java/io/github/pitzzahh/utilities/classes/Person.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/Person.java index ea670f2..50cefb7 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/Person.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/Person.java @@ -1,8 +1,9 @@ -package io.github.pitzzahh.utilities.classes; +package io.github.pitzzahh.util.utilities.classes; import java.time.Period; import java.time.LocalDate; -import io.github.pitzzahh.utilities.classes.enums.Gender; + +import io.github.pitzzahh.util.utilities.classes.enums.Gender; /** * Template class used to create a person object. diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/TextColors.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/TextColors.java similarity index 99% rename from src/main/java/io/github/pitzzahh/utilities/classes/TextColors.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/TextColors.java index 9f718be..8785296 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/TextColors.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/TextColors.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities.classes; +package io.github.pitzzahh.util.utilities.classes; /** * Class used for coloring texts. diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Difficulty.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Difficulty.java similarity index 80% rename from src/main/java/io/github/pitzzahh/utilities/classes/enums/Difficulty.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Difficulty.java index e8b9b67..cc278e7 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Difficulty.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Difficulty.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities.classes.enums; +package io.github.pitzzahh.util.utilities.classes.enums; /** * Enum for difficult situations. diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Gender.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Gender.java similarity index 86% rename from src/main/java/io/github/pitzzahh/utilities/classes/enums/Gender.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Gender.java index 4192ddf..616dd77 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Gender.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Gender.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities.classes.enums; +package io.github.pitzzahh.util.utilities.classes.enums; /** * enum template for genders. diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Operation.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Operation.java similarity index 94% rename from src/main/java/io/github/pitzzahh/utilities/classes/enums/Operation.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Operation.java index 11f5c34..9734314 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Operation.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Operation.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities.classes.enums; +package io.github.pitzzahh.util.utilities.classes.enums; /** * Enum of operations. diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Quadrant.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Quadrant.java similarity index 88% rename from src/main/java/io/github/pitzzahh/utilities/classes/enums/Quadrant.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Quadrant.java index 76d50ab..312245a 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Quadrant.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Quadrant.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities.classes.enums; +package io.github.pitzzahh.util.utilities.classes.enums; /** * Used in representing which quadrant the coordinate belongs. diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Role.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Role.java similarity index 92% rename from src/main/java/io/github/pitzzahh/utilities/classes/enums/Role.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Role.java index 51c9220..b859855 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Role.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Role.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities.classes.enums; +package io.github.pitzzahh.util.utilities.classes.enums; /** * enum that can be used for making roles. diff --git a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Status.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Status.java similarity index 84% rename from src/main/java/io/github/pitzzahh/utilities/classes/enums/Status.java rename to src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Status.java index c01a623..30460dd 100644 --- a/src/main/java/io/github/pitzzahh/utilities/classes/enums/Status.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/enums/Status.java @@ -1,4 +1,4 @@ -package io.github.pitzzahh.utilities.classes.enums; +package io.github.pitzzahh.util.utilities.classes.enums; /** * enum used in processes. diff --git a/src/main/java/io/github/pitzzahh/utilities/validation/Validator.java b/src/main/java/io/github/pitzzahh/util/utilities/validation/Validator.java similarity index 97% rename from src/main/java/io/github/pitzzahh/utilities/validation/Validator.java rename to src/main/java/io/github/pitzzahh/util/utilities/validation/Validator.java index 3cec1c5..55999dc 100644 --- a/src/main/java/io/github/pitzzahh/utilities/validation/Validator.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/validation/Validator.java @@ -1,9 +1,10 @@ -package io.github.pitzzahh.utilities.validation; +package io.github.pitzzahh.util.utilities.validation; import java.util.Arrays; import java.util.regex.Pattern; import java.util.function.Predicate; -import io.github.pitzzahh.utilities.classes.enums.Gender; + +import io.github.pitzzahh.util.utilities.classes.enums.Gender; /** * Interface used for various input validation. From 36287575b50da7abfa80052fa08395da4210833e Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 9 Oct 2022 16:08:14 +0800 Subject: [PATCH 3/6] added DynamicArray class --- .../util/utilities/classes/DynamicArray.java | 243 ++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java diff --git a/src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java new file mode 100644 index 0000000..e198781 --- /dev/null +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java @@ -0,0 +1,243 @@ +package io.github.pitzzahh.util.utilities.classes; + +import static java.util.Objects.requireNonNull; +import static java.util.stream.IntStream.range; +import java.util.stream.StreamSupport; +import java.util.function.Predicate; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import static java.util.Arrays.*; +import java.util.stream.Stream; +import java.io.Serializable; +import java.util.*; + +/** + * Class that functions same as an array but the size increases. + * @param the type of the dynamic array. + */ +public final class DynamicArray implements Serializable { + + /** + * The array where the elements are stored. + * Called balloon because balloon expands as you add air to it. + */ + transient Object[] balloon; + + /** + * placeholder for any insert modification in the dynamic array. + */ + transient int modificationCount; + + /** + * No args dynamic array. Sets the dynamic array size to default size of 10. + */ + public DynamicArray() { + this.balloon = new Object[] {}; + } + + /** + * Creates a {@code DynamicArray} object with an initial size + * @param initialSize the initial size of the array. + */ + public DynamicArray(int initialSize) { + if (initialSize > 0) this.balloon = new Object[initialSize]; + else if (initialSize == 0) this.balloon = new Object[]{}; + else throw new IllegalArgumentException("Illegal Capacity: " + initialSize); + } + + /** + * Insert an element in the dynamic array. + * @param element the element to be inserted. + * @return {@code true} if inserted successfully. + */ + public boolean insert(T element) { + modificationCount++; + return this.add(element); + } + + /** + * Inserts an array of elements in the dynamic array. + * @param elements the element to be inserted. + * @return {@code true} if inserted successfully. + */ + @SafeVarargs + public final boolean insert(T... elements) { + modificationCount += elements.length; + Arrays.stream(elements).forEachOrdered(this::add); + return size() == elements.length; + } + + /** + * Removes an element in the dynamic array. + * @param element the element to be removed. + * @return {@code true} if removed successfully. + */ + public boolean remove(T element) { + requireNonNull(element, "Argument cannot be null"); + var index = indexOf(element); + if (index == -1) return false; + this.balloon[index] = null; + return trimAndGrow(); + } + + /** + * Removes an element in the dynamic array and returns it. + * @param element the element to be removed. + * @return {@code T} the removed element. + */ + @SuppressWarnings("unchecked") + public T removeAndGet(T element) { + requireNonNull(element, "Argument cannot be null"); + final var index = indexOf(element); + var toBeRemoved = (T) new Object(); + if (index != -1) toBeRemoved = get(index); + this.balloon[index] = null; + trimAndGrow(); + return toBeRemoved; + } + + /** + * Removes an element in a specific index. + * @param index the index of the element to be removed. + * @return {@code true} if the element has been removed. + */ + public boolean remove(int index) { + checkBounds(index); + this.balloon[index] = null; + return trimAndGrow(); + } + + /** + * Removes all the elements in the dynamicArray. + */ + public void removeAll() { + this.balloon = new Object[] {}; + } + + /** + * Returns the index of the element in the dynamic array. + * @param element the element to search for the index. + * @return the index of the element in the dynamic array. If index is not present, returns -1. + */ + public int indexOf(T element) { + requireNonNull(element, "Argument cannot be null"); + return getIndex(element); + } + + /** + * Get the element at a specific index. + * @param index the index of the element. + * @return the element {@link T} in the dynamic array. + */ + @SuppressWarnings("unchecked") + public T get(int index) { + checkBounds(index); + return (T) this.balloon[index]; + } + + /** + * Get the size of the dynamic array. + * @return {@code int} contains the size of the dynamic array. + */ + public int size() { + return this.balloon.length; + } + + /** + * Sorts the dynamic array. + * @param ascending if sorting is ascending, otherwise descending. + */ + public void sort(boolean ascending) { + removeNulls(); + checkTypes(); + if (!ascending) this.balloon = this.stream() + .sorted(Collections.reverseOrder()) + .toArray(); + else this.balloon = this.stream() + .sorted() + .toArray(); + } + + /** + * Puts the dynamic array to abstraction. + * @return a {@code Stream} of objects. + * @see Stream + * @see Spliterators + * @see StreamSupport + */ + public Stream stream() { + return StreamSupport.stream( + Spliterators.spliterator( + this.balloon, + 0, + size(), + Spliterator.ORDERED | Spliterator.IMMUTABLE + ), false + ); + } + + // Internal implementation that checks if the dynamic array contains different types. + private void checkTypes() throws IllegalStateException { + var differentTypes = Arrays.stream(this.balloon) + .anyMatch(e -> !e.getClass().equals(this.balloon[0].getClass())); // mutability, for removal or change. + if (differentTypes) throw new IllegalStateException("Cannot sort different types in a dynamic array"); + } + + // Internal implementation of getting the index of an element in the dynamic array. + private int getIndex(T element) { + return range(0, size()) + .filter(i -> this.balloon[i] != null) + .filter(i -> this.balloon[i].equals(element)) + .findAny() + .orElse(-1); + } + + // Internal implementation of adding element in the dynamic array. + private boolean add(T element) { + requireNonNull(element, "Argument cannot be null"); + var size = getCurrentSize.get(); + if (isFull.test(size)) trimAndGrow(); + balloon[size] = element; + return size == indexOf(element); + } + + // Internal implementation that trims the dynamic array and grow the length by 10. + private boolean trimAndGrow() { + var size = getCurrentSize.get(); + removeNulls(); + this.balloon = copyOf(this.stream().toArray(), size + modificationCount); + modificationCount = 0; + return size() == size; + } + + // Internal implementation that removes null from the dynamic array. + private void removeNulls() { + this.balloon = this.stream() + .filter(Objects::nonNull) + .toArray(); + } + + // Internal utility method, checks if index is out of bound. + private void checkBounds(int index) { + if (isOutOfBounds.apply(index)) throw new IndexOutOfBoundsException("Index out of bounds: " + index); + } + + // Internal implementation that checks filters non-null objects and count them. + private final Supplier getCurrentSize = () -> (int) this.stream() + .filter(Objects::nonNull) + .count(); + + // Internal implementation that checks if the dynamic array is full. + private final Predicate isFull = currentElements -> currentElements >= size(); + + // Internal implementation that checks if the index is out of bounds. + private final Function isOutOfBounds = index -> index > getCurrentSize.get() || index < 0; + + @Override + public String toString() { + return this.stream() + .map(String::valueOf) + .collect(Collectors.joining(", ", "[", "]")); + } +} From be0d308eaaada284a7c8ff4b357bb52641a157f5 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 9 Oct 2022 16:08:45 +0800 Subject: [PATCH 4/6] update to latest version 1.0.10 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index cb0615c..a3115d5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.pitzzahh util-classes - 1.0.9 + 1.0.10 jar From 1ed1331194a74dfa6d59854c8e26f791ea524858 Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 9 Oct 2022 16:14:17 +0800 Subject: [PATCH 5/6] added comments --- .../util/utilities/classes/DynamicArray.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java b/src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java index e198781..81e23a8 100644 --- a/src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java +++ b/src/main/java/io/github/pitzzahh/util/utilities/classes/DynamicArray.java @@ -177,14 +177,21 @@ public Stream stream() { ); } - // Internal implementation that checks if the dynamic array contains different types. + /** + * Internal implementation that checks if the dynamic array contains different types. + * @throws IllegalStateException if the data types in the dynamic array are not all the same. + */ private void checkTypes() throws IllegalStateException { var differentTypes = Arrays.stream(this.balloon) .anyMatch(e -> !e.getClass().equals(this.balloon[0].getClass())); // mutability, for removal or change. if (differentTypes) throw new IllegalStateException("Cannot sort different types in a dynamic array"); } - // Internal implementation of getting the index of an element in the dynamic array. + /** + * Internal implementation of getting the index of an element in the dynamic array. + * @param element the element to search for the index. + * @return the index of the element in the dynamic array. If index is not present, returns -1. + */ private int getIndex(T element) { return range(0, size()) .filter(i -> this.balloon[i] != null) @@ -193,7 +200,11 @@ private int getIndex(T element) { .orElse(-1); } - // Internal implementation of adding element in the dynamic array. + /** + * Internal implementation of adding element in the dynamic array. + * @param element the element to be added. + * @return {@code true} if the element index is same as the index where it should be. + */ private boolean add(T element) { requireNonNull(element, "Argument cannot be null"); var size = getCurrentSize.get(); @@ -202,7 +213,10 @@ private boolean add(T element) { return size == indexOf(element); } - // Internal implementation that trims the dynamic array and grow the length by 10. + /** + * Internal implementation that trims the dynamic array and grow the length by 10. + * @return {@code true} if the dynamic array has been trimmed and grown. + */ private boolean trimAndGrow() { var size = getCurrentSize.get(); removeNulls(); @@ -211,27 +225,38 @@ private boolean trimAndGrow() { return size() == size; } - // Internal implementation that removes null from the dynamic array. + /** + * Internal implementation that removes null from the dynamic array. + */ private void removeNulls() { this.balloon = this.stream() .filter(Objects::nonNull) .toArray(); } - // Internal utility method, checks if index is out of bound. + /** + * Internal utility method, checks if index is out of bound. + * @param index the index to be checked. + */ private void checkBounds(int index) { if (isOutOfBounds.apply(index)) throw new IndexOutOfBoundsException("Index out of bounds: " + index); } - // Internal implementation that checks filters non-null objects and count them. + /** + * Internal implementation that checks filters non-null objects and count them. + */ private final Supplier getCurrentSize = () -> (int) this.stream() .filter(Objects::nonNull) .count(); - // Internal implementation that checks if the dynamic array is full. + /** + * Internal implementation that checks if the dynamic array is full. + */ private final Predicate isFull = currentElements -> currentElements >= size(); - // Internal implementation that checks if the index is out of bounds. + /** + * Internal implementation that checks if the index is out of bounds. + */ private final Function isOutOfBounds = index -> index > getCurrentSize.get() || index < 0; @Override From 9acafedf14a3c9265c182169c439c19a8dc92a7f Mon Sep 17 00:00:00 2001 From: pitzzahh Date: Sun, 9 Oct 2022 16:24:59 +0800 Subject: [PATCH 6/6] added tests info --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b325b45..e7039df 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,5 @@ Be sure to replace the **VERSION** key below with the one of the versions shown ``` +---- +### Tests for this repository can be found here: [util-classes-tests](https://github.com/pitzzahh/util-classes-tests)