diff --git a/src/main/java/net/minestom/server/utils/math/ByteRange.java b/src/main/java/net/minestom/server/utils/math/ByteRange.java new file mode 100644 index 000000000..6487991f6 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/math/ByteRange.java @@ -0,0 +1,18 @@ +package net.minestom.server.utils.math; + +public class ByteRange extends Range { + + public ByteRange(Byte minimum, Byte maximum) { + super(minimum, maximum); + } + + public ByteRange(Byte value) { + super(value); + } + + /** {@inheritDoc} */ + @Override + public boolean isInRange(Byte value) { + return value >= this.getMinimum() && value <= this.getMaximum(); + } +} diff --git a/src/main/java/net/minestom/server/utils/math/DoubleRange.java b/src/main/java/net/minestom/server/utils/math/DoubleRange.java new file mode 100644 index 000000000..6b0890e76 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/math/DoubleRange.java @@ -0,0 +1,18 @@ +package net.minestom.server.utils.math; + +public class DoubleRange extends Range { + + public DoubleRange(Double minimum, Double maximum) { + super(minimum, maximum); + } + + public DoubleRange(Double value) { + super(value); + } + + /** {@inheritDoc} */ + @Override + public boolean isInRange(Double value) { + return value >= this.getMinimum() && value <= this.getMaximum(); + } +} diff --git a/src/main/java/net/minestom/server/utils/math/FloatRange.java b/src/main/java/net/minestom/server/utils/math/FloatRange.java index fa1805fb7..47a1fa972 100644 --- a/src/main/java/net/minestom/server/utils/math/FloatRange.java +++ b/src/main/java/net/minestom/server/utils/math/FloatRange.java @@ -1,32 +1,19 @@ package net.minestom.server.utils.math; -public class FloatRange { +public class FloatRange extends Range { - private float min, max; + public FloatRange(Float minimum, Float maximum) { + super(minimum, maximum); + } - public FloatRange(float min, float max) { - this.min = min; - this.max = max; - } + public FloatRange(Float value) { + super(value); + } - public FloatRange(float value) { - this(value, value); - } - - public float getMin() { - return min; - } - - public void setMin(float min) { - this.min = min; - } - - public float getMax() { - return max; - } - - public void setMax(float max) { - this.max = max; - } + /** {@inheritDoc} */ + @Override + public boolean isInRange(Float value) { + return value >= this.getMinimum() && value <= this.getMaximum(); + } } diff --git a/src/main/java/net/minestom/server/utils/math/IntRange.java b/src/main/java/net/minestom/server/utils/math/IntRange.java index bb30a1614..7d0a77c2f 100644 --- a/src/main/java/net/minestom/server/utils/math/IntRange.java +++ b/src/main/java/net/minestom/server/utils/math/IntRange.java @@ -1,31 +1,19 @@ package net.minestom.server.utils.math; -public class IntRange { +public class IntRange extends Range { - private int min, max; + public IntRange(Integer minimum, Integer maximum) { + super(minimum, maximum); + } - public IntRange(int min, int max) { - this.min = min; - this.max = max; - } + public IntRange(Integer value) { + super(value); + } - public IntRange(int value) { - this(value, value); - } + /** {@inheritDoc} */ + @Override + public boolean isInRange(Integer value) { + return value >= this.getMinimum() && value <= this.getMaximum(); - public int getMin() { - return min; - } - - public void setMin(int min) { - this.min = min; - } - - public int getMax() { - return max; - } - - public void setMax(int max) { - this.max = max; - } + } } diff --git a/src/main/java/net/minestom/server/utils/math/LongRange.java b/src/main/java/net/minestom/server/utils/math/LongRange.java new file mode 100644 index 000000000..bc44c17b9 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/math/LongRange.java @@ -0,0 +1,18 @@ +package net.minestom.server.utils.math; + +public class LongRange extends Range { + + public LongRange(Long minimum, Long maximum) { + super(minimum, maximum); + } + + public LongRange(Long value) { + super(value); + } + + /** {@inheritDoc} */ + @Override + public boolean isInRange(Long value) { + return value >= this.getMinimum() && value <= this.getMaximum(); + } +} diff --git a/src/main/java/net/minestom/server/utils/math/Range.java b/src/main/java/net/minestom/server/utils/math/Range.java new file mode 100644 index 000000000..b455b5d64 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/math/Range.java @@ -0,0 +1,77 @@ +package net.minestom.server.utils.math; + +/** + * Represents the base for any data type that is numeric. + * + * @param The type numeric of the range object. + */ +public abstract class Range { + + private T minimum; + private T maximum; + + /** + * Constructs a new {@link Range} with a {@code minimum} and a {@code maximum} value. + * + * @param minimum The minimum of the range. + * @param maximum The maximum of the range. + */ + public Range(T minimum, T maximum) { + this.minimum = minimum; + this.maximum = maximum; + } + + /** + * Constructs a new {@link Range} with the {@code value}. + * + * @param value The value of the range. + */ + public Range(T value) { + this(value, value); + } + + /** + * Retrieves the minimum value of the range. + * + * @return The range's minimum value. + */ + public T getMinimum() { + return this.minimum; + } + + /** + * Changes the minimum value of the range. + * + * @param minimum The new minimum value. + */ + public void setMinimum(T minimum) { + this.minimum = minimum; + } + + /** + * Retrieves the maximum value of the range. + * + * @return The range's maximum value. + */ + public T getMaximum() { + return this.maximum; + } + + /** + * Changes the maximum value of the range. + * + * @param maximum The new maximum value. + */ + public void setMaximum(T maximum) { + this.maximum = maximum; + } + + /** + * Whether the given {@code value} is in range of the minimum and the maximum. + * + * @param value The value to be checked. + * @return {@code true} if the value in the range of {@code minimum} and {@code maximum}, + * otherwise {@code false}. + */ + public abstract boolean isInRange(T value); +} diff --git a/src/main/java/net/minestom/server/utils/math/ShortRange.java b/src/main/java/net/minestom/server/utils/math/ShortRange.java new file mode 100644 index 000000000..725212894 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/math/ShortRange.java @@ -0,0 +1,18 @@ +package net.minestom.server.utils.math; + +public class ShortRange extends Range { + + public ShortRange(Short minimum, Short maximum) { + super(minimum, maximum); + } + + public ShortRange(Short value) { + super(value); + } + + /** {@inheritDoc} */ + @Override + public boolean isInRange(Short value) { + return value >= this.getMinimum() && value <= this.getMaximum(); + } +}