com.speedment.runtime.compute.internal.expression.OrElseUtil 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 simply returning a default value instead.
*
* @author Emil Forslund
* @since 3.1.0
*/
public final class OrElseUtil {
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToDoubleOrElse
doubleOrElse(ToDoubleNullable expression, double defaultValue) {
return new ToDoubleOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToDoubleNullable.orElse(double)}.
*
* @param the input entity type
*/
private final static class ToDoubleOrElseImpl
extends AbstractNonNullable>
implements ToDoubleOrElse {
private final double value;
private ToDoubleOrElseImpl(ToDoubleNullable inner, double value) {
super(inner);
this.value = value;
}
@Override
public double defaultValue() {
return value;
}
@Override
public double applyAsDouble(T object) {
return inner.isNull(object) ? value :
inner.applyAsDouble(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToDoubleOrElse)) return false;
final ToDoubleOrElse that = (ToDoubleOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToFloatOrElse
floatOrElse(ToFloatNullable expression, float defaultValue) {
return new ToFloatOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToFloatNullable.orElse(float)}.
*
* @param the input entity type
*/
private final static class ToFloatOrElseImpl
extends AbstractNonNullable>
implements ToFloatOrElse {
private final float value;
private ToFloatOrElseImpl(ToFloatNullable inner, float value) {
super(inner);
this.value = value;
}
@Override
public float defaultValue() {
return value;
}
@Override
public float applyAsFloat(T object) {
return inner.isNull(object) ? value :
inner.applyAsFloat(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToFloatOrElse)) return false;
final ToFloatOrElse that = (ToFloatOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToLongOrElse
longOrElse(ToLongNullable expression, long defaultValue) {
return new ToLongOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToLongNullable.orElse(long)}.
*
* @param the input entity type
*/
private final static class ToLongOrElseImpl
extends AbstractNonNullable>
implements ToLongOrElse {
private final long value;
private ToLongOrElseImpl(ToLongNullable inner, long value) {
super(inner);
this.value = value;
}
@Override
public long defaultValue() {
return value;
}
@Override
public long applyAsLong(T object) {
return inner.isNull(object) ? value :
inner.applyAsLong(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToLongOrElse)) return false;
final ToLongOrElse that = (ToLongOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToIntOrElse
intOrElse(ToIntNullable expression, int defaultValue) {
return new ToIntOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToIntNullable.orElse(int)}.
*
* @param the input entity type
*/
private final static class ToIntOrElseImpl
extends AbstractNonNullable>
implements ToIntOrElse {
private final int value;
private ToIntOrElseImpl(ToIntNullable inner, int value) {
super(inner);
this.value = value;
}
@Override
public int defaultValue() {
return value;
}
@Override
public int applyAsInt(T object) {
return inner.isNull(object) ? value :
inner.applyAsInt(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToIntOrElse)) return false;
final ToIntOrElse that = (ToIntOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToShortOrElse
shortOrElse(ToShortNullable expression, short defaultValue) {
return new ToShortOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToShortNullable.orElse(short)}.
*
* @param the input entity type
*/
private final static class ToShortOrElseImpl
extends AbstractNonNullable>
implements ToShortOrElse {
private final short value;
private ToShortOrElseImpl(ToShortNullable inner, short value) {
super(inner);
this.value = value;
}
@Override
public short defaultValue() {
return value;
}
@Override
public short applyAsShort(T object) {
return inner.isNull(object) ? value :
inner.applyAsShort(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToShortOrElse)) return false;
final ToShortOrElse that = (ToShortOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToByteOrElse
byteOrElse(ToByteNullable expression, byte defaultValue) {
return new ToByteOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToByteNullable.orElse(byte)}.
*
* @param the input entity type
*/
private final static class ToByteOrElseImpl
extends AbstractNonNullable>
implements ToByteOrElse {
private final byte value;
private ToByteOrElseImpl(ToByteNullable inner, byte value) {
super(inner);
this.value = value;
}
@Override
public byte defaultValue() {
return value;
}
@Override
public byte applyAsByte(T object) {
return inner.isNull(object) ? value :
inner.applyAsByte(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToByteOrElse)) return false;
final ToByteOrElse that = (ToByteOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToCharOrElse
charOrElse(ToCharNullable expression, char defaultValue) {
return new ToCharOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToCharNullable.orElse(char)}.
*
* @param the input entity type
*/
private final static class ToCharOrElseImpl
extends AbstractNonNullable>
implements ToCharOrElse {
private final char value;
private ToCharOrElseImpl(ToCharNullable inner, char value) {
super(inner);
this.value = value;
}
@Override
public char defaultValue() {
return value;
}
@Override
public char applyAsChar(T object) {
return inner.isNull(object) ? value :
inner.applyAsChar(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToCharOrElse)) return false;
final ToCharOrElse that = (ToCharOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToBooleanOrElse
booleanOrElse(ToBooleanNullable expression, boolean defaultValue) {
return new ToBooleanOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToBooleanNullable.orElse(boolean)}.
*
* @param the input entity type
*/
private final static class ToBooleanOrElseImpl
extends AbstractNonNullable>
implements ToBooleanOrElse {
private final boolean value;
private ToBooleanOrElseImpl(ToBooleanNullable inner, boolean value) {
super(inner);
this.value = value;
}
@Override
public boolean defaultValue() {
return value;
}
@Override
public boolean applyAsBoolean(T object) {
return inner.isNull(object) ? value :
inner.applyAsBoolean(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToBooleanOrElse)) return false;
final ToBooleanOrElse that = (ToBooleanOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
value == that.defaultValue();
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToStringOrElse
stringOrElse(ToStringNullable expression, String defaultValue) {
return new ToStringOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToStringNullable.orElse(String)}.
*
* @param the input entity type
*/
private final static class ToStringOrElseImpl
extends AbstractNonNullable>
implements ToStringOrElse {
private final String value;
private ToStringOrElseImpl(ToStringNullable inner, String value) {
super(inner);
this.value = value;
}
@Override
public String defaultValue() {
return value;
}
@Override
public String apply(T object) {
return inner.isNull(object) ? value :
inner.apply(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToStringOrElse)) return false;
final ToStringOrElse that = (ToStringOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
Objects.equals(value, that.defaultValue());
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @param the enum type
* @return the non-nullable expression
*/
public static > ToEnumOrElse
enumOrElse(ToEnumNullable expression, E defaultValue) {
return new ToEnumOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToEnumNullable.orElse(Enum)}.
*
* @param the input entity type
*/
private final static class ToEnumOrElseImpl>
extends AbstractNonNullable>
implements ToEnumOrElse {
private final E value;
private ToEnumOrElseImpl(ToEnumNullable inner, E value) {
super(inner);
this.value = value;
}
@Override
public Class enumClass() {
return inner.enumClass();
}
@Override
public E defaultValue() {
return value;
}
@Override
public E apply(T object) {
return inner.isNull(object) ? value :
inner.apply(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToEnumOrElse)) return false;
final ToEnumOrElse that = (ToEnumOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
Objects.equals(value, that.defaultValue());
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* 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}, then the default value is returned instead.
*
* @param expression the nullable expression to wrap
* @param defaultValue the default value to use
* @param the input entity type
* @return the non-nullable expression
*/
public static ToBigDecimalOrElse
bigDecimalOrElse(ToBigDecimalNullable expression, BigDecimal defaultValue) {
return new ToBigDecimalOrElseImpl<>(expression, defaultValue);
}
/**
* Internal class used when calling {@code ToBigDecimalNullable.orElse(BigDecimal)}.
*
* @param the input entity type
*/
private final static class ToBigDecimalOrElseImpl
extends AbstractNonNullable>
implements ToBigDecimalOrElse {
private final BigDecimal value;
private ToBigDecimalOrElseImpl(ToBigDecimalNullable inner, BigDecimal value) {
super(inner);
this.value = value;
}
@Override
public BigDecimal defaultValue() {
return value;
}
@Override
public BigDecimal apply(T object) {
return inner.isNull(object) ? value :
inner.apply(object);
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ToBigDecimalOrElse)) return false;
final ToBigDecimalOrElse that = (ToBigDecimalOrElse) o;
return Objects.equals(inner, that.innerNullable()) &&
Objects.equals(value, that.defaultValue());
}
@Override
public final int hashCode() {
return Objects.hash(inner, value);
}
}
/**
* Abstract base for a {@link NonNullableExpression}.
*
* @param the input type
* @param the wrapped nullable expression type
*/
private static abstract class AbstractNonNullable>
implements NonNullableExpression {
final INNER inner;
AbstractNonNullable(INNER inner) {
this.inner = requireNonNull(inner);
}
@Override
public final INNER innerNullable() {
return inner;
}
}
/**
* Utility classes should not be instantiated.
*/
private OrElseUtil() {}
}