com.speedment.runtime.compute.internal.expression.OrElseGetUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tool-deploy Show documentation
Show all versions of tool-deploy Show documentation
A Speedment bundle that shades all dependencies into one jar. This is
useful when deploying an application on a server.
The newest version!
/*
*
* Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
*
* 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 com.speedment.runtime.compute.internal.expression;
import com.speedment.runtime.compute.*;
import com.speedment.runtime.compute.expression.Expression;
import com.speedment.runtime.compute.expression.NonNullableExpression;
import com.speedment.runtime.compute.expression.orelse.*;
import java.math.BigDecimal;
import java.util.Objects;
import static java.util.Objects.requireNonNull;
/**
* Utility class used to create expressions that wrap a nullable expression and
* handles any {@code null}-values by applying a different expression to the
* input.
*
* @author Emil Forslund
* @since 3.1.0
*/
public final class OrElseGetUtil {
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToDoubleOrElseGet
doubleOrElseGet(ToDoubleNullable expression, ToDouble defaultGetter) {
return new ToDoubleOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToDoubleNullable.doubleOrElse(double)}.
*
* @param the input entity type
*/
private final static class ToDoubleOrElseGetImpl
extends AbstractNonNullable, ToDouble>
implements ToDoubleOrElseGet {
private ToDoubleOrElseGetImpl(ToDoubleNullable inner, ToDouble getter) {
super(inner, getter);
}
@Override
public double applyAsDouble(T object) {
return inner.isNull(object)
? getter.applyAsDouble(object)
: inner.applyAsDouble(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToFloatOrElseGet
floatOrElseGet(ToFloatNullable expression, ToFloat defaultGetter) {
return new ToFloatOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToFloatNullable.doubleOrElse(float)}.
*
* @param the input entity type
*/
private final static class ToFloatOrElseGetImpl
extends AbstractNonNullable, ToFloat>
implements ToFloatOrElseGet {
private ToFloatOrElseGetImpl(ToFloatNullable inner, ToFloat getter) {
super(inner, getter);
}
@Override
public float applyAsFloat(T object) {
return inner.isNull(object)
? getter.applyAsFloat(object)
: inner.applyAsFloat(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToLongOrElseGet
longOrElseGet(ToLongNullable expression, ToLong defaultGetter) {
return new ToLongOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToLongNullable.doubleOrElse(long)}.
*
* @param the input entity type
*/
private final static class ToLongOrElseGetImpl
extends AbstractNonNullable, ToLong>
implements ToLongOrElseGet {
private ToLongOrElseGetImpl(ToLongNullable inner, ToLong getter) {
super(inner, getter);
}
@Override
public long applyAsLong(T object) {
return inner.isNull(object)
? getter.applyAsLong(object)
: inner.applyAsLong(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToIntOrElseGet
intOrElseGet(ToIntNullable expression, ToInt defaultGetter) {
return new ToIntOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToIntNullable.doubleOrElse(int)}.
*
* @param the input entity type
*/
private final static class ToIntOrElseGetImpl
extends AbstractNonNullable, ToInt>
implements ToIntOrElseGet {
private ToIntOrElseGetImpl(ToIntNullable inner, ToInt getter) {
super(inner, getter);
}
@Override
public int applyAsInt(T object) {
return inner.isNull(object)
? getter.applyAsInt(object)
: inner.applyAsInt(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToShortOrElseGet
shortOrElseGet(ToShortNullable expression, ToShort defaultGetter) {
return new ToShortOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToShortNullable.doubleOrElse(short)}.
*
* @param the input entity type
*/
private final static class ToShortOrElseGetImpl
extends AbstractNonNullable, ToShort>
implements ToShortOrElseGet {
private ToShortOrElseGetImpl(ToShortNullable inner, ToShort getter) {
super(inner, getter);
}
@Override
public short applyAsShort(T object) {
return inner.isNull(object)
? getter.applyAsShort(object)
: inner.applyAsShort(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToByteOrElseGet
byteOrElseGet(ToByteNullable expression, ToByte defaultGetter) {
return new ToByteOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToByteNullable.doubleOrElse(byte)}.
*
* @param the input entity type
*/
private final static class ToByteOrElseGetImpl
extends AbstractNonNullable, ToByte>
implements ToByteOrElseGet {
private ToByteOrElseGetImpl(ToByteNullable inner, ToByte getter) {
super(inner, getter);
}
@Override
public byte applyAsByte(T object) {
return inner.isNull(object)
? getter.applyAsByte(object)
: inner.applyAsByte(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToCharOrElseGet
charOrElseGet(ToCharNullable expression, ToChar defaultGetter) {
return new ToCharOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToCharNullable.doubleOrElse(char)}.
*
* @param the input entity type
*/
private final static class ToCharOrElseGetImpl
extends AbstractNonNullable, ToChar>
implements ToCharOrElseGet {
private ToCharOrElseGetImpl(ToCharNullable inner, ToChar getter) {
super(inner, getter);
}
@Override
public char applyAsChar(T object) {
return inner.isNull(object)
? getter.applyAsChar(object)
: inner.applyAsChar(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToBooleanOrElseGet
booleanOrElseGet(ToBooleanNullable expression, ToBoolean defaultGetter) {
return new ToBooleanOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToBooleanNullable.doubleOrElse(boolean)}.
*
* @param the input entity type
*/
private final static class ToBooleanOrElseGetImpl
extends AbstractNonNullable, ToBoolean>
implements ToBooleanOrElseGet {
private ToBooleanOrElseGetImpl(ToBooleanNullable inner, ToBoolean getter) {
super(inner, getter);
}
@Override
public boolean applyAsBoolean(T object) {
return inner.isNull(object)
? getter.applyAsBoolean(object)
: inner.applyAsBoolean(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToStringOrElseGet
stringOrElseGet(ToStringNullable expression, ToString defaultGetter) {
return new ToStringOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToStringNullable.doubleOrElse(string)}.
*
* @param the input entity type
*/
private final static class ToStringOrElseGetImpl
extends AbstractNonNullable, ToString>
implements ToStringOrElseGet {
private ToStringOrElseGetImpl(ToStringNullable inner, ToString getter) {
super(inner, getter);
}
@Override
public String apply(T object) {
return inner.isNull(object)
? getter.apply(object)
: inner.apply(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @return the non-nullable expression
*/
public static ToBigDecimalOrElseGet
bigDecimalOrElseGet(ToBigDecimalNullable expression, ToBigDecimal defaultGetter) {
return new ToBigDecimalOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling
* {@code ToBigDecimalNullable.doubleOrElse(bigDecimal)}.
*
* @param the input entity type
*/
private final static class ToBigDecimalOrElseGetImpl
extends AbstractNonNullable, ToBigDecimal>
implements ToBigDecimalOrElseGet {
private ToBigDecimalOrElseGetImpl(ToBigDecimalNullable inner, ToBigDecimal getter) {
super(inner, getter);
}
@Override
public BigDecimal apply(T object) {
return inner.isNull(object)
? getter.apply(object)
: inner.apply(object);
}
}
/**
* Returns an expression that returns the same value as the specified
* expression if it is not {@code null}, and if the wrapped expression
* returns {@code null}, applies a different expression to the input.
*
* @param expression the nullable expression to wrap
* @param defaultGetter getter for the default value from the input
* @param the input entity type
* @param the enum type
* @return the non-nullable expression
*/
public static > ToEnumOrElseGet
enumOrElseGet(ToEnumNullable expression, ToEnum defaultGetter) {
return new ToEnumOrElseGetImpl<>(expression, defaultGetter);
}
/**
* Internal class used when calling {@code ToEnumNullable.doubleOrElse(enum)}.
*
* @param the input entity type
* @param the enum type
*/
private final static class ToEnumOrElseGetImpl>
extends AbstractNonNullable, ToEnum>
implements ToEnumOrElseGet {
private ToEnumOrElseGetImpl(ToEnumNullable inner, ToEnum getter) {
super(inner, getter);
}
@Override
public Class enumClass() {
return inner.enumClass();
}
@Override
public E apply(T object) {
return inner.isNull(object)
? getter.apply(object)
: inner.apply(object);
}
}
/**
* Abstract base for a {@link NonNullableExpression}.
*
* @param the input type
* @param the wrapped nullable expression type
* @param the default value expression type
*/
private static abstract class AbstractNonNullable
, DEFAULT extends Expression>
implements OrElseGetExpression {
final INNER inner;
final DEFAULT getter;
AbstractNonNullable(INNER inner, DEFAULT getter) {
this.inner = requireNonNull(inner);
this.getter = requireNonNull(getter);
}
@Override
public final INNER innerNullable() {
return inner;
}
@Override
public final DEFAULT defaultValueGetter() {
return getter;
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OrElseGetExpression)) return false;
final OrElseGetExpression that = (OrElseGetExpression) o;
return Objects.equals(inner, that.innerNullable()) &&
Objects.equals(getter, that.defaultValueGetter());
}
@Override
public final int hashCode() {
return Objects.hash(inner, getter);
}
}
/**
* Utility classes should not be instantiated.
*/
private OrElseGetUtil() {}
}