Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
*
* Copyright (c) 2006-2020, 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.expression;
import com.speedment.runtime.compute.*;
import com.speedment.runtime.compute.internal.BinaryJoiningExpressionImpl;
import com.speedment.runtime.compute.internal.JoiningExpressionImpl;
import com.speedment.runtime.compute.internal.ToByteNullableImpl;
import com.speedment.runtime.compute.internal.ToDoubleNullableImpl;
import com.speedment.runtime.compute.internal.expression.AbsUtil;
import com.speedment.runtime.compute.internal.expression.DivideUtil;
import com.speedment.runtime.compute.internal.expression.MinusUtil;
import com.speedment.runtime.compute.internal.expression.MultiplyUtil;
import com.speedment.runtime.compute.internal.expression.NegateUtil;
import com.speedment.runtime.compute.internal.expression.PlusUtil;
import com.speedment.runtime.compute.internal.expression.PowUtil;
import com.speedment.runtime.compute.internal.expression.SignUtil;
import com.speedment.runtime.compute.internal.expression.SqrtUtil;
import static java.util.Arrays.asList;
/**
* Common mathematical expressions often used on Speedment entities.
*
* @author Emil Forslund
* @since 3.1.0
*/
@SuppressWarnings("overloads")
public final class Expressions {
/**
* Utility classes should not be instantiated.
*/
private Expressions() {throw new UnsupportedOperationException();}
////////////////////////////////////////////////////////////////////////////
// Conversions //
////////////////////////////////////////////////////////////////////////////
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDouble byteToDouble(ToByte expression) {
return expression.asDouble();
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDouble shortToDouble(ToShort expression) {
return expression.asDouble();
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDouble intToDouble(ToInt expression) {
return expression.asDouble();
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDouble longToDouble(ToLong expression) {
return expression.asDouble();
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDouble floatToDouble(ToFloat expression) {
return expression.asDouble();
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting. If the result of
* the input is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable byteToDoubleNullable(ToByteNullable expression) {
return expression.mapToDoubleIfPresent(b -> b);
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting. If the result of
* the input is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable shortToDoubleNullable(ToShortNullable expression) {
return expression.mapToDoubleIfPresent(b -> b);
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting. If the result of
* the input is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable intToDoubleNullable(ToIntNullable expression) {
return expression.mapToDoubleIfPresent(b -> b);
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting. If the result of
* the input is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable longToDoubleNullable(ToLongNullable expression) {
return expression.mapToDoubleIfPresent(b -> b);
}
/**
* Creates and returns an expression that converts the result of the
* specified expression into a {@code double} by casting. If the result of
* the input is {@code null}, then the new expression will also return
* {@code null}.
*
* @param expression the initial expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable floatToDoubleNullable(ToFloatNullable expression) {
return expression.mapToDoubleIfPresent(b -> b);
}
////////////////////////////////////////////////////////////////////////////
// Absolute Value //
////////////////////////////////////////////////////////////////////////////
/**
* 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 abs(ToByte expression) {
return AbsUtil.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 abs(ToShort expression) {
return AbsUtil.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 abs(ToInt expression) {
return AbsUtil.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 abs(ToLong expression) {
return AbsUtil.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 abs(ToFloat expression) {
return AbsUtil.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 abs(ToDouble expression) {
return AbsUtil.absDouble(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 absOrNull(ToByteNullable expression) {
return AbsUtil.absByteOrNull(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 ToShortNullable absOrNull(ToShortNullable expression) {
return AbsUtil.absShortOrNull(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 ToIntNullable absOrNull(ToIntNullable expression) {
return AbsUtil.absIntOrNull(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 ToLongNullable absOrNull(ToLongNullable expression) {
return AbsUtil.absLongOrNull(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 ToFloatNullable absOrNull(ToFloatNullable expression) {
return AbsUtil.absFloatOrNull(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 ToDoubleNullable absOrNull(ToDoubleNullable expression) {
return AbsUtil.absDoubleOrNull(expression);
}
////////////////////////////////////////////////////////////////////////////
// Get The Sign //
////////////////////////////////////////////////////////////////////////////
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte sign(ToByte expression) {
return SignUtil.signByte(expression);
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte sign(ToShort expression) {
return SignUtil.signShort(expression);
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte sign(ToInt expression) {
return SignUtil.signInt(expression);
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte sign(ToLong expression) {
return SignUtil.signLong(expression);
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte sign(ToFloat expression) {
return SignUtil.signFloat(expression);
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte sign(ToDouble expression) {
return SignUtil.signDouble(expression);
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByte sign(ToBigDecimal expression) {
return SignUtil.signBigDecimal(expression);
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}. If the result of the given expression is
* {@code null}, then the result of this expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByteNullable signOrNull(ToByteNullable expression) {
return new ToByteNullableImpl<>(
SignUtil.signByte(expression.orThrow()),
expression.isNull());
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}. If the result of the given expression is
* {@code null}, then the result of this expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByteNullable signOrNull(ToShortNullable expression) {
return new ToByteNullableImpl<>(
SignUtil.signShort(expression.orThrow()),
expression.isNull());
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}. If the result of the given expression is
* {@code null}, then the result of this expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByteNullable signOrNull(ToIntNullable expression) {
return new ToByteNullableImpl<>(
SignUtil.signInt(expression.orThrow()),
expression.isNull());
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}. If the result of the given expression is
* {@code null}, then the result of this expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByteNullable signOrNull(ToLongNullable expression) {
return new ToByteNullableImpl<>(
SignUtil.signLong(expression.orThrow()),
expression.isNull());
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}. If the result of the given expression is
* {@code null}, then the result of this expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByteNullable signOrNull(ToFloatNullable expression) {
return new ToByteNullableImpl<>(
SignUtil.signFloat(expression.orThrow()),
expression.isNull());
}
/**
* Creates and returns an expression that returns {@code 1} if the result of
* the input expression is positive, {@code -1} if the result of the input
* expression is negative and {@code 0} if the result of the input
* expression is {@code 0}. If the result of the given expression is
* {@code null}, then the result of this expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToByteNullable signOrNull(ToDoubleNullable expression) {
return new ToByteNullableImpl<>(
SignUtil.signDouble(expression.orThrow()),
expression.isNull());
}
////////////////////////////////////////////////////////////////////////////
// Square Root //
////////////////////////////////////////////////////////////////////////////
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble sqrt(ToByte expression) {
return SqrtUtil.sqrtByte(expression);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble sqrt(ToShort expression) {
return SqrtUtil.sqrtShort(expression);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble sqrt(ToInt expression) {
return SqrtUtil.sqrtInt(expression);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble sqrt(ToLong expression) {
return SqrtUtil.sqrtLong(expression);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble sqrt(ToFloat expression) {
return SqrtUtil.sqrtFloat(expression);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDouble sqrt(ToDouble expression) {
return SqrtUtil.sqrtDouble(expression);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression. If the result of the input expression
* is {@code null}, then the result of the new expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable sqrtOrNull(ToByteNullable expression) {
return new ToDoubleNullableImpl<>(
SqrtUtil.sqrtByte(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression. If the result of the input expression
* is {@code null}, then the result of the new expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable sqrtOrNull(ToShortNullable expression) {
return new ToDoubleNullableImpl<>(
SqrtUtil.sqrtShort(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression. If the result of the input expression
* is {@code null}, then the result of the new expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable sqrtOrNull(ToIntNullable expression) {
return new ToDoubleNullableImpl<>(
SqrtUtil.sqrtInt(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression. If the result of the input expression
* is {@code null}, then the result of the new expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable sqrtOrNull(ToLongNullable expression) {
return new ToDoubleNullableImpl<>(
SqrtUtil.sqrtLong(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression. If the result of the input expression
* is {@code null}, then the result of the new expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable sqrtOrNull(ToFloatNullable expression) {
return new ToDoubleNullableImpl<>(
SqrtUtil.sqrtFloat(expression.orThrow()),
expression.isNull()
);
}
/**
* Creates and returns an expression that returns the square root of the
* result from the input expression. If the result of the input expression
* is {@code null}, then the result of the new expression will also be
* {@code null}.
*
* @param expression the input expression
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable sqrtOrNull(ToDoubleNullable expression) {
return new ToDoubleNullableImpl<>(
SqrtUtil.sqrtDouble(expression.orThrow()),
expression.isNull()
);
}
////////////////////////////////////////////////////////////////////////////
// Power //
////////////////////////////////////////////////////////////////////////////
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToByte expression, int power) {
return PowUtil.bytePowInt(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToByte expression, double power) {
return PowUtil.bytePowDouble(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself as many times as the result of
* applying {@code power} to the input.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToByte expression, ToInt power) {
return PowUtil.bytePowInt(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself as many times as the result of
* applying {@code power} to the input.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToByte expression, ToDouble power) {
return PowUtil.bytePowDouble(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable
powOrNull(ToByteNullable expression, int power) {
return new ToDoubleNullableImpl<>(
PowUtil.bytePowInt(expression.orThrow(), power),
expression.isNull()
);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable
powOrNull(ToByteNullable expression, double power) {
return new ToDoubleNullableImpl<>(
PowUtil.bytePowDouble(expression.orThrow(), power),
expression.isNull()
);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself as many times as the result of
* applying {@code power} to the input.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable
powOrNull(ToByteNullable expression, ToInt power) {
return new ToDoubleNullableImpl<>(
PowUtil.bytePowInt(expression.orThrow(), power),
expression.isNull()
);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself as many times as the result of
* applying {@code power} to the input.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable
powOrNull(ToByteNullable expression, ToDouble power) {
return new ToDoubleNullableImpl<>(
PowUtil.bytePowDouble(expression.orThrow(), power),
expression.isNull()
);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToShort expression, int power) {
return PowUtil.shortPowInt(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToShort expression, double power) {
return PowUtil.shortPowDouble(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself as many times as the result of
* applying {@code power} to the input.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToShort expression, ToInt power) {
return PowUtil.shortPowInt(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself as many times as the result of
* applying {@code power} to the input.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDouble pow(ToShort expression, ToDouble power) {
return PowUtil.shortPowDouble(expression, power);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable
powOrNull(ToShortNullable expression, int power) {
return new ToDoubleNullableImpl<>(
PowUtil.shortPowInt(expression.orThrow(), power),
expression.isNull()
);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself {@code power} times.
*
* @param expression the input expression
* @param power the power
* @param the input type
* @return the new expression
*/
public static ToDoubleNullable
powOrNull(ToShortNullable expression, double power) {
return new ToDoubleNullableImpl<>(
PowUtil.shortPowDouble(expression.orThrow(), power),
expression.isNull()
);
}
/**
* Creates and returns an expression that takes the result of an input
* expression and multiplies it with itself as many times as the result of
* applying {@code power} to the input.
*
* @param expression the input expression
* @param power the power
* @param