com.speedment.runtime.compute.internal.expression.SignUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of generator-deploy Show documentation
Show all versions of generator-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.ToBigDecimal;
import com.speedment.runtime.compute.ToByte;
import com.speedment.runtime.compute.ToDouble;
import com.speedment.runtime.compute.ToFloat;
import com.speedment.runtime.compute.ToInt;
import com.speedment.runtime.compute.ToLong;
import com.speedment.runtime.compute.ToShort;
import com.speedment.runtime.compute.expression.Expression;
import com.speedment.runtime.compute.expression.UnaryExpression;
import java.util.Objects;
import static java.util.Objects.requireNonNull;
/**
* Utility class for creating expressions that gives the sign ({@code 1},
* {@code -1} or {@code 0}) depending on if the result of another expression is
* positive, negative or {@code 0}.
*
* @author Emil Forslund
* @since 3.1.0
*/
public final class SignUtil {
private final static byte NEGATIVE = -1, POSITIVE = 1, ZERO = 0;
/**
* Returns an expression that wraps another expression and returns
* {@code -1} if its result is negative, {@code 1} if its result is positive
* and {@code 0} if its result is equal to {@code 0}.
*
* @param expression the expression to wrap
* @param the input entity type
* @return sign of the result of the wrapped expression
*/
public static ToByte signBigDecimal(ToBigDecimal expression) {
class BigDecimalSign extends AbstractSign> {
private BigDecimalSign(ToBigDecimal tToBigDecimal) {
super(tToBigDecimal);
}
@Override
public byte applyAsByte(T object) {
return (byte) inner.apply(object).signum();
}
}
return new BigDecimalSign(expression);
}
/**
* Returns an expression that wraps another expression and returns
* {@code -1} if its result is negative, {@code 1} if its result is positive
* and {@code 0} if its result is equal to {@code 0}.
*
* @param expression the expression to wrap
* @param the input entity type
* @return sign of the result of the wrapped expression
*/
public static ToByte signDouble(ToDouble expression) {
class DoubleSign extends AbstractSign> {
private DoubleSign(ToDouble tToDouble) {
super(tToDouble);
}
@Override
public byte applyAsByte(T object) {
final double value = inner.applyAsDouble(object);
return value < 0 ? NEGATIVE : (value > 0 ? POSITIVE : ZERO);
}
}
return new DoubleSign(expression);
}
/**
* Returns an expression that wraps another expression and returns
* {@code -1} if its result is negative, {@code 1} if its result is positive
* and {@code 0} if its result is equal to {@code 0}.
*
* @param expression the expression to wrap
* @param the input entity type
* @return sign of the result of the wrapped expression
*/
public static ToByte signFloat(ToFloat expression) {
class FloatSign extends AbstractSign> {
private FloatSign(ToFloat tToFloat) {
super(tToFloat);
}
@Override
public byte applyAsByte(T object) {
final float value = inner.applyAsFloat(object);
return value < 0 ? NEGATIVE : (value > 0 ? POSITIVE : ZERO);
}
}
return new FloatSign(expression);
}
/**
* Returns an expression that wraps another expression and returns
* {@code -1} if its result is negative, {@code 1} if its result is positive
* and {@code 0} if its result is equal to {@code 0}.
*
* @param expression the expression to wrap
* @param the input entity type
* @return sign of the result of the wrapped expression
*/
public static ToByte signLong(ToLong expression) {
class LongSign extends AbstractSign> {
private LongSign(ToLong tToLong) {
super(tToLong);
}
@Override
public byte applyAsByte(T object) {
final long value = inner.applyAsLong(object);
return value < 0 ? NEGATIVE : (value > 0 ? POSITIVE : ZERO);
}
}
return new LongSign(expression);
}
/**
* Returns an expression that wraps another expression and returns
* {@code -1} if its result is negative, {@code 1} if its result is positive
* and {@code 0} if its result is equal to {@code 0}.
*
* @param expression the expression to wrap
* @param the input entity type
* @return sign of the result of the wrapped expression
*/
public static ToByte signInt(ToInt expression) {
class IntSign extends AbstractSign> {
private IntSign(ToInt tToInt) {
super(tToInt);
}
@Override
public byte applyAsByte(T object) {
final int value = inner.applyAsInt(object);
return value < 0 ? NEGATIVE : (value > 0 ? POSITIVE : ZERO);
}
}
return new IntSign(expression);
}
/**
* Returns an expression that wraps another expression and returns
* {@code -1} if its result is negative, {@code 1} if its result is positive
* and {@code 0} if its result is equal to {@code 0}.
*
* @param expression the expression to wrap
* @param the input entity type
* @return sign of the result of the wrapped expression
*/
public static ToByte signShort(ToShort expression) {
class ShortSign extends AbstractSign> {
private ShortSign(ToShort tToShort) {
super(tToShort);
}
@Override
public byte applyAsByte(T object) {
final short value = inner.applyAsShort(object);
return value < 0 ? NEGATIVE : (value > 0 ? POSITIVE : ZERO);
}
}
return new ShortSign(expression);
}
/**
* Returns an expression that wraps another expression and returns
* {@code -1} if its result is negative, {@code 1} if its result is positive
* and {@code 0} if its result is equal to {@code 0}.
*
* @param expression the expression to wrap
* @param the input entity type
* @return sign of the result of the wrapped expression
*/
public static ToByte signByte(ToByte expression) {
class ByteSign extends AbstractSign> {
private ByteSign(ToByte tToByte) {
super(tToByte);
}
@Override
public byte applyAsByte(T object) {
final byte value = inner.applyAsByte(object);
return value < 0 ? NEGATIVE : (value > 0 ? POSITIVE : ZERO);
}
}
return new ByteSign(expression);
}
/**
* Internal base implementation for a sign-operation.
*
* @param the input entity type
* @param the inner expression type
*/
private abstract static class AbstractSign>
implements UnaryExpression, ToByte {
final INNER inner;
AbstractSign(INNER inner) {
this.inner = requireNonNull(inner);
}
@Override
public final INNER inner() {
return inner;
}
@Override
public final Operator operator() {
return Operator.SIGN;
}
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UnaryExpression)) return false;
final UnaryExpression that = (UnaryExpression) o;
return Objects.equals(inner(), that.inner())
&& Objects.equals(operator(), that.operator());
}
@Override
public final int hashCode() {
return Objects.hash(inner(), operator());
}
}
/**
* Utility classes should not be instantiated.
*/
private SignUtil() {}
}