From e5697e9083c8faef9324b05e7ed54a8130ace691 Mon Sep 17 00:00:00 2001 From: rotgruengelb Date: Sun, 28 Jan 2024 23:58:07 +0100 Subject: [PATCH] 1.0.0 --- gradle.properties | 2 +- .../rotgruengelb/nixienaut/ClampedNum.java | 88 ------- .../net/rotgruengelb/nixienaut/Dummy.java | 19 +- .../exeption/ImpossibleStateException.java | 18 ++ .../exeption/NotImplementedException.java | 3 +- .../nixienaut/list/QuickArrayList.java | 3 +- .../nixienaut/math/ClampedNum.java | 116 ++++++++++ .../nixienaut/{ => math}/RGB.java | 22 +- src/test/java/tests/ClampedNumTests.java | 100 ++++---- src/test/java/tests/RGBTests.java | 217 +++++++++--------- 10 files changed, 317 insertions(+), 271 deletions(-) delete mode 100644 src/main/java/net/rotgruengelb/nixienaut/ClampedNum.java create mode 100644 src/main/java/net/rotgruengelb/nixienaut/exeption/ImpossibleStateException.java create mode 100644 src/main/java/net/rotgruengelb/nixienaut/math/ClampedNum.java rename src/main/java/net/rotgruengelb/nixienaut/{ => math}/RGB.java (92%) diff --git a/gradle.properties b/gradle.properties index 6204417..aef125e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.3.0 +version=1.0.0 diff --git a/src/main/java/net/rotgruengelb/nixienaut/ClampedNum.java b/src/main/java/net/rotgruengelb/nixienaut/ClampedNum.java deleted file mode 100644 index 6738fde..0000000 --- a/src/main/java/net/rotgruengelb/nixienaut/ClampedNum.java +++ /dev/null @@ -1,88 +0,0 @@ -package net.rotgruengelb.nixienaut; - -/** - * A number that is clamped between a minimum and a maximum value. - * - * @param The type of the number. - */ -public class ClampedNum> { - private final T minValue; - private final T maxValue; - private T value; - - /** - * Creates a new ClampedNum object. - * - * @param minValue The minimum value. - * @param maxValue The maximum value. - * @see ClampedNum#setValue(T) - * @see ClampedNum#adjustAndSetValue(T) - */ - public ClampedNum(T minValue, T maxValue) { - this.minValue = minValue; - this.maxValue = maxValue; - } - - /** - * Gets the minimum value. - * - * @return The minimum value. - */ - public T getValue() { - return value; - } - - /** - * Sets the value. - * - * @param value The value to set. - * @throws IllegalArgumentException If the value is out of range. - * @see ClampedNum#adjustAndSetValue(T) - * @see ClampedNum#getValue() - */ - public void setValue(T value) throws IllegalArgumentException { - if (value.compareTo(minValue) < 0 || value.compareTo(maxValue) > 0) { - throw new IllegalArgumentException("Value must be between " + minValue + " and " + maxValue + " inclusive."); - } - this.value = value; - } - - - /** - * Sets the value and clamps it if it is out of range. - * - * @param value The value to set. - * @see ClampedNum#setValue(T) - * @see ClampedNum#getValue() - */ - public void adjustAndSetValue(T value) { - if (value.compareTo(minValue) < 0) { - this.value = minValue; - } else if (value.compareTo(maxValue) > 0) { - this.value = maxValue; - } else { - this.value = value; - } - } - - - /** - * Gets the minimum value. - * - * @return The minimum value. - * @see ClampedNum#getMaxValue() - */ - public T getMinValue() { - return minValue; - } - - /** - * Gets the maximum value. - * - * @return The maximum value. - * @see ClampedNum#getMinValue() - */ - public T getMaxValue() { - return maxValue; - } -} \ No newline at end of file diff --git a/src/main/java/net/rotgruengelb/nixienaut/Dummy.java b/src/main/java/net/rotgruengelb/nixienaut/Dummy.java index 6691356..12c9a0a 100644 --- a/src/main/java/net/rotgruengelb/nixienaut/Dummy.java +++ b/src/main/java/net/rotgruengelb/nixienaut/Dummy.java @@ -1,7 +1,5 @@ package net.rotgruengelb.nixienaut; -import net.rotgruengelb.nixienaut.annotation.PlaceholderValue; - /** * This is a dummy class. * @@ -9,13 +7,12 @@ */ public class Dummy { - /** - * This is a dummy method. - * It does nothing. - * - * @see Dummy - */ - @PlaceholderValue("TODO") - public static void _void() { - } + /** + * This is a dummy method. + * It does nothing. + * + * @see Dummy + */ + public static void _void() { + } } diff --git a/src/main/java/net/rotgruengelb/nixienaut/exeption/ImpossibleStateException.java b/src/main/java/net/rotgruengelb/nixienaut/exeption/ImpossibleStateException.java new file mode 100644 index 0000000..328d753 --- /dev/null +++ b/src/main/java/net/rotgruengelb/nixienaut/exeption/ImpossibleStateException.java @@ -0,0 +1,18 @@ +package net.rotgruengelb.nixienaut.exeption; + +public class ImpossibleStateException extends RuntimeException { + + /** + * This exception is thrown when a state produced in program is impossible. + * + * @see ImpossibleStateException#ImpossibleStateException(String message) + */ + public ImpossibleStateException() { super("The state produced is Impossible! This should never happen! Please report this bug!"); } + + /** + * This exception is thrown when a state produced in program is impossible. + * + * @param message The message to be displayed + */ + public ImpossibleStateException(String message) { super(message); } +} diff --git a/src/main/java/net/rotgruengelb/nixienaut/exeption/NotImplementedException.java b/src/main/java/net/rotgruengelb/nixienaut/exeption/NotImplementedException.java index 0bdc6f2..d6bc8ec 100644 --- a/src/main/java/net/rotgruengelb/nixienaut/exeption/NotImplementedException.java +++ b/src/main/java/net/rotgruengelb/nixienaut/exeption/NotImplementedException.java @@ -1,6 +1,5 @@ package net.rotgruengelb.nixienaut.exeption; - /** * This exception is thrown when a method is called that is not implemented yet. */ @@ -8,12 +7,14 @@ public class NotImplementedException extends RuntimeException { /** * This exception is thrown when a method is called that is not implemented yet. + * * @see NotImplementedException#NotImplementedException(String message) */ public NotImplementedException() { super("Not implemented yet!"); } /** * This exception is thrown when a method is called that is not implemented yet. + * * @param message The message to be displayed */ public NotImplementedException(String message) { super(message); } diff --git a/src/main/java/net/rotgruengelb/nixienaut/list/QuickArrayList.java b/src/main/java/net/rotgruengelb/nixienaut/list/QuickArrayList.java index b205fd0..d13946e 100644 --- a/src/main/java/net/rotgruengelb/nixienaut/list/QuickArrayList.java +++ b/src/main/java/net/rotgruengelb/nixienaut/list/QuickArrayList.java @@ -2,11 +2,11 @@ import java.util.ArrayList; - /** * This class is just a wrapper for ArrayList to make it easier to
* add elements to the list in a chain. * and then return the list itself. + * * @see QuickArrayList#qAdd(E e) */ public class QuickArrayList extends ArrayList { @@ -15,6 +15,7 @@ public class QuickArrayList extends ArrayList { * This class is just a wrapper for ArrayList to make it easier to
* add elements to the list in a chain. * and then return the list itself. + * * @param e The type of the elements in the list * @return The list itself * @see ArrayList#add(Object) diff --git a/src/main/java/net/rotgruengelb/nixienaut/math/ClampedNum.java b/src/main/java/net/rotgruengelb/nixienaut/math/ClampedNum.java new file mode 100644 index 0000000..99a0da2 --- /dev/null +++ b/src/main/java/net/rotgruengelb/nixienaut/math/ClampedNum.java @@ -0,0 +1,116 @@ +package net.rotgruengelb.nixienaut.math; + +/** + * A number that is clamped between a minimum and a maximum value. + * + * @param The type of the number. + */ +public class ClampedNum> { + private final T minValue; + private final T maxValue; + private T value; + + /** + * Creates a new ClampedNum object. + * + * @param minValue The minimum value. + * @param maxValue The maximum value. + * @see ClampedNum#set(T) + * @see ClampedNum#adjustSet(T) + */ + public ClampedNum(T minValue, T maxValue) { + this.minValue = minValue; + this.maxValue = maxValue; + } + + /** + * Clamps an integer value between a minimum and a maximum value. + * + * @param value The value to clamp. + * @param minValue The minimum value. + * @param maxValue The maximum value. + * @return The clamped value. + */ + public static int clamp(int value, int minValue, int maxValue) { + if (value < 0) { + return minValue; + } else if (value > 0) { + return maxValue; + } else { + return value; + } + } + + /** + * Clamps a float value between a minimum and a maximum value. + * + * @param value The value to clamp. + * @param minValue The minimum value. + * @param maxValue The maximum value. + * @return The clamped value. + */ + public static float clamp(float value, float minValue, float maxValue) { + if (value < 0) { + return minValue; + } else if (value > 0) { + return maxValue; + } else { + return value; + } + } + + /** + * Gets the minimum value. + * + * @return The minimum value. + */ + public T get() { return value; } + + /** + * Sets the value. + * + * @param value The value to set. + * @throws IllegalArgumentException If the value is out of range. + * @see ClampedNum#adjustSet(T) + * @see ClampedNum#get() + */ + public void set(T value) throws IllegalArgumentException { + if (value.compareTo(minValue) < 0 || value.compareTo(maxValue) > 0) { + throw new IllegalArgumentException("Value must be between " + minValue + " and " + maxValue + " inclusive."); + } + this.value = value; + } + + /** + * Sets the value and clamps it if it is out of range. + * + * @param value The value to set. + * @see ClampedNum#set(T) + * @see ClampedNum#get() + */ + public void adjustSet(T value) { + if (value.compareTo(minValue) < 0) { + this.value = minValue; + } else if (value.compareTo(maxValue) > 0) { + this.value = maxValue; + } else { + this.value = value; + } + } + + /** + * Gets the minimum value. + * + * @return The minimum value. + * @see ClampedNum#getMaxValue() + */ + public T getMinValue() { return minValue; } + + /** + * Gets the maximum value. + * + * @return The maximum value. + * @see ClampedNum#getMinValue() + */ + public T getMaxValue() { return maxValue; } +} \ No newline at end of file diff --git a/src/main/java/net/rotgruengelb/nixienaut/RGB.java b/src/main/java/net/rotgruengelb/nixienaut/math/RGB.java similarity index 92% rename from src/main/java/net/rotgruengelb/nixienaut/RGB.java rename to src/main/java/net/rotgruengelb/nixienaut/math/RGB.java index 3aecb17..a2093ae 100644 --- a/src/main/java/net/rotgruengelb/nixienaut/RGB.java +++ b/src/main/java/net/rotgruengelb/nixienaut/math/RGB.java @@ -1,4 +1,6 @@ -package net.rotgruengelb.nixienaut; +package net.rotgruengelb.nixienaut.math; + +import net.rotgruengelb.nixienaut.Dummy; /** * This class represents an RGB color.
@@ -27,9 +29,9 @@ public class RGB { * @throws IllegalArgumentException If any of the values are out of range. */ public RGB(int r, int g, int b) { - this.r.setValue(r); - this.g.setValue(g); - this.b.setValue(b); + this.r.set(r); + this.g.set(g); + this.b.set(b); } /** @@ -120,7 +122,7 @@ public int[] toArray() { * @see RGB#r(int) */ public int r() { - return this.r.getValue(); + return this.r.get(); } /** @@ -130,7 +132,7 @@ public int r() { * @see RGB#g(int) */ public int g() { - return this.g.getValue(); + return this.g.get(); } /** @@ -140,7 +142,7 @@ public int g() { * @see RGB#b(int) */ public int b() { - return this.b.getValue(); + return this.b.get(); } /** @@ -150,7 +152,7 @@ public int b() { * @see RGB#r() */ public void r(int value) { - this.r.setValue(value); + this.r.set(value); } /** @@ -160,7 +162,7 @@ public void r(int value) { * @see RGB#g() */ public void g(int value) { - this.g.setValue(value); + this.g.set(value); } /** @@ -170,7 +172,7 @@ public void g(int value) { * @see RGB#b() */ public void b(int value) { - this.b.setValue(value); + this.b.set(value); } } \ No newline at end of file diff --git a/src/test/java/tests/ClampedNumTests.java b/src/test/java/tests/ClampedNumTests.java index 9616895..71b869d 100644 --- a/src/test/java/tests/ClampedNumTests.java +++ b/src/test/java/tests/ClampedNumTests.java @@ -1,65 +1,65 @@ package tests; -import net.rotgruengelb.nixienaut.ClampedNum; +import net.rotgruengelb.nixienaut.math.ClampedNum; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ClampedNumTests { - @Test - public void testValidInitialization() { - ClampedNum clampedNum = new ClampedNum<>(0, 100); - assertNull(clampedNum.getValue()); - } + @Test + public void testValidInitialization() { + ClampedNum clampedNum = new ClampedNum<>(0, 100); + assertNull(clampedNum.get()); + } - @Test - public void testValidSetValue() { - ClampedNum clampedNum = new ClampedNum<>(0, 100); - clampedNum.setValue(50); - assertEquals(Integer.valueOf(50), clampedNum.getValue()); - } + @Test + public void testValidSetValue() { + ClampedNum clampedNum = new ClampedNum<>(0, 100); + clampedNum.set(50); + assertEquals(Integer.valueOf(50), clampedNum.get()); + } - @Test - public void testSetValueBelowMin() { - ClampedNum clampedNum = new ClampedNum<>(0, 100); - try { - clampedNum.setValue(-1); - fail("IllegalArgumentException should be thrown"); - } catch (IllegalArgumentException e) { - assertEquals("Value must be between 0 and 100 inclusive.", e.getMessage()); - } - } + @Test + public void testSetValueBelowMin() { + ClampedNum clampedNum = new ClampedNum<>(0, 100); + try { + clampedNum.set(-1); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + assertEquals("Value must be between 0 and 100 inclusive.", e.getMessage()); + } + } - @Test - public void testSetValueAboveMax() { - ClampedNum clampedNum = new ClampedNum<>(0, 100); - try { - clampedNum.setValue(101); - fail("IllegalArgumentException should be thrown"); - } catch (IllegalArgumentException e) { - assertEquals("Value must be between 0 and 100 inclusive.", e.getMessage()); - } - } + @Test + public void testSetValueAboveMax() { + ClampedNum clampedNum = new ClampedNum<>(0, 100); + try { + clampedNum.set(101); + fail("IllegalArgumentException should be thrown"); + } catch (IllegalArgumentException e) { + assertEquals("Value must be between 0 and 100 inclusive.", e.getMessage()); + } + } - @Test - public void testSetValueEqualMin() { - ClampedNum clampedNum = new ClampedNum<>(0, 100); - clampedNum.setValue(0); - assertEquals(Integer.valueOf(0), clampedNum.getValue()); - } + @Test + public void testSetValueEqualMin() { + ClampedNum clampedNum = new ClampedNum<>(0, 100); + clampedNum.set(0); + assertEquals(Integer.valueOf(0), clampedNum.get()); + } - @Test - public void testSetValueEqualMax() { - ClampedNum clampedNum = new ClampedNum<>(0, 100); - clampedNum.setValue(100); - assertEquals(Integer.valueOf(100), clampedNum.getValue()); - } + @Test + public void testSetValueEqualMax() { + ClampedNum clampedNum = new ClampedNum<>(0, 100); + clampedNum.set(100); + assertEquals(Integer.valueOf(100), clampedNum.get()); + } - @Test - public void testSetValueWithinRange() { - ClampedNum clampedNum = new ClampedNum<>(0, 100); - clampedNum.setValue(50); - assertEquals(Integer.valueOf(50), clampedNum.getValue()); - } + @Test + public void testSetValueWithinRange() { + ClampedNum clampedNum = new ClampedNum<>(0, 100); + clampedNum.set(50); + assertEquals(Integer.valueOf(50), clampedNum.get()); + } } \ No newline at end of file diff --git a/src/test/java/tests/RGBTests.java b/src/test/java/tests/RGBTests.java index 6b27c2e..b42ec8f 100644 --- a/src/test/java/tests/RGBTests.java +++ b/src/test/java/tests/RGBTests.java @@ -1,6 +1,6 @@ package tests; -import net.rotgruengelb.nixienaut.RGB; +import net.rotgruengelb.nixienaut.math.RGB; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -8,113 +8,112 @@ class RGBTests { - @Test - void testConstructorAndGetters() { - RGB color = new RGB(255, 128, 0); - - assertEquals(255, color.r()); - assertEquals(128, color.g()); - assertEquals(0, color.b()); - } - - @Test - void testFromInt() { - RGB color = RGB.fromInt(0xFFAABB); - - assertEquals(0xFF, color.r()); - assertEquals(0xAA, color.g()); - assertEquals(0xBB, color.b()); - } - - @Test - void testFromHex() { - RGB color1 = new RGB(0, 255, 0); - RGB color2 = RGB.fromHex("#00FF60"); - - assertEquals(0, color2.r()); - assertEquals(255, color2.g()); - assertEquals(96, color2.b()); - - assertEquals(0, color1.r()); - assertEquals(255, color1.g()); - assertEquals(0, color1.b()); - } - - @Test - void testToInt() { - RGB color = new RGB(128, 64, 32); - - assertEquals(0x804020, color.toInt()); - } - - @Test - void testToString() { - RGB color = new RGB(255, 0, 128); - - assertEquals("RGB(255, 0, 128)", color.toString()); - } - - @Test - void testToHex() { - RGB color = new RGB(0, 128, 255); - - assertEquals("#0080FF", color.toHex()); - } - - @Test - void testToArray() { - RGB color = new RGB(64, 128, 192); - - int[] expectedArray = {64, 128, 192}; - assertArrayEquals(expectedArray, color.toArray()); - } - - @Test - void testSetters() { - RGB color = new RGB(0, 0, 0); - - color.r(255); - color.g(128); - color.b(64); - - assertEquals(255, color.r()); - assertEquals(128, color.g()); - assertEquals(64, color.b()); - } - - @Test - void testInstancesAreIndependent() { - RGB color1 = new RGB(0, 0, 0); - RGB color2 = new RGB(255, 255, 255); - - color1.r(128); - color1.g(64); - color1.b(32); - - assertEquals(128, color1.r()); - assertEquals(64, color1.g()); - assertEquals(32, color1.b()); - - assertEquals(255, color2.r()); - assertEquals(255, color2.g()); - assertEquals(255, color2.b()); - } - - @Test - void testFromHexCreatesIndependentInstances() { - RGB color1 = RGB.fromHex("#00FF00"); - RGB color2 = RGB.fromHex("#0000FF"); - - color1.r(128); - color1.b(32); - - assertEquals(128, color1.r()); - assertEquals(255, color1.g()); - assertEquals(32, color1.b()); - - assertEquals(0, color2.r()); - assertEquals(0, color2.g()); - assertEquals(255, color2.b()); - } + @Test + void testConstructorAndGetters() { + RGB color = new RGB(255, 128, 0); + assertEquals(255, color.r()); + assertEquals(128, color.g()); + assertEquals(0, color.b()); + } + + @Test + void testFromInt() { + RGB color = RGB.fromInt(0xFFAABB); + + assertEquals(0xFF, color.r()); + assertEquals(0xAA, color.g()); + assertEquals(0xBB, color.b()); + } + + @Test + void testFromHex() { + RGB color1 = new RGB(0, 255, 0); + RGB color2 = RGB.fromHex("#00FF60"); + + assertEquals(0, color2.r()); + assertEquals(255, color2.g()); + assertEquals(96, color2.b()); + + assertEquals(0, color1.r()); + assertEquals(255, color1.g()); + assertEquals(0, color1.b()); + } + + @Test + void testToInt() { + RGB color = new RGB(128, 64, 32); + + assertEquals(0x804020, color.toInt()); + } + + @Test + void testToString() { + RGB color = new RGB(255, 0, 128); + + assertEquals("RGB(255, 0, 128)", color.toString()); + } + + @Test + void testToHex() { + RGB color = new RGB(0, 128, 255); + + assertEquals("#0080FF", color.toHex()); + } + + @Test + void testToArray() { + RGB color = new RGB(64, 128, 192); + + int[] expectedArray = {64, 128, 192}; + assertArrayEquals(expectedArray, color.toArray()); + } + + @Test + void testSetters() { + RGB color = new RGB(0, 0, 0); + + color.r(255); + color.g(128); + color.b(64); + + assertEquals(255, color.r()); + assertEquals(128, color.g()); + assertEquals(64, color.b()); + } + + @Test + void testInstancesAreIndependent() { + RGB color1 = new RGB(0, 0, 0); + RGB color2 = new RGB(255, 255, 255); + + color1.r(128); + color1.g(64); + color1.b(32); + + assertEquals(128, color1.r()); + assertEquals(64, color1.g()); + assertEquals(32, color1.b()); + + assertEquals(255, color2.r()); + assertEquals(255, color2.g()); + assertEquals(255, color2.b()); + } + + @Test + void testFromHexCreatesIndependentInstances() { + RGB color1 = RGB.fromHex("#00FF00"); + RGB color2 = RGB.fromHex("#0000FF"); + + color1.r(128); + color1.b(32); + + assertEquals(128, color1.r()); + assertEquals(255, color1.g()); + assertEquals(32, color1.b()); + + assertEquals(0, color2.r()); + assertEquals(0, color2.g()); + assertEquals(255, color2.b()); + } }