com.speedment.runtime.compute.ToEnumNullable Maven / Gradle / Ivy
/*
*
* 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;
import com.speedment.runtime.compute.expression.Expression;
import com.speedment.runtime.compute.expression.ExpressionType;
import com.speedment.runtime.compute.internal.ToEnumImpl;
import com.speedment.runtime.compute.internal.ToEnumNullableImpl;
import com.speedment.runtime.compute.internal.expression.ComposedUtil;
import com.speedment.runtime.compute.internal.expression.OrElseGetUtil;
import com.speedment.runtime.compute.internal.expression.OrElseThrowUtil;
import com.speedment.runtime.compute.internal.expression.OrElseUtil;
import com.speedment.runtime.compute.trait.HasCompare;
import com.speedment.runtime.compute.trait.HasCompose;
import com.speedment.runtime.compute.trait.HasHash;
import com.speedment.runtime.compute.trait.HasMapIfPresent;
import com.speedment.runtime.compute.trait.HasMapToDoubleIfPresent;
import com.speedment.runtime.compute.trait.ToNullable;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.UnaryOperator;
/**
* Expression that given an entity returns an {@code enum} value, or
* {@code null}. The expression also knows about the enum class and can
* therefore not be implemented as a lambda like the other expressions in this
* module.
*
* @param type to extract from
* @param enum type
*
* @see Function
*
* @author Emil Forslund
* @since 3.1.0
*/
public interface ToEnumNullable>
extends Expression,
ToNullable>,
HasMapToDoubleIfPresent>,
HasMapIfPresent, ToEnumNullable>,
HasHash,
HasCompare,
HasCompose {
/**
* Returns a typed {@code ToEnumNullable} using the provided
* {@code lambda}.
*
* @param type to extract from
* @param enum type
* @param lambda to convert
* @param enumClass class for the enum
* @return a typed {@code ToEnumNullable} using the provided
* {@code lambda}
*
* @throws NullPointerException if the provided {@code lambda} is
* {@code null}
*/
static > ToEnumNullable
of(Class enumClass, Function lambda) {
return new ToEnumNullableImpl<>(enumClass, lambda);
}
/**
* Returns the {@code class} of the enum that this expression returns.
*
* @return the enum class
*/
Class enumClass();
@Override
default ExpressionType expressionType() {
return ExpressionType.ENUM_NULLABLE;
}
/**
* Returns an alternative expression that represents the
* {@link Enum#ordinal()} of the enum that would otherwise have been
* returned by this expression.
*
* If this expression would have returned {@code null}, then the new
* expression will also evaluate to {@code null}.
*
* @return the ordinal expression
*/
default ToIntNullable asOrdinal() {
return t -> isNotNull(t)
? apply(t).ordinal()
: null;
}
/**
* Returns an alternative expression that represents the {@link Enum#name()}
* of the enum that would otherwise have been returned by this expression.
*
* If this expression would have returned {@code null}, then the new
* expression will also evaluate to {@code null}.
*
* @return the name expression
*/
default ToStringNullable asName() {
return t -> isNotNull(t)
? apply(t).name()
: null;
}
@Override
default ToEnum orThrow() {
return OrElseThrowUtil.enumOrElseThrow(this);
}
@Override
default ToEnum orElseGet(ToEnum getter) {
return OrElseGetUtil.enumOrElseGet(this, getter);
}
@Override
default ToEnum orElse(E value) {
return OrElseUtil.enumOrElse(this, value);
}
@Override
default ToDoubleNullable mapToDoubleIfPresent(ToDoubleFunction mapper) {
final ToEnumNullable delegate = this;
return new ToDoubleNullable() {
@Override
public Double apply(T object) {
return delegate.isNull(object) ? null
: mapper.applyAsDouble(delegate.apply(object));
}
@Override
public double applyAsDouble(T object) {
return mapper.applyAsDouble(delegate.apply(object));
}
@Override
public ToDouble orElseGet(ToDouble getter) {
return object -> delegate.isNull(object)
? getter.applyAsDouble(object)
: mapper.applyAsDouble(delegate.apply(object));
}
@Override
public ToDouble orElse(Double value) {
return object -> delegate.isNull(object)
? value
: mapper.applyAsDouble(delegate.apply(object));
}
@Override
public boolean isNull(T object) {
return delegate.isNull(object);
}
@Override
public boolean isNotNull(T object) {
return delegate.isNotNull(object);
}
};
}
@Override
default ToEnumNullable mapIfPresent(UnaryOperator mapper) {
final ToEnumNullable delegate = this;
return new ToEnumNullable() {
@Override
public Class enumClass() {
return delegate.enumClass();
}
@Override
public E apply(T object) {
return delegate.isNull(object) ? null
: mapper.apply(delegate.apply(object));
}
@Override
public ToEnum orElseGet(ToEnum getter) {
return new ToEnumImpl<>(
delegate.enumClass(),
object -> delegate.isNull(object)
? getter.apply(object)
: mapper.apply(delegate.apply(object))
);
}
@Override
public ToEnum orElse(E value) {
return new ToEnumImpl<>(
delegate.enumClass(),
object -> delegate.isNull(object)
? value
: mapper.apply(delegate.apply(object))
);
}
@Override
public boolean isNull(T object) {
return delegate.isNull(object);
}
@Override
public boolean isNotNull(T object) {
return delegate.isNotNull(object);
}
};
}
@Override
default long hash(T object) {
final E e = apply(object);
return e == null ? -1 : e.hashCode();
}
@Override
default int compare(T first, T second) {
final E a = apply(first);
final E b = apply(second);
if (a == null) {
return (b == null) ? -1 : 0;
} else {
return a.compareTo(b);
}
}
@Override
default ToEnumNullable compose(Function super V, ? extends T> before) {
@SuppressWarnings("unchecked")
final Function casted = (Function) before;
return ComposedUtil.composeToEnumNullable(casted, this);
}
}