com.speedment.runtime.compute.internal.expression.NegateUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime-deploy Show documentation
Show all versions of runtime-deploy Show documentation
A Speedment bundle that shades all dependencies into one jar. This is
useful when deploying an application on a server.
/**
*
* 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.ToBooleanNullableImpl;
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 negation of
* another expression.
*
* @author Emil Forslund
* @since 3.1.0
*/
public final class NegateUtil {
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte negateByte(ToByte expression) {
class NegateByte extends AbstractNegate> implements ToByte {
private NegateByte(ToByte inner) {
super(inner);
}
@Override
public byte applyAsByte(T object) {
return (byte) -inner.applyAsByte(object);
}
}
return new NegateByte(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToShort negateShort(ToShort expression) {
class NegateShort extends AbstractNegate> implements ToShort {
private NegateShort(ToShort inner) {
super(inner);
}
@Override
public short applyAsShort(T object) {
return (short) -inner.applyAsShort(object);
}
}
return new NegateShort(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToInt negateInt(ToInt expression) {
class NegateInt extends AbstractNegate> implements ToInt {
private NegateInt(ToInt inner) {
super(inner);
}
@Override
public int applyAsInt(T object) {
return -inner.applyAsInt(object);
}
}
return new NegateInt(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToLong negateLong(ToLong expression) {
class NegateLong extends AbstractNegate> implements ToLong {
private NegateLong(ToLong inner) {
super(inner);
}
@Override
public long applyAsLong(T object) {
return -inner.applyAsLong(object);
}
}
return new NegateLong(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToFloat negateFloat(ToFloat expression) {
class NegateFloat extends AbstractNegate> implements ToFloat {
private NegateFloat(ToFloat inner) {
super(inner);
}
@Override
public float applyAsFloat(T object) {
return -inner.applyAsFloat(object);
}
}
return new NegateFloat(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble negateDouble(ToDouble expression) {
class NegateDouble extends AbstractNegate> implements ToDouble {
private NegateDouble(ToDouble inner) {
super(inner);
}
@Override
public double applyAsDouble(T object) {
return -inner.applyAsDouble(object);
}
}
return new NegateDouble(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToBigDecimal negateBigDecimal(ToBigDecimal expression) {
class NegateBigDecimal implements ToBigDecimal, UnaryExpression> {
private final ToBigDecimal inner;
private NegateBigDecimal(ToBigDecimal inner) {
this.inner = inner;
}
@Override
public ToBigDecimal inner() {
return inner;
}
@Override
public Operator operator() {
return Operator.NEGATE;
}
@Override
public BigDecimal apply(T object) {
return inner.apply(object).negate();
}
}
return new NegateBigDecimal(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToBoolean negateBoolean(ToBoolean expression) {
class NegateBoolean extends AbstractNegate> implements ToBoolean {
private NegateBoolean(ToBoolean inner) {
super(inner);
}
@Override
public boolean applyAsBoolean(T object) {
return !inner.applyAsBoolean(object);
}
}
return new NegateBoolean(expression);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression. 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 negateByteOrNull(ToByteNullable expression) {
return new ToByteNullableImpl<>(
negateByte(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression. 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 negateShortOrNull(ToShortNullable expression) {
return new ToShortNullableImpl<>(
negateShort(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression. 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 negateIntOrNull(ToIntNullable expression) {
return new ToIntNullableImpl<>(
negateInt(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression. 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 negateLongOrNull(ToLongNullable expression) {
return new ToLongNullableImpl<>(
negateLong(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression. 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 negateFloatOrNull(ToFloatNullable expression) {
return new ToFloatNullableImpl<>(
negateFloat(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression. 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 negateDoubleOrNull(ToDoubleNullable expression) {
return new ToDoubleNullableImpl<>(
negateDouble(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that will compute the negative result
* of the specified expression. 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 ToBooleanNullable negateBooleanOrNull(ToBooleanNullable expression) {
return new ToBooleanNullableImpl<>(
negateBoolean(expression.orThrow()),
expression.isNull()
);
}
/**
* The abstract base for a negate expression.
*
* @param the input type
* @param the inner expression type
*/
private abstract static class AbstractNegate>
implements UnaryExpression {
final INNER inner;
private AbstractNegate(INNER inner) {
this.inner = requireNonNull(inner);
}
@Override
public final INNER inner() {
return inner;
}
@Override
public final Operator operator() {
return Operator.NEGATE;
}
@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 NegateUtil() {}
}