functionalj.lens.lenses.AnyAccess 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-2019 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.lens.core.AccessUtils.createNullableAccess;
import static functionalj.lens.core.AccessUtils.createResultAccess;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import functionalj.function.Func1;
import lombok.val;
import nullablej.nullable.Nullable;
@SuppressWarnings("javadoc")
@FunctionalInterface
public interface AnyAccess
extends Func1 {
public default AnyAccess newAccess(Function access) {
return access::apply;
}
public default BooleanAccess that(Predicate checker) {
return booleanAccess(false, any -> checker.test(any));
}
public default BooleanAccess thatIs(DATA value) {
return booleanAccess(
value != null,
any -> {
return any == value;
});
}
public default BooleanAccess thatIsNot(DATA value) {
return booleanAccess(
value == null,
any -> {
return any != value;
});
}
public default BooleanAccess thatIsIn(Collection collection) {
return booleanAccess(
collection != null,
any -> {
return collection.contains(any);
});
}
public default BooleanAccess thatIsNotIn(Collection collection) {
return booleanAccess(
collection == null,
any -> {
return collection.contains(any);
});
}
public default BooleanAccess thatEquals(DATA value) {
return booleanAccess(
value == null,
any -> {
return Objects.equals(any, value);
});
}
public default BooleanAccess thatNotEqualsTo(DATA value) {
return booleanAccess(
value == null,
any -> {
return !Objects.equals(any, value);
});
}
public default BooleanAccess thatIsNull() {
return booleanAccess(
true,
any -> {
return any == null;
});
}
public default BooleanAccess thatIsNotNull() {
return booleanAccess(
false,
any -> {
return any != null;
});
}
public default IntegerAccess getHashCode() {
return intAccess(
Integer.MIN_VALUE,
any -> {
return any.hashCode();
});
}
public default StringAccess asString() {
return stringAccess(
null,
any -> {
return any.toString();
});
}
public default IntegerAccess intAccess(int defaultValue, Function function) {
return host -> {
val value = __internal__.processValue(this, host, defaultValue, function);
return value;
};
}
public default LongAccess longAccess(long defaultValue, Function function) {
return host -> {
val value = __internal__.processValue(this, host, defaultValue, function);
return value;
};
}
public default DoubleAccess doubleAccess(double defaultValue, Function function) {
return host -> {
val value = __internal__.processValue(this, host, defaultValue, function);
return value;
};
}
public default BigIntegerAccess bigIntegerAccess(BigInteger defaultValue, Function function) {
return host -> {
val value = __internal__.processValue(this, host, defaultValue, function);
return value;
};
}
public default BigDecimalAccess bigDecimalAccess(BigDecimal defaultValue, Function function) {
return host -> {
val value = __internal__.processValue(this, host, defaultValue, function);
return value;
};
}
public default StringAccess stringAccess(String defaultValue, Function function) {
return host -> {
val value = __internal__.processValue(this, host, defaultValue, function);
return value;
};
}
public default BooleanAccess booleanAccess(boolean defaultValue, Function function) {
return host -> {
val value = __internal__.processValue(this, host, defaultValue, function);
return value;
};
}
public default AnyAccess orDefaultTo(DATA fallbackValue) {
return __internal__.orDefaultTo(this, fallbackValue)::apply;
}
public default AnyAccess orDefaultFrom(Supplier extends DATA> fallbackValueSupplier) {
return __internal__.orDefaultFrom(this, fallbackValueSupplier)::apply;
}
public default AnyAccess orThrow() {
return __internal__.orThrow(this)::apply;
}
public default AnyAccess orThrow(Supplier exceptionSupplier) {
return __internal__.orThrow(this, exceptionSupplier)::apply;
}
public default NullableAccess> toNullable() {
return __internal__.toNullable(this, f -> (AnyAccess)f::apply);
}
public static class __internal__ {
public static TARGET processValue(AnyAccess access, HOST host, TARGET defaultValue, Function function) {
if (host == null)
return defaultValue;
val value = access.apply(host);
if (value == null)
return defaultValue;
val newValue = function.apply(value);
return newValue;
}
public static Function orDefaultTo(Function access, DATA fallbackValue) {
return host -> {
if (host == null)
return fallbackValue;
val value = access.apply(host);
if (value == null)
return fallbackValue;
return value;
};
}
public static Function orDefaultFrom(Function super HOST, DATA> access, Supplier extends DATA> fallbackValueSupplier) {
return host -> {
if (host == null)
return fallbackValueSupplier.get();
val value = access.apply(host);
if (value == null)
return fallbackValueSupplier.get();
return value;
};
}
public static Function orThrow(Function access) {
return host -> {
if (host == null)
throw new NullPointerException();
val value = access.apply(host);
if (value == null)
throw new NullPointerException();
return value;
};
}
public static
Function orThrow(Function access, Supplier exceptionSupplier) {
return host -> {
if (host == null)
throw exceptionSupplier.get();
val value = access.apply(host);
if (value == null)
throw exceptionSupplier.get();
return value;
};
}
public static >
NullableAccess toNullable(
Function access,
Function, ACCESS> createSubLens) {
return createNullableAccess(
host -> {
val value = access.apply(host);
return Nullable.of(value);
},
createSubLens);
}
public static >
ResultAccess toResult(
Function access,
Function, ACCESS> createSubLens) {
return createResultAccess(
Func1.from(access)::applySafely,
createSubLens);
}
}
}