All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.op4j.functions.FnBoolean Maven / Gradle / Ivy

The newest version!
/*
 * =============================================================================
 * 
 *   Copyright (c) 2010, The OP4J team (http://www.op4j.org)
 * 
 *   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 org.op4j.functions;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.commons.lang.Validate;
import org.javaruntype.type.Type;
import org.javaruntype.type.Types;
import org.op4j.exceptions.ExecutionException;

/**
 * 
 * @since 1.0
 * 
 * @author Soraya Sánchez
 *
 */
public final class FnBoolean {

    
    private static final ToNumber TO_BIG_DECIMAL = new ToNumber(Types.BIG_DECIMAL);
    private static final ToNumber TO_BIG_INTEGER = new ToNumber(Types.BIG_INTEGER);
    private static final ToNumber TO_DOUBLE = new ToNumber(Types.DOUBLE);
    private static final ToNumber TO_FLOAT = new ToNumber(Types.FLOAT);
    private static final ToNumber TO_LONG = new ToNumber(Types.LONG);
    private static final ToNumber TO_INTEGER = new ToNumber(Types.INTEGER);
    private static final ToNumber TO_SHORT = new ToNumber(Types.SHORT);
    private static final ToNumber TO_BYTE = new ToNumber(Types.BYTE);
    private static final Negate NEGATE = new Negate();
    
    @SuppressWarnings("unchecked")
    private static final Function IS_TRUE = (Function) (Function) eq(true);
    
    @SuppressWarnings("unchecked")
    private static final Function IS_FALSE = (Function) (Function) eq(false);

    
    
	private FnBoolean() {
		super();           
	}

	
	
	
	

	/**
	 * 

* Converts a Boolean to a BigDecimal. true = 1, false = 0. *

* * @return a BigDecimal representing the boolean target. */ public static final Function toBigDecimal() { return TO_BIG_DECIMAL; } /** *

* Converts a Boolean to a BigInteger. true = 1, false = 0. *

* * @return a BigInteger representing the boolean target. */ public static final Function toBigInteger() { return TO_BIG_INTEGER; } /** *

* Converts a Boolean to a Double. true = 1, false = 0. *

* * @return a Double representing the boolean target. */ public static final Function toDouble() { return TO_DOUBLE; } /** *

* Converts a Boolean to a Float. true = 1, false = 0. *

* * @return a Float representing the boolean target. */ public static final Function toFloat() { return TO_FLOAT; } /** *

* Converts a Boolean to a Long. true = 1, false = 0. *

* * @return a Long representing the boolean target. */ public static final Function toLong() { return TO_LONG; } /** *

* Converts a Boolean to a Integer. true = 1, false = 0. *

* * @return a Integer representing the boolean target. */ public static final Function toInteger() { return TO_INTEGER; } /** *

* Converts a Boolean to a Short. true = 1, false = 0. *

* * @return a Short representing the boolean target. */ public static final Function toShort() { return TO_SHORT; } /** *

* Converts a Boolean to a Byte. true = 1, false = 0. *

* * @return a Byte representing the boolean target. */ public static final Function toByte() { return TO_BYTE; } /** *

* Negates the target Boolean, returning Boolean.FALSE if the * target object is Boolean.TRUE, and Boolean.TRUE if it is Boolean.FALSE. *

* * @return the negation of the target object. */ public static final Function negate() { return NEGATE; } /** *

* Determines whether the target object and the specified object are equal * by calling the equals method on the target object. *

* * @param object the object to compare to the target * @return true if both objects are equal, false if not. */ public static final Function eq(final Boolean object) { return FnObject.eq(object); } /** *

* Determines whether the target object and the specified object are equal * by calling the equals method on the target object. *

* * @param object the object to compare to the target * @return true if both objects are equal, false if not. */ public static final Function eq(final boolean object) { return FnObject.eq(object); } /** *

* Determines whether the target object and the specified object are NOT equal * by calling the equals method on the target object. *

* * @param object the object to compare to the target * @return false if both objects are equal, true if not. */ public static final Function notEq(final Boolean object) { return FnObject.notEq(object); } /** *

* Determines whether the target object and the specified object are NOT equal * by calling the equals method on the target object. *

* * @param object the object to compare to the target * @return false if both objects are equal, true if not. */ public static final Function notEq(final boolean object) { return FnObject.notEq(object); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are equal * by calling the equals method. *

* * @param object the object to compare to the target * @return true if both objects are equal, false if not. */ public static final Function eqBy(final IFunction by, final Boolean object) { return FnFunc.chain(by, eq(object)); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are equal * by calling the equals method. *

* * @param object the object to compare to the target * @return true if both objects are equal, false if not. */ public static final Function eqBy(final IFunction by, final boolean object) { return FnFunc.chain(by, eq(object)); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are NOT equal * by calling the equals method. *

* * @param object the object to compare to the target * @return false if both objects are equal, true if not. */ public static final Function notEqBy(final IFunction by, final Boolean object) { return FnFunc.chain(by, notEq(object)); } /** *

* Determines whether the result of executing the specified function * on the target object and the specified object parameter are NOT equal * by calling the equals method. *

* * @param object the object to compare to the target * @return false if both objects are equal, true if not. */ public static final Function notEqBy(final IFunction by, final boolean object) { return FnFunc.chain(by, notEq(object)); } /** *

* Returns true if the target object is Boolean.TRUE, false * if it is Boolean.FALSE. *

* * @return true if the target object is Boolean.TRUE, false if Boolean.FALSE */ public static final Function isTrue() { return IS_TRUE; } /** *

* Returns true if the target object is Boolean.FALSE, false * if it is Boolean.TRUE. *

* * @return true if the target object is Boolean.FALSE, false if Boolean.TRUE */ public static final Function isFalse() { return IS_FALSE; } /** *

* Takes two boolean functions (Function<?,Boolean>) as * parameters and returns another one which returns true if both functions * return true, and false if not. *

* * @param left the left side of the "and" operation * @param right the right side of the "and" operation * @return a function returning true if both functions return true, and false if not. */ public static final Function and(final IFunction left, final IFunction right) { return new And(left, right); } /** *

* Takes two boolean functions (Function<?,Boolean>) as * parameters and returns another one which returns true if any of the functions * returns true, and false if not. *

* * @param left the left side of the "or" operation * @param right the right side of the "or" operation * @return a function returning true if any of the functions return true, and false if not. */ public static final Function or(final IFunction left, final IFunction right) { return new Or(left, right); } /** *

* Takes a boolean function (Function<?,Boolean>) as a * parameter and returns another one which returns true if the specified function * returns false, and false if the function returns true. *

* * @param function the function to be negated * @return a function returning true if the specified function returns false, and false if * it returns true. */ public static final Function not(final IFunction function) { return new Not(function); } /** *

* Determines whether the target object is null or not. *

* * @return true if the target object is null, false if not. */ public static final Function isNull() { return FnObject.isNull(); } /** *

* Determines whether the target object is null or not. *

* * @return false if the target object is null, true if not. */ public static final Function isNotNull() { return FnObject.isNotNull(); } /** *

* Determines whether the result of executing the * specified function on the target object is null or not. *

* * @return true if the function result is null, false if not. */ public static final Function isNullBy(final IFunction by) { return FnFunc.chain(by, FnObject.isNull()); } /** *

* Determines whether the result of executing the * specified function on the target object is null or not. *

* * @return false if the function result is null, true if not. */ public static final Function isNotNullBy(final IFunction by) { return FnFunc.chain(by, FnObject.isNotNull()); } static final class ToNumber extends AbstractNullAsNullFunction { private final Number trueValue; private final Number falseValue; protected ToNumber(final Type type) { super(); if (type.equals(Types.BIG_DECIMAL)) { this.trueValue = BigDecimal.valueOf(1); this.falseValue = BigDecimal.valueOf(0); } else if (type.equals(Types.BIG_INTEGER)) { this.trueValue = BigInteger.valueOf(1); this.falseValue = BigInteger.valueOf(0); } else if (type.equals(Types.DOUBLE)) { this.trueValue = Double.valueOf(1); this.falseValue = Double.valueOf(0); } else if (type.equals(Types.FLOAT)) { this.trueValue = Float.valueOf(1); this.falseValue = Float.valueOf(0); } else if (type.equals(Types.LONG)) { this.trueValue = Long.valueOf(1); this.falseValue = Long.valueOf(0); } else if (type.equals(Types.INTEGER)) { this.trueValue = Integer.valueOf(1); this.falseValue = Integer.valueOf(0); } else if (type.equals(Types.SHORT)) { this.trueValue = Short.valueOf((short)1); this.falseValue = Short.valueOf((short)0); } else if (type.equals(Types.BYTE)) { this.trueValue = Byte.valueOf((byte)1); this.falseValue = Byte.valueOf((byte)0); } else { throw new ExecutionException("Unsupported type \"" + type + "\""); } } @Override @SuppressWarnings("unchecked") public final X nullAsNullExecute(final Boolean number, final ExecCtx ctx) throws Exception { return (number.booleanValue()? (X) this.trueValue : (X) this.falseValue); } } static class Negate extends AbstractNullAsNullFunction { public Negate() { super(); } @Override protected Boolean nullAsNullExecute(final Boolean object, final ExecCtx ctx) throws Exception { return (object.booleanValue()? Boolean.FALSE : Boolean.TRUE); } } static class And extends Function { private final IFunction left; private final IFunction right; public And(final IFunction left, final IFunction right) { super(); Validate.notNull(left, "Null function found: None of the specified functions can be null"); Validate.notNull(right, "Null function found: None of the specified functions can be null"); this.left = left; this.right = right; } public Boolean execute(final R object, final ExecCtx ctx) throws Exception { Boolean result = this.left.execute(object, ctx); if (result == null) { throw new ExecutionException("Evaluation function returned null, which is " + "not allowed executing \"and\""); } if (!result.booleanValue()) { return Boolean.FALSE; } result = this.right.execute(object, ctx); if (result == null) { throw new ExecutionException("Evaluation function returned null, which is " + "not allowed executing \"and\""); } if (!result.booleanValue()) { return Boolean.FALSE; } return Boolean.TRUE; } } static class Or extends Function { private final IFunction left; private final IFunction right; public Or(final IFunction left, final IFunction right) { super(); Validate.notNull(left, "Null function found: None of the specified functions can be null"); Validate.notNull(right, "Null function found: None of the specified functions can be null"); this.left = left; this.right = right; } public Boolean execute(final R object, final ExecCtx ctx) throws Exception { Boolean result = this.left.execute(object, ctx); if (result == null) { throw new ExecutionException("Evaluation function returned null, which is " + "not allowed executing \"or\""); } if (result.booleanValue()) { return Boolean.TRUE; } result = this.right.execute(object, ctx); if (result == null) { throw new ExecutionException("Evaluation function returned null, which is " + "not allowed executing \"or\""); } if (result.booleanValue()) { return Boolean.TRUE; } return Boolean.FALSE; } } static class Not extends Function { private final IFunction function; public Not(final IFunction function) { super(); Validate.notNull(function, "Null function found: None of the specified functions can be null"); this.function = function; } public Boolean execute(final T object, final ExecCtx ctx) throws Exception { final Boolean result = this.function.execute(object, ctx); if (result == null) { throw new ExecutionException("Evaluation function returned null, which is " + "not allowed executing \"or\""); } if (result.booleanValue()) { return Boolean.FALSE; } return Boolean.TRUE; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy