tech.sirwellington.alchemy.arguments.assertions.NumberAssertions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alchemy-arguments Show documentation
Show all versions of alchemy-arguments Show documentation
Part of the Alchemy Collection.
Easy, Simple, and Robust argument checking logic
for your Services, Libraries, and Scripts.
/*
* Copyright 2015 SirWellington Tech.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tech.sirwellington.alchemy.arguments.assertions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.arguments.Checks;
import tech.sirwellington.alchemy.arguments.FailedAssertionException;
/**
*
* @author SirWellington
*/
@NonInstantiable
public final class NumberAssertions
{
private final static Logger LOG = LoggerFactory.getLogger(NumberAssertions.class);
NumberAssertions() throws IllegalAccessException
{
throw new IllegalAccessException("cannot instantiate");
}
/**
* Asserts than an integer is {@code >} the supplied value.
*
* @param exclusiveLowerBound The argument must be {@code > exclusiveLowerBound}.
*
* @return
*/
public static AlchemyAssertion greaterThan(int exclusiveLowerBound)
{
Checks.Internal.checkThat(exclusiveLowerBound != Integer.MAX_VALUE, "Integers cannot exceed " + Integer.MAX_VALUE);
return (Integer integer) ->
{
Assertions.notNull().check(integer);
if (integer <= exclusiveLowerBound)
{
throw new FailedAssertionException("Number must be > " + exclusiveLowerBound);
}
};
}
/**
* Asserts than a long is {@code > exclusiveLowerBound}.
*
* @param exclusiveLowerBound The argument must be {@code >} this value.
*
* @return
*/
public static AlchemyAssertion greaterThan(long exclusiveLowerBound)
{
Checks.Internal.checkThat(exclusiveLowerBound != Long.MAX_VALUE, "Longs cannot exceed " + Long.MAX_VALUE);
return (Long number) ->
{
Assertions.notNull().check(number);
if (number <= exclusiveLowerBound)
{
throw new FailedAssertionException("Number must be > " + exclusiveLowerBound);
}
};
}
/**
* Asserts that an integer is {@code >=} the supplied value.
*
* @param lowerBound The argument integer must be {@code >= inclusiveLowerBound}
*
* @return
*/
public static AlchemyAssertion greaterThanOrEqualTo(int lowerBound)
{
return (Integer number) ->
{
Assertions.notNull().check(number);
if (number < lowerBound)
{
throw new FailedAssertionException("Number must be greater than or equal to " + lowerBound);
}
};
}
/**
* Asserts that a long is {@code >= inclusiveLowerBound}.
*
* @param lowerBound The argument integer must be {@code >= inclusiveUpperBound}
*
* @return
*/
public static AlchemyAssertion greaterThanOrEqualTo(long lowerBound)
{
return (Long number) ->
{
Assertions.notNull().check(number);
if (number < lowerBound)
{
throw new FailedAssertionException("Number must be greater than or equal to " + lowerBound);
}
};
}
/**
* Asserts that an integer is positive, or {@code > 0}
*
* @return
*/
public static AlchemyAssertion positiveInteger()
{
return (Integer number) ->
{
Assertions.notNull().check(number);
if (number <= 0)
{
throw new FailedAssertionException("Expected positive integer: " + number);
}
};
}
/**
* Asserts that an integer is negative, or {@code < 0}.
*
* @return
*/
public static AlchemyAssertion negativeInteger()
{
return lessThan(0);
}
/**
* Asserts that an integer is {@code <=} the supplied value.
*
* @param inclusiveUpperBound The argument must be {@code <= inclusiveUpperBound}.
*
* @return
*/
public static AlchemyAssertion lessThanOrEqualTo(int inclusiveUpperBound)
{
return (Integer number) ->
{
Assertions.notNull().check(number);
if (number > inclusiveUpperBound)
{
throw new FailedAssertionException("Number must be less than or equal to " + inclusiveUpperBound);
}
};
}
/**
* Asserts that a long is {@code <=} the supplied value.
*
* @param inclusiveUpperBound The argument must be {@code <= inclusiveUpperBound}.
*
* @return
*/
public static AlchemyAssertion lessThanOrEqualTo(long inclusiveUpperBound)
{
return (Long number) ->
{
Assertions.notNull().check(number);
if (number > inclusiveUpperBound)
{
throw new FailedAssertionException("Number must be less than or equal to " + inclusiveUpperBound);
}
};
}
/**
* Asserts that a Long is positive, or {@code > 0}
*
* @return
*/
public static AlchemyAssertion positiveLong()
{
return (Long number) ->
{
Assertions.notNull().check(number);
if (number <= 0)
{
throw new FailedAssertionException("Expected positive long: " + number);
}
};
}
/**
* Asserts that a Long is negative, or {@code < 0}.
*
* @return
*/
public static AlchemyAssertion negativeLong()
{
return lessThan(0L);
}
/**
* Asserts than an integer is {@code <} the supplied value.
*
* @param exclusiveUpperBound The argument must be {@code < exclusiveUpperBound}.
*
* @return
*/
public static AlchemyAssertion lessThan(int exclusiveUpperBound)
{
Checks.Internal.checkThat(exclusiveUpperBound != Integer.MIN_VALUE, "Ints cannot be less than " + Integer.MIN_VALUE);
return (Integer number) ->
{
Assertions.notNull().check(number);
if (number >= exclusiveUpperBound)
{
throw new FailedAssertionException("Number must be < " + exclusiveUpperBound);
}
};
}
/**
* Asserts than a long is {@code <} the supplied value.
*
* @param exclusiveUpperBound The argument must be {@code < exclusiveUpperBound}.
*
* @return
*/
public static AlchemyAssertion lessThan(long exclusiveUpperBound)
{
Checks.Internal.checkThat(exclusiveUpperBound != Long.MIN_VALUE, "Longs cannot be less than " + Long.MIN_VALUE);
return (Long number) ->
{
Assertions.notNull().check(number);
if (number >= exclusiveUpperBound)
{
throw new FailedAssertionException("Number must be < " + exclusiveUpperBound);
}
};
}
/**
* Asserts that an integer argument is in the specified (inclusive) range.
*
* @param min The lower bound for the range, inclusive
* @param max The upper bound for the range, inclusive
*
* @return
*
* @throws IllegalArgumentException If {@code min >= max}. {@code min} should always be less than {@code max}.
*/
public static AlchemyAssertion numberBetween(int min, int max) throws IllegalArgumentException
{
Checks.Internal.checkThat(min < max, "Minimum must be less than Max.");
return (Integer number) ->
{
Assertions.notNull().check(number);
if (number < min || number > max)
{
String message = String.format("Expected a number between %d and %d but got %d instead", min, max, number);
throw new FailedAssertionException(message);
}
};
}
/**
* Asserts that a long argument is in the specified (inclusive) range.
*
* @param min The lower bound for the range, inclusive
* @param max The upper bound for the range, inclusive
*
* @return
*
* @throws IllegalArgumentException If {@code min >= max}. {@code min} should always be less
*/
public static AlchemyAssertion numberBetween(long min, long max) throws IllegalArgumentException
{
Checks.Internal.checkThat(min < max, "Minimum must be less than Max.");
return (Long number) ->
{
Assertions.notNull().check(number);
if (number < min || number > max)
{
String message = String.format("Expected a number between %d and %d but got %d instead", min, max, number);
throw new FailedAssertionException(message);
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy