Skip to content

isZero() & isOne() introduced to NumericConstraintBase. #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/BigDecimalConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ protected Predicate<BigDecimal> isLessThanOrEqual(BigDecimal max) {
protected BigDecimal zeroValue() {
return BigDecimal.ZERO;
}

@Override
protected BigDecimal oneValue() {
return BigDecimal.ONE;
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/BigIntegerConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ protected Predicate<BigInteger> isLessThanOrEqual(BigInteger max) {
protected BigInteger zeroValue() {
return BigInteger.ZERO;
}

@Override
protected BigInteger oneValue() {
return BigInteger.ONE;
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/ByteConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ protected Predicate<Byte> isLessThanOrEqual(Byte max) {
protected Byte zeroValue() {
return 0;
}

@Override
protected Byte oneValue() {
return 1;
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/CharacterConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ protected Predicate<Character> isLessThanOrEqual(Character max) {
protected Character zeroValue() {
return Character.MIN_VALUE;
}

@Override
protected Character oneValue() {
return '1';
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/DoubleConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ protected Predicate<Double> isLessThanOrEqual(Double max) {
protected Double zeroValue() {
return 0.0;
}

@Override
protected Double oneValue() {
return 1.0;
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/FloatConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ protected Predicate<Float> isLessThanOrEqual(Float max) {
protected Float zeroValue() {
return 0f;
}

@Override
protected Float oneValue() {
return 1f;
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/IntegerConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ protected Predicate<Integer> isLessThanOrEqual(Integer max) {
protected Integer zeroValue() {
return 0;
}

@Override
protected Integer oneValue() {
return 1;
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/LongConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ protected Predicate<Long> isLessThanOrEqual(Long max) {
protected Long zeroValue() {
return 0L;
}

@Override
protected Long oneValue() {
return 1L;
}
}
5 changes: 5 additions & 0 deletions src/main/java/am/ik/yavi/constraint/ShortConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ protected Predicate<Short> isLessThanOrEqual(Short max) {
protected Short zeroValue() {
return 0;
}

@Override
protected Short oneValue() {
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public C lessThanOrEqual(V max) {
}

/**
* Is the given value positve. Positive means it is greater than 0.
*
* <pre>
* 0 -> false
* 1 -> true
* n where n > 0 -> true
* n where n < 0 -> false
* </pre>
*
* @since 0.10.0
*/
public C positive() {
Expand All @@ -59,9 +68,7 @@ public C positive() {
return cast();
}

/**
* @since 0.10.0
*/
/** @since 0.10.0 */
public C positiveOrZero() {
this.predicates()
.add(ConstraintPredicate.of(this.isGreaterThanOrEqual(zeroValue()),
Expand All @@ -70,6 +77,16 @@ public C positiveOrZero() {
}

/**
* Is the given value negative. Negative means it is less than 0.
*
* <pre>
* 0 -> false
* 1 -> false
* -1 -> true
* n where n > 0 -> false
* n where n < 0 -> true
* </pre>
*
* @since 0.10.0
*/
public C negative() {
Expand All @@ -78,15 +95,29 @@ public C negative() {
return cast();
}

/**
* @since 0.10.0
*/
/** @since 0.10.0 */
public C negaitveOrZero() {
this.predicates().add(ConstraintPredicate.of(this.isLessThanOrEqual(zeroValue()),
NUMERIC_NEGATIVE_OR_ZERO, () -> new Object[] {}, VALID));
return cast();
}

/**
* Is the given value equal to the zero representation of its type. The exact
* representation of <i>zero</i> can be found in {@link #zeroValue()}.
*/
public C isZero() {
return this.equalTo(zeroValue());
}

/**
* Is the given value equal to the <i>one</i> representation of its type The exact
* representation of <i>one</i> can be found in {@link #oneValue()}.
*/
public C isOne() {
return this.equalTo(oneValue());
}

protected abstract Predicate<V> isGreaterThan(V min);

protected abstract Predicate<V> isGreaterThanOrEqual(V min);
Expand All @@ -95,5 +126,17 @@ public C negaitveOrZero() {

protected abstract Predicate<V> isLessThanOrEqual(V max);

/**
* The value that represents zero
*
* @return the numeric value zero
*/
protected abstract V zeroValue();

/**
* The value that represents one
*
* @return the numeric value one
*/
protected abstract V oneValue();
}
29 changes: 29 additions & 0 deletions src/test/java/am/ik/yavi/constraint/BigDecimalConstraintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package am.ik.yavi.constraint;

import am.ik.yavi.constraint.base.NumericConstraintBase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -155,6 +156,34 @@ void validNegativeOrZero(BigDecimal value) {
assertThat(predicate.test(value)).isTrue();
}

@ParameterizedTest
@ValueSource(strings = { "-101", "-120" })
void invalidZero(BigDecimal value) {
Predicate<BigDecimal> predicate = retrievePredicate(
NumericConstraintBase::isZero);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validZero() {
Predicate<BigDecimal> predicate = retrievePredicate(
NumericConstraintBase::isZero);
assertThat(predicate.test(BigDecimal.ZERO)).isTrue();
}

@ParameterizedTest
@ValueSource(strings = { "-101", "-120" })
void invalidOne(BigDecimal value) {
Predicate<BigDecimal> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validOne() {
Predicate<BigDecimal> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test(BigDecimal.ONE)).isTrue();
}

private static Predicate<BigDecimal> retrievePredicate(
Function<BigDecimalConstraint<BigDecimal>, BigDecimalConstraint<BigDecimal>> constraint) {
return constraint.apply(new BigDecimalConstraint<>()).predicates().peekFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package am.ik.yavi.constraint;

import am.ik.yavi.constraint.base.NumericConstraintBase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -156,6 +156,34 @@ void validNegativeOrZero(BigInteger value) {
assertThat(predicate.test(value)).isTrue();
}

@ParameterizedTest
@ValueSource(strings = { "-101", "-120" })
void invalidZero(BigInteger value) {
Predicate<BigInteger> predicate = retrievePredicate(
NumericConstraintBase::isZero);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validZero() {
Predicate<BigInteger> predicate = retrievePredicate(
NumericConstraintBase::isZero);
assertThat(predicate.test(BigInteger.ZERO)).isTrue();
}

@ParameterizedTest
@ValueSource(strings = { "-101", "-120" })
void invalidOne(BigInteger value) {
Predicate<BigInteger> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validOne() {
Predicate<BigInteger> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test(BigInteger.ONE)).isTrue();
}

private static Predicate<BigInteger> retrievePredicate(
Function<BigIntegerConstraint<BigInteger>, BigIntegerConstraint<BigInteger>> constraint) {
return constraint.apply(new BigIntegerConstraint<>()).predicates().peekFirst()
Expand Down
28 changes: 27 additions & 1 deletion src/test/java/am/ik/yavi/constraint/ByteConstraintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package am.ik.yavi.constraint;

import am.ik.yavi.constraint.base.NumericConstraintBase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.math.BigInteger;
import java.util.function.Function;
import java.util.function.Predicate;

Expand Down Expand Up @@ -145,6 +145,32 @@ void validNegativeOrZero(byte value) {
assertThat(predicate.test(value)).isTrue();
}

@ParameterizedTest
@ValueSource(bytes = { -101, -120 })
void invalidZero(byte value) {
Predicate<Byte> predicate = retrievePredicate(NumericConstraintBase::isZero);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validZero() {
Predicate<Byte> predicate = retrievePredicate(NumericConstraintBase::isZero);
assertThat(predicate.test(Byte.valueOf("0"))).isTrue();
}

@ParameterizedTest
@ValueSource(bytes = { -101, -120 })
void invalidOne(byte value) {
Predicate<Byte> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validOne() {
Predicate<Byte> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test(Byte.valueOf("1"))).isTrue();
}

private static Predicate<Byte> retrievePredicate(
Function<ByteConstraint<Byte>, ByteConstraint<Byte>> constraint) {
return constraint.apply(new ByteConstraint<>()).predicates().peekFirst()
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/am/ik/yavi/constraint/CharacterConstraintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package am.ik.yavi.constraint;

import am.ik.yavi.constraint.base.NumericConstraintBase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -112,6 +113,32 @@ void validNegative(char value) {
assertThat(predicate.test(value)).isFalse();
}

@ParameterizedTest
@ValueSource(chars = { 101, 120 })
void invalidZero(char value) {
Predicate<Character> predicate = retrievePredicate(NumericConstraintBase::isZero);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validZero() {
Predicate<Character> predicate = retrievePredicate(NumericConstraintBase::isZero);
assertThat(predicate.test(Character.MIN_VALUE)).isTrue();
}

@ParameterizedTest
@ValueSource(chars = { 101, 120 })
void invalidOne(char value) {
Predicate<Character> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test(value)).isFalse();
}

@Test
void validOne() {
Predicate<Character> predicate = retrievePredicate(NumericConstraintBase::isOne);
assertThat(predicate.test('1')).isTrue();
}

private static Predicate<Character> retrievePredicate(
Function<CharacterConstraint<Character>, CharacterConstraint<Character>> constraint) {
return constraint.apply(new CharacterConstraint<>()).predicates().peekFirst()
Expand Down
Loading