/
ToBigDecimalNullable.java
103 lines (91 loc) · 3.16 KB
/
ToBigDecimalNullable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.speedment.runtime.compute;
import com.speedment.runtime.compute.expression.Expression;
import com.speedment.runtime.compute.expression.ExpressionType;
import com.speedment.runtime.compute.internal.expression.ComposedUtil;
import com.speedment.runtime.compute.internal.expression.OrElseGetUtil;
import com.speedment.runtime.compute.internal.expression.OrElseThrowUtil;
import com.speedment.runtime.compute.internal.expression.OrElseUtil;
import com.speedment.runtime.compute.trait.HasCompare;
import com.speedment.runtime.compute.trait.HasCompose;
import com.speedment.runtime.compute.trait.HasHash;
import com.speedment.runtime.compute.trait.ToNullable;
import java.math.BigDecimal;
import static java.util.Objects.requireNonNull;
import java.util.function.Function;
/**
* Expression that given an entity returns a nullable {@code BigDecimal} value.
* This expression can be implemented using a lambda, or it can be a result of
* another operation. It has additional methods for operating on it.
*
* @param <T> type to extract from
*
* @see Function
*
* @author Per Minborg
* @since 3.1.0
*/
@FunctionalInterface
public interface ToBigDecimalNullable<T>
extends Expression<T>,
ToNullable<T, BigDecimal, ToBigDecimal<T>>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
/**
* Returns a typed {@code ToBigDecimalNullable<T>} using the provided
* {@code lambda}.
*
* @param <T> type to extract from
* @param lambda to convert
* @return a typed {@code ToBigDecimalNullable<T>} using the provided
* {@code lambda}
*
* @throws NullPointerException if the provided {@code lambda} is
* {@code null}
*/
static <T> ToBigDecimalNullable<T> of(Function<T, BigDecimal> lambda) {
if (lambda instanceof ToBigDecimalNullable) {
return (ToBigDecimalNullable<T>) lambda;
} else {
return lambda::apply;
}
}
@Override
BigDecimal apply(T object);
@Override
default ExpressionType expressionType() {
return ExpressionType.BIG_DECIMAL_NULLABLE;
}
@Override
default ToBigDecimal<T> orThrow() throws NullPointerException {
return OrElseThrowUtil.bigDecimalOrElseThrow(this);
}
@Override
default ToBigDecimal<T> orElseGet(ToBigDecimal<T> getter) {
return OrElseGetUtil.bigDecimalOrElseGet(this, getter);
}
@Override
default ToBigDecimal<T> orElse(BigDecimal value) {
return OrElseUtil.bigDecimalOrElse(this, value);
}
@Override
default long hash(T object) {
return isNull(object) ? 0 : apply(object).hashCode();
}
@Override
default int compare(T first, T second) {
if (isNull(first)) {
return isNull(second) ? 0 : 1;
} else if (isNull(second)) {
return -1;
} else {
return apply(first).compareTo(apply(second));
}
}
@Override
default <V> ToBigDecimalNullable<V> compose(Function<? super V, ? extends T> before) {
@SuppressWarnings("unchecked")
final Function<V, T> casted = (Function<V, T>) before;
return ComposedUtil.composeNullable(casted, this);
}
}