Example usage for org.apache.commons.functor.core.composite ConditionalBinaryFunction ConditionalBinaryFunction

List of usage examples for org.apache.commons.functor.core.composite ConditionalBinaryFunction ConditionalBinaryFunction

Introduction

In this page you can find the example usage for org.apache.commons.functor.core.composite ConditionalBinaryFunction ConditionalBinaryFunction.

Prototype

public ConditionalBinaryFunction(BinaryPredicate<? super L, ? super R> ifPred,
        BinaryFunction<? super L, ? super R, ? extends T> thenFunc,
        BinaryFunction<? super L, ? super R, ? extends T> elseFunc) 

Source Link

Document

Create a new ConditionalBinaryFunction.

Usage

From source file:org.apache.commons.functor.example.kata.four.DataMunger.java

/**
 * A BinaryFunction that will calculate the absolute
 * difference between col1 and col2 in the given
 * String arguments, and return the argument
 * whose difference is smallest.//from   w  w w  .  ja v  a 2 s.  co  m
 */
private static final BinaryFunction<String, String, String> lesserSpread(final int col1, final int col2) {
    return new ConditionalBinaryFunction<String, String, String>(IsNull.<String>left(), // if left is null
            RightIdentity.<String, String>function(), //   return right
            Conditional.function( //   else return the parameter with the least spread
                    Composite.predicate( //     if left is less than right
                            IsLessThan.instance(), absSpread(col1, col2), absSpread(col1, col2)),
                    LeftIdentity.<String, String>function(), //       return left
                    RightIdentity.<String, String>function() //       else return right
            ));
}

From source file:org.apache.commons.functor.example.map.PredicatedMap.java

public PredicatedMap(Map<K, V> map, final Predicate<K> keyPredicate, final Predicate<V> valuePredicate) {
    super(map);//from   w w  w.ja  v a2 s.  c  o m
    setOnPut(new ConditionalBinaryFunction<Map<K, V>, Object[], V>(new BinaryPredicate<Map<K, V>, Object[]>() {
        @SuppressWarnings("unchecked")
        public boolean test(Map<K, V> a, Object[] b) {
            return keyPredicate.test((K) Array.get(b, 0)) && valuePredicate.test((V) Array.get(b, 1));
        }
    }, DEFAULT_ON_PUT, BinaryProcedureBinaryFunction
            .<Map<K, V>, Object[], V>adapt(new Throw<Map<K, V>, Object>(new IllegalArgumentException()))));

    setOnPutAll(new BinaryProcedure<Map<K, V>, Map<K, V>>() {
        public void run(Map<K, V> dest, Map<K, V> src) {
            for (Iterator<Map.Entry<K, V>> iter = src.entrySet().iterator(); iter.hasNext();) {
                Map.Entry<K, V> pair = iter.next();
                if (keyPredicate.test(pair.getKey()) && valuePredicate.test(pair.getValue())) {
                    dest.put(pair.getKey(), pair.getValue());
                }
            }
        }
    });
}