functionalj.lens.lenses.LongAccess Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// Copyright (c) 2017-2021 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.lens.lenses;
import static functionalj.function.Apply.access;
import static functionalj.function.Apply.applyPrimitive;
import static functionalj.function.Apply.getPrimitive;
import static functionalj.function.Compare.compareOrNull;
import static java.util.Objects.requireNonNull;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.LongPredicate;
import java.util.function.LongSupplier;
import java.util.function.LongUnaryOperator;
import java.util.function.ToLongFunction;
import functionalj.function.Func1;
import functionalj.functions.LongFuncs;
import functionalj.lens.lenses.java.time.InstantAccess;
import functionalj.lens.lenses.java.time.LocalDateTimeAccess;
import functionalj.ref.Ref;
import lombok.val;
public interface LongAccess
extends
NumberAccess>,
ToLongFunction,
ConcreteAccess> {
/** The reference to a function to calculate factorial for integer. **/
public static final Ref factorialRef = Ref.ofValue(value -> LongFuncs.factorial(value));
public static LongAccess of(Function accessToValue) {
requireNonNull(accessToValue);
if (accessToValue instanceof LongAccess) {
return (LongAccess)accessToValue;
}
if (accessToValue instanceof ToLongFunction) {
@SuppressWarnings("unchecked")
val func1 = (ToLongFunction)accessToValue;
val access = ofPrimitive(func1);
return access;
}
if (accessToValue instanceof Func1) {
val func1 = (Func1)accessToValue;
val access = (LongAccessBoxed)func1::applyUnsafe;
return access;
}
val func = (Function)accessToValue;
val access = (LongAccessBoxed)(host -> func.apply(host));
return access;
}
public static LongAccess ofPrimitive(ToLongFunction accessToValue) {
requireNonNull(accessToValue);
val access = (LongAccessPrimitive)accessToValue::applyAsLong;
return access;
}
@Override
public default LongAccess newAccess(Function accessToValue) {
return of(accessToValue);
}
//== abstract functionalities ==
public long applyAsLong(HOST host);
public Long applyUnsafe(HOST host) throws Exception;
//-- conversion --
public default LongAccessBoxed boxed() {
return host -> apply(host);
}
@Override
public default IntegerAccess asInteger() {
return asInteger(Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public default IntegerAccess asInteger(int overflowValue) {
return asInteger(overflowValue);
}
public default IntegerAccess asInteger(int negativeOverflowValue, int positiveOverflowValue) {
return IntegerAccess.of(host -> {
val value = access(this, host);
if (value < Integer.MIN_VALUE)
return negativeOverflowValue;
if (value > Integer.MAX_VALUE)
return positiveOverflowValue;
return (int)Math.round(value);
});
}
@Override
public default LongAccessPrimitive asLong() {
return host -> {
long longValue = applyAsLong(host);
return (long)longValue;
};
}
@Override
public default DoubleAccessPrimitive asDouble() {
return host -> {
long longValue = applyAsLong(host);
return (double)longValue;
};
}
@Override
public default StringAccess asString() {
return host -> "" + access(this, host);
}
@Override
public default StringAccess asString(String template) {
return host -> {
val value = access(this, host);
return String.format(template, value);
};
}
public default BigIntegerAccess asBitInteger() {
return host -> {
val value = access(this, host);
return BigInteger.valueOf(value);
};
}
public default BigDecimalAccess asBitDecimal() {
return host -> {
val value = access(this, host);
return BigDecimal.valueOf(value);
};
}
public default InstantAccess asInstant() {
return host -> {
long timestampMilliSecond = apply(host);
return Instant.ofEpochMilli(timestampMilliSecond);
};
}
public default LocalDateTimeAccess asLocalDateTime() {
return asLocalDateTime(ZoneId.systemDefault());
}
public default LocalDateTimeAccess asLocalDateTime(ZoneId zone) {
return host -> {
val timestampMilliSecond = apply(host);
val instant = Instant.ofEpochMilli(timestampMilliSecond);
return LocalDateTime.ofInstant(instant, zone);
};
}
//-- Equality --
public default BooleanAccessPrimitive that(LongPredicate checker) {
return host -> {
val value = access(this, host);
return checker.test(value);
};
}
public default BooleanAccessPrimitive thatIs(long anotherValue) {
return host -> {
val value = access(this, host);
return value == anotherValue;
};
}
public default BooleanAccessPrimitive thatIs(LongSupplier anotherSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(anotherSupplier);
return value == anotherValue;
};
}
public default BooleanAccessPrimitive thatIs(ToLongFunction anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherAccess, host);
return value == anotherValue;
};
}
public default BooleanAccessPrimitive thatIsNot(long anotherValue) {
return host -> {
val value = access(this, host);
return value != anotherValue;
};
}
public default BooleanAccessPrimitive thatIsNot(LongSupplier anotherSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(anotherSupplier);
return value != anotherValue;
};
}
public default BooleanAccessPrimitive thatIsNot(ToLongFunction anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherAccess, host);
return value != anotherValue;
};
}
public default BooleanAccessPrimitive thatIsAnyOf(long ... otherValues) {
return host -> {
val value = access(this, host);
for (val anotherValue : otherValues) {
if (value == anotherValue) {
return true;
}
}
return false;
};
}
// public default BooleanAccessPrimitive thatIsAnyOf(LongFuncList otherValues) {
// return host -> {
// val value = access(this, host);
// return otherValues.anyMatch(anotherValue -> value == anotherValue);
// };
// }
public default BooleanAccessPrimitive thatIsNoneOf(long ... otherValues) {
return host -> {
val value = access(this, host);
for (val anotherValue : otherValues) {
if (value == anotherValue) {
return false;
}
}
return true;
};
}
// public default BooleanAccessPrimitive thatIsNoneOf(LongFuncList otherValues) {
// return host -> {
// val value = access(this, host);
// return otherValues.noneMatch(anotherValue -> value == anotherValue);
// };
// }
public default BooleanAccessPrimitive thatIsOne() {
return thatIs(1);
}
public default BooleanAccessPrimitive thatIsZero() {
return thatIs(0);
}
public default BooleanAccessPrimitive thatIsMinusOne() {
return thatIs(-1);
}
public default BooleanAccessPrimitive thatIsFourtyTwo() {
return thatIs(42);
}
public default BooleanAccessPrimitive thatIsNotOne() {
return thatIsNot(1);
}
public default BooleanAccessPrimitive thatIsNotZero() {
return thatIsNot(0);
}
public default BooleanAccessPrimitive thatIsNotMinusOne() {
return thatIsNot(-1);
}
public default BooleanAccessPrimitive thatIsPositive() {
return host -> {
val value = access(this, host);
return value > 0;
};
}
public default BooleanAccessPrimitive thatIsNegative() {
return host -> {
val value = access(this, host);
return value < 0;
};
}
public default BooleanAccessPrimitive thatIsNotPositive() {
return host -> {
val value = access(this, host);
return value <= 0;
};
}
public default BooleanAccessPrimitive thatIsNotNegative() {
return host -> {
val value = access(this, host);
return value >= 0;
};
}
public default BooleanAccessPrimitive thatEquals(long anotherValue) {
return thatIs(anotherValue);
}
public default BooleanAccessPrimitive thatEquals(LongSupplier anotherSupplier) {
return thatIs(anotherSupplier);
}
public default BooleanAccessPrimitive thatEquals(ToLongFunction anotherAccess) {
return thatIs(anotherAccess);
}
public default BooleanAccessPrimitive eq(long anotherValue) {
return thatEquals(anotherValue);
}
public default BooleanAccessPrimitive eq(LongSupplier anotherSupplier) {
return thatEquals(anotherSupplier);
}
public default BooleanAccessPrimitive eq(ToLongFunction anotherAccess) {
return thatEquals(anotherAccess);
}
public default BooleanAccessPrimitive thatNotEquals(long anotherValue) {
return thatNotEquals(anotherValue);
}
public default BooleanAccessPrimitive thatNotEquals(LongSupplier anotherSupplier) {
return thatNotEquals(anotherSupplier);
}
public default BooleanAccessPrimitive thatNotEquals(ToLongFunction anotherAccess) {
return thatNotEquals(anotherAccess);
}
public default BooleanAccessPrimitive neq(long anotherValue) {
return thatNotEquals(anotherValue);
}
public default BooleanAccessPrimitive neq(LongSupplier anotherSupplier) {
return thatNotEquals(anotherSupplier);
}
public default BooleanAccessPrimitive neq(ToLongFunction anotherAccess) {
return thatNotEquals(anotherAccess);
}
public default BooleanAccessPrimitive thatEqualsOne() {
return thatEquals(1);
}
public default BooleanAccessPrimitive thatEqualsZero() {
return thatEquals(0);
}
public default BooleanAccessPrimitive thatEqualsMinusOne() {
return thatEquals(-1);
}
public default BooleanAccessPrimitive thatEqualsFourtyTwo() {
return thatEquals(42);
}
public default BooleanAccessPrimitive thatNotEqualsOne() {
return thatEquals(1);
}
public default BooleanAccessPrimitive thatNotEqualsZero() {
return thatEquals(0);
}
public default BooleanAccessPrimitive thatNotEqualsMinusOne() {
return thatEquals(-1);
}
//-- Compare --
public default Comparator inOrder() {
return (a, b) -> {
val aValue = this.apply(a);
val bValue = this.apply(b);
return compareOrNull(aValue, bValue);
};
}
public default Comparator inReverseOrder() {
return (a, b) -> {
val aValue = this.apply(a);
val bValue = this.apply(b);
return compareOrNull(bValue, aValue);
};
}
public default IntegerAccessPrimitive compareTo(long anotherValue) {
return host -> {
val value = access(this, host);
val compare = compareOrNull(value, anotherValue);
return compare;
};
}
public default IntegerAccessPrimitive compareTo(LongSupplier anotherSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(anotherSupplier);
val compare = compareOrNull(value, anotherValue);
return compare;
};
}
public default IntegerAccessPrimitive compareTo(ToLongFunction anotherFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherFunction, host);
val compare = compareOrNull(value, anotherValue);
return compare;
};
}
public default IntegerAccessPrimitive cmp(long anotherValue) {
return compareTo(anotherValue);
}
public default IntegerAccessPrimitive cmp(LongSupplier anotherSupplier) {
return compareTo(anotherSupplier);
}
public default IntegerAccessPrimitive cmp(ToLongFunction anotherAccess) {
return compareTo(anotherAccess);
}
public default BooleanAccessPrimitive thatGreaterThan(long anotherValue) {
return host -> {
val value = access(this, host);
return value > anotherValue;
};
}
public default BooleanAccessPrimitive thatGreaterThan(LongSupplier anotherSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(anotherSupplier);
return value > anotherValue;
};
}
public default BooleanAccessPrimitive thatGreaterThan(ToLongFunction anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherAccess, host);
return value > anotherValue;
};
}
public default BooleanAccessPrimitive gt(long anotherValue) {
return thatGreaterThan(anotherValue);
}
public default BooleanAccessPrimitive gt(LongSupplier anotherSupplier) {
return thatGreaterThan(anotherSupplier);
}
public default BooleanAccessPrimitive gt(ToLongFunction anotherAccess) {
return thatGreaterThan(anotherAccess);
}
public default BooleanAccessPrimitive thatLessThan(long anotherValue) {
return host -> {
val value = access(this, host);
return value < anotherValue;
};
}
public default BooleanAccessPrimitive thatLessThan(LongSupplier anotherSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(anotherSupplier);
return value < anotherValue;
};
}
public default BooleanAccessPrimitive thatLessThan(ToLongFunction anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherAccess, host);
return value < anotherValue;
};
}
public default BooleanAccessPrimitive lt(long anotherValue) {
return thatLessThan(anotherValue);
}
public default BooleanAccessPrimitive lt(LongSupplier anotherSupplier) {
return thatLessThan(anotherSupplier);
}
public default BooleanAccessPrimitive lt(ToLongFunction anotherAccess) {
return thatLessThan(anotherAccess);
}
public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(long anotherValue) {
return host -> {
val value = access(this, host);
return value >= anotherValue;
};
}
public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(LongSupplier anotherSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(anotherSupplier);
return value >= anotherValue;
};
}
public default BooleanAccessPrimitive thatGreaterThanOrEqualsTo(ToLongFunction anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherAccess, host);
return value >= anotherValue;
};
}
public default BooleanAccessPrimitive gteq(long anotherValue) {
return thatGreaterThanOrEqualsTo(anotherValue);
}
public default BooleanAccessPrimitive gteq(LongSupplier anotherSupplier) {
return thatGreaterThanOrEqualsTo(anotherSupplier);
}
public default BooleanAccessPrimitive gteq(ToLongFunction anotherAccess) {
return thatGreaterThanOrEqualsTo(anotherAccess);
}
public default BooleanAccessPrimitive thatLessThanOrEqualsTo(long anotherValue) {
return host -> {
val value = access(this, host);
return value <= anotherValue;
};
}
public default BooleanAccessPrimitive thatLessThanOrEqualsTo(LongSupplier anotherSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(anotherSupplier);
return value <= anotherValue;
};
}
public default BooleanAccessPrimitive thatLessThanOrEqualsTo(ToLongFunction anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherAccess, host);
return value <= anotherValue;
};
}
public default BooleanAccessPrimitive lteq(long anotherValue) {
return thatLessThanOrEqualsTo(anotherValue);
}
public default BooleanAccessPrimitive lteq(LongSupplier anotherSupplier) {
return thatLessThanOrEqualsTo(anotherSupplier);
}
public default BooleanAccessPrimitive lteq(ToLongFunction anotherAccess) {
return thatLessThanOrEqualsTo(anotherAccess);
}
//-- Min+Max --
public default LongAccessPrimitive min(long anotherValue) {
return host -> {
val value = access(this, host);
return Math.min(value, anotherValue);
};
}
public default LongAccessPrimitive min(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return Math.min(value, anotherValue);
};
}
public default LongAccessPrimitive min(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return Math.min(value, anotherValue);
};
}
public default LongAccessPrimitive max(long anotherValue) {
return host -> {
val value = access(this, host);
return Math.max(value, anotherValue);
};
}
public default LongAccessPrimitive max(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return Math.max(value, anotherValue);
};
}
public default LongAccessPrimitive max(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return Math.max(value, anotherValue);
};
}
//-- Math --
public default MathOperators __mathOperators() {
return LongMathOperators.instance;
}
public default BooleanAccessPrimitive thatIsOdd() {
return host -> {
val value = access(this, host);
return value % 2 != 0;
};
}
public default BooleanAccessPrimitive thatIsEven() {
return host -> {
val value = access(this, host);
return value % 2 == 0;
};
}
public default BooleanAccessPrimitive thatIsDivisibleBy(long anotherValue) {
return host -> {
val value = access(this, host);
return value % anotherValue == 0;
};
}
public default BooleanAccessPrimitive thatIsDivisibleBy(LongSupplier anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = anotherAccess.getAsLong();
return value % anotherValue == 0;
};
}
public default BooleanAccessPrimitive thatIsDivisibleBy(ToLongFunction anotherAccess) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(anotherAccess, host);
return value % anotherValue == 0;
};
}
public default LongAccessPrimitive abs() {
return host -> {
val value = access(this, host);
return (value < 0) ? -value : value;
};
}
public default LongAccessPrimitive negate() {
return host -> {
val value = access(this, host);
return -value;
};
}
public default LongAccessPrimitive signum() {
return host -> {
val value = access(this, host);
return (value == 0) ? 0 : (value < 0) ? -1 : 1;
};
}
public default LongAccessPrimitive plus(long anotherValue) {
return host -> {
val value = access(this, host);
return value + anotherValue;
};
}
public default LongAccessPrimitive plus(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return value + anotherValue;
};
}
public default LongAccessPrimitive plus(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return value + anotherValue;
};
}
public default LongAccessPrimitive minus(long anotherValue) {
return host -> {
val value = access(this, host);
return value - anotherValue;
};
}
public default LongAccessPrimitive minus(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return value - anotherValue;
};
}
public default LongAccessPrimitive minus(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return value - anotherValue;
};
}
public default LongAccessPrimitive time(long anotherValue) {
return host -> {
val value = access(this, host);
return value * anotherValue;
};
}
public default LongAccessPrimitive time(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return value * anotherValue;
};
}
public default LongAccessPrimitive time(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return value * anotherValue;
};
}
public default DoubleAccessPrimitive dividedBy(long anotherValue) {
return host -> {
val value = access(this, host);
return 1.0 * value / anotherValue;
};
}
public default DoubleAccessPrimitive dividedBy(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return 1.0 * value / anotherValue;
};
}
public default DoubleAccessPrimitive dividedBy(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return 1.0*value / anotherValue;
};
}
public default LongAccessPrimitive remainderBy(long anotherValue) {
return host -> {
val value = access(this, host);
return value % anotherValue;
};
}
public default LongAccessPrimitive remainderBy(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return value % anotherValue;
};
}
public default LongAccessPrimitive remainderBy(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return value % anotherValue;
};
}
public default DoubleAccessPrimitive inverse() {
return host -> {
val value = access(this, host);
return 1/(value * 1.0);
};
}
public default LongAccessPrimitive square() {
return host -> {
val value = access(this, host);
return value * value;
};
}
public default DoubleAccessPrimitive squareRoot () {
return host -> {
val value = access(this, host);
return Math.sqrt(value);
};
}
public default LongAccessPrimitive factorial() {
return host -> {
val value = access(this, host);
if (value <= 0) {
return 1;
}
return factorialRef.get().applyAsLong(value);
};
}
// TODO - Make this Long once we are ready.
public default LongAccessPrimitive pow(long anotherValue) {
return host -> {
val value = access(this, host);
return (long)Math.pow(value, anotherValue);
};
}
public default LongAccessPrimitive pow(LongSupplier valueSupplier) {
return host -> {
val value = access(this, host);
val anotherValue = getPrimitive(valueSupplier);
return (long)Math.pow(value, anotherValue);
};
}
public default LongAccessPrimitive pow(ToLongFunction valueFunction) {
return host -> {
val value = access(this, host);
val anotherValue = applyPrimitive(valueFunction, host);
return (long)Math.pow(value, anotherValue);
};
}
}