com.speedment.runtime.compute.internal.expression.AbsUtil Maven / Gradle / Ivy
/**
*
* 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.UnaryExpression;
import com.speedment.runtime.compute.internal.ToByteNullableImpl;
import com.speedment.runtime.compute.internal.ToDoubleNullableImpl;
import com.speedment.runtime.compute.internal.ToFloatNullableImpl;
import com.speedment.runtime.compute.internal.ToIntNullableImpl;
import com.speedment.runtime.compute.internal.ToLongNullableImpl;
import com.speedment.runtime.compute.internal.ToShortNullableImpl;
import java.math.BigDecimal;
import java.util.Objects;
import static java.util.Objects.requireNonNull;
/**
* Utility class used to construct expression that gives the absolute value of
* another expression.
*
* @author Emil Forslund
* @since 3.1.0
*/
public final class AbsUtil {
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any).
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte absByte(ToByte expression) {
class AbsByte extends AbstractAbs> implements ToByte {
private AbsByte(ToByte inner) {
super(inner);
}
@Override
public byte applyAsByte(T object) {
final byte value = inner.applyAsByte(object);
return value < 0 ? (byte) -value : value;
}
}
return new AbsByte(expression);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any).
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToShort absShort(ToShort expression) {
class AbsShort extends AbstractAbs> implements ToShort {
private AbsShort(ToShort inner) {
super(inner);
}
@Override
public short applyAsShort(T object) {
final short value = inner.applyAsShort(object);
return value < 0 ? (short) -value : value;
}
}
return new AbsShort(expression);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any).
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToInt absInt(ToInt expression) {
class AbsInt extends AbstractAbs> implements ToInt {
private AbsInt(ToInt inner) {
super(inner);
}
@Override
public int applyAsInt(T object) {
final int value = inner.applyAsInt(object);
return value < 0 ? -value : value;
}
}
return new AbsInt(expression);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any).
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToLong absLong(ToLong expression) {
class AbsLong extends AbstractAbs> implements ToLong {
private AbsLong(ToLong inner) {
super(inner);
}
@Override
public long applyAsLong(T object) {
final long value = inner.applyAsLong(object);
return value < 0 ? -value : value;
}
}
return new AbsLong(expression);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any).
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToFloat absFloat(ToFloat expression) {
class AbsFloat extends AbstractAbs> implements ToFloat {
private AbsFloat(ToFloat inner) {
super(inner);
}
@Override
public float applyAsFloat(T object) {
final float value = inner.applyAsFloat(object);
return value < 0 ? -value : value;
}
}
return new AbsFloat(expression);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any).
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble absDouble(ToDouble expression) {
class AbsDouble extends AbstractAbs> implements ToDouble {
private AbsDouble(ToDouble inner) {
super(inner);
}
@Override
public double applyAsDouble(T object) {
final double value = inner.applyAsDouble(object);
return value < 0 ? -value : value;
}
}
return new AbsDouble(expression);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any).
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToBigDecimal absBigDecimal(ToBigDecimal expression) {
class AbsBigDecimal extends AbstractAbs> implements ToBigDecimal {
private AbsBigDecimal(ToBigDecimal inner) {
super(inner);
}
@Override
public BigDecimal apply(T object) {
return inner.apply(object).abs();
}
}
return new AbsBigDecimal(expression);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any). If the result of the original
* expression is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByteNullable absByteOrNull(ToByteNullable expression) {
return new ToByteNullableImpl<>(
absByte(expression.orThrow()),
expression.isNull()
);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any). If the result of the original
* expression is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToShortNullable absShortOrNull(ToShortNullable expression) {
return new ToShortNullableImpl<>(
absShort(expression.orThrow()),
expression.isNull()
);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any). If the result of the original
* expression is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToIntNullable absIntOrNull(ToIntNullable expression) {
return new ToIntNullableImpl<>(
absInt(expression.orThrow()),
expression.isNull()
);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any). If the result of the original
* expression is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToLongNullable absLongOrNull(ToLongNullable expression) {
return new ToLongNullableImpl<>(
absLong(expression.orThrow()),
expression.isNull()
);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any). If the result of the original
* expression is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToFloatNullable absFloatOrNull(ToFloatNullable expression) {
return new ToFloatNullableImpl<>(
absFloat(expression.orThrow()),
expression.isNull()
);
}
/**
* Returns an expression that takes an expression and returns its absolute
* (removing the negation sign if any). If the result of the original
* expression is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable absDoubleOrNull(ToDoubleNullable expression) {
return new ToDoubleNullableImpl<>(
absDouble(expression.orThrow()),
expression.isNull()
);
}
/**
* The abstract base for an absolute expression.
*
* @param the input entity type
* @param the inner expression type
*/
private abstract static class AbstractAbs>
implements UnaryExpression {
final INNER inner;
private AbstractAbs(INNER inner) {
this.inner = requireNonNull(inner);
}
@Override
public final INNER inner() {
return inner;
}
@Override
public final Operator operator() {
return Operator.ABS;
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
else if (!(o instanceof UnaryExpression)) return false;
final UnaryExpression that = (UnaryExpression) o;
return Objects.equals(inner(), that.inner())
&& operator().equals(that.operator());
}
@Override
public final int hashCode() {
return Objects.hash(inner(), operator());
}
}
/**
* Utility classes should not be instantiated.
*/
private AbsUtil() {}
}