Skip to content

Commit

Permalink
runtime-compute: Add HasMapToDouble trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed May 8, 2018
1 parent f085c05 commit 31ed3db
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import java.math.BigDecimal;
import static java.util.Objects.requireNonNull;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.UnaryOperator;

/**
* Expression that given an entity returns a non-null {@code String} value. This
* expression can be implemented using a lambda, or it can be a result of
* Expression that given an entity returns a non-null {@code BigDecimal} value.
* This expression can be implemented using a lambda, or it can be a result of
* another operation. It has additional methods for operating on it.
*
* @param <T> type to extract from
Expand All @@ -43,6 +44,7 @@ public interface ToBigDecimal<T>
HasMultiply<T, ToDouble<T>, ToDouble<T>, ToDouble<T>>,
HasDivide<T>,
HasMap<T, UnaryOperator<BigDecimal>, ToBigDecimal<T>>,
HasMapToDouble<T, ToDoubleFunction<BigDecimal>>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
Expand Down Expand Up @@ -284,6 +286,11 @@ default ToBigDecimal<T> map(UnaryOperator<BigDecimal> mapper) {
return MapperUtil.map(this, mapper);
}

@Override
default ToDouble<T> mapToDouble(ToDoubleFunction<BigDecimal> mapper) {
return MapperUtil.mapToDouble(this, mapper);
}

@Override
default long hash(T object) {
return apply(object).hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.util.function.Function;

/**
* Expression that given an entity returns a non-null {@code String} value. This
* expression can be implemented using a lambda, or it can be a result of
* Expression that given an entity returns a nullable {@code BigDecimal} value.
* This expression can be implemented using a lambda, or it can be a result of
* another operation. It has additional methods for operating on it.
*
* @param <T> type to extract from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.speedment.runtime.compute.trait.HasCompose;
import com.speedment.runtime.compute.trait.HasHash;
import com.speedment.runtime.compute.trait.HasMap;
import com.speedment.runtime.compute.trait.HasMapToDouble;

import java.math.BigDecimal;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -43,6 +44,7 @@ public interface ToBoolean<T>
HasAsInt<T>,
HasAsLong<T>,
HasMap<T, BooleanUnaryOperator, ToBoolean<T>>,
HasMapToDouble<T, BooleanToDoubleFunction>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
Expand Down Expand Up @@ -88,6 +90,7 @@ default ToLong<T> asLong() {
return CastUtil.castToLong(this);
}

@Override
default ToDouble<T> mapToDouble(BooleanToDoubleFunction operator) {
return MapperUtil.mapToDouble(this, operator);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.speedment.runtime.compute;

import com.speedment.common.function.BooleanToDoubleFunction;
import com.speedment.common.function.BooleanUnaryOperator;
import com.speedment.common.function.ByteToDoubleFunction;
import com.speedment.common.function.ByteUnaryOperator;
Expand Down Expand Up @@ -44,6 +45,7 @@ public interface ToByte<T>
HasMultiply<T, ToInt<T>, ToInt<T>, ToLong<T>>,
HasDivide<T>,
HasMap<T, ByteUnaryOperator, ToByte<T>>,
HasMapToDouble<T, ByteToDoubleFunction>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
Expand Down Expand Up @@ -89,6 +91,7 @@ default ToLong<T> asLong() {
return CastUtil.castToLong(this);
}

@Override
default ToDouble<T> mapToDouble(ByteToDoubleFunction operator) {
return MapperUtil.mapToDouble(this, operator);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.speedment.runtime.compute;

import com.speedment.common.function.BooleanToDoubleFunction;
import com.speedment.common.function.ByteToDoubleFunction;
import com.speedment.common.function.CharUnaryOperator;
import com.speedment.common.function.ToCharFunction;
import com.speedment.runtime.compute.expression.Expression;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.speedment.runtime.compute;

import com.speedment.common.function.BooleanUnaryOperator;
import com.speedment.common.function.ByteToDoubleFunction;
import com.speedment.common.function.FloatToDoubleFunction;
import com.speedment.common.function.FloatUnaryOperator;
import com.speedment.common.function.ToFloatFunction;
Expand Down Expand Up @@ -44,6 +45,7 @@ public interface ToFloat<T>
HasMultiply<T, ToFloat<T>, ToFloat<T>, ToDouble<T>>,
HasDivide<T>,
HasMap<T, FloatUnaryOperator, ToFloat<T>>,
HasMapToDouble<T, FloatToDoubleFunction>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
Expand Down Expand Up @@ -89,6 +91,7 @@ default ToLong<T> asLong() {
return CastUtil.castToLong(this);
}

@Override
default ToDouble<T> mapToDouble(FloatToDoubleFunction operator) {
return MapperUtil.mapToDouble(this, operator);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.speedment.runtime.compute;

import com.speedment.common.function.ByteToDoubleFunction;
import com.speedment.runtime.compute.expression.Expression;
import com.speedment.runtime.compute.expression.ExpressionType;
import com.speedment.runtime.compute.expression.Expressions;
Expand Down Expand Up @@ -43,6 +44,7 @@ public interface ToInt<T>
HasMultiply<T, ToLong<T>, ToLong<T>, ToLong<T>>,
HasDivide<T>,
HasMap<T, IntUnaryOperator, ToInt<T>>,
HasMapToDouble<T, IntToDoubleFunction>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
Expand Down Expand Up @@ -89,6 +91,7 @@ default ToLong<T> asLong() {
return CastUtil.castToLong(this);
}

@Override
default ToDouble<T> mapToDouble(IntToDoubleFunction operator) {
return MapperUtil.mapToDouble(this, operator);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.speedment.runtime.compute;

import com.speedment.common.function.ByteToDoubleFunction;
import com.speedment.runtime.compute.expression.Expression;
import com.speedment.runtime.compute.expression.ExpressionType;
import com.speedment.runtime.compute.expression.Expressions;
Expand Down Expand Up @@ -43,6 +44,7 @@ public interface ToLong<T>
HasMultiply<T, ToLong<T>, ToLong<T>, ToLong<T>>,
HasDivide<T>,
HasMap<T, LongUnaryOperator, ToLong<T>>,
HasMapToDouble<T, LongToDoubleFunction>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
Expand Down Expand Up @@ -89,6 +91,7 @@ default ToLong<T> asLong() {
return this;
}

@Override
default ToDouble<T> mapToDouble(LongToDoubleFunction operator) {
return MapperUtil.mapToDouble(this, operator);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.speedment.runtime.compute;

import com.speedment.common.function.BooleanUnaryOperator;
import com.speedment.common.function.ByteToDoubleFunction;
import com.speedment.common.function.ShortToDoubleFunction;
import com.speedment.common.function.ShortUnaryOperator;
import com.speedment.common.function.ToShortFunction;
Expand Down Expand Up @@ -44,6 +45,7 @@ public interface ToShort<T>
HasMultiply<T, ToInt<T>, ToInt<T>, ToLong<T>>,
HasDivide<T>,
HasMap<T, ShortUnaryOperator, ToShort<T>>,
HasMapToDouble<T, ShortToDoubleFunction>,
HasHash<T>,
HasCompare<T>,
HasCompose<T> {
Expand Down Expand Up @@ -90,6 +92,7 @@ default ToLong<T> asLong() {
return CastUtil.castToLong(this);
}

@Override
default ToDouble<T> mapToDouble(ShortToDoubleFunction operator) {
return MapperUtil.mapToDouble(this, operator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ enum MapperType {
/**
* Implemented as a {@link java.util.function.UnaryOperator}.
*/
BIG_DECIMAL_TO_DOUBLE,

/**
* Implemented as a {@link java.util.function.ToDoubleFunction}.
*/
BIG_DECIMAL_TO_BIG_DECIMAL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,30 @@ public MapperType mapperType() {
};
}

/**
* Returns an expression that first applies the specified expression to get
* a value, then applies the specified mapping operation to that value to
* get the final result.
*
* @param expression the expression to apply to the input
* @param mapper the mapper to apply to the result
* @param <T> the input type
* @return the new expression
*/
public static <T> ToDouble<T> mapToDouble(ToBigDecimal<T> expression, ToDoubleFunction<BigDecimal> mapper) {
return new ToDoubleMapper<T, ToBigDecimal<T>, ToDoubleFunction<BigDecimal>>(expression, mapper) {
@Override
public double applyAsDouble(T object) {
return this.mapper.applyAsDouble(this.inner.apply(object));
}

@Override
public MapperType mapperType() {
return MapperType.BIG_DECIMAL_TO_DOUBLE;
}
};
}

/**
* Abstract base for a mapping operation that results in a {@code byte}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.speedment.runtime.compute.trait;

import com.speedment.runtime.compute.ToDouble;

/**
* Trait for expressions that can be mapped to a new expression of the same type
* by supplying a mapping function.
*
* @param <T> the input entity type
* @param <MAPPER> the functional interface used for mapping values
*
* @author Emil Forslund
* @since 3.1.0
*/
public interface HasMapToDouble<T, MAPPER> {

/**
* Maps the result of this expression into a {@code double} by passing it to
* the specified {@code mapper}, producing a new expression that implements
* {@link ToDouble}.
*
* @param mapper the mapper to use on the result of this expression
* @return the new mapped expression
*/
ToDouble<T> mapToDouble(MAPPER mapper);

}

0 comments on commit 31ed3db

Please sign in to comment.