Example usage for org.apache.commons.functor BinaryProcedure BinaryProcedure

List of usage examples for org.apache.commons.functor BinaryProcedure BinaryProcedure

Introduction

In this page you can find the example usage for org.apache.commons.functor BinaryProcedure BinaryProcedure.

Prototype

BinaryProcedure

Source Link

Usage

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

public FixedSizeMap(Map<K, V> map) {
    super(map);/*  w ww  .j  a v a  2s  . co  m*/
    setOnPut(new BinaryFunction<Map<K, V>, Object[], V>() {
        public V evaluate(Map<K, V> map, Object[] b) {
            K key = (K) Array.get(b, 0);
            V value = (V) Array.get(b, 1);
            if (map.containsKey(key)) {
                return map.put(key, value);
            } else {
                throw new IllegalArgumentException();
            }
        }
    });

    setOnPutAll(new BinaryProcedure<Map<K, V>, Map<K, V>>() {
        public void run(Map<K, V> a, Map<K, V> b) {
            Map<K, V> dest = a;
            Map<K, V> src = b;

            if (GeneratorContains.instance().test(IteratorToGeneratorAdapter.adapt(src.keySet().iterator()),
                    Not.not(new ContainsKey(dest)))) {
                throw new IllegalArgumentException();
            } else {
                dest.putAll(src);
            }
        }
    });

    setOnRemove(new BinaryProcedureBinaryFunction<Map<K, V>, K, V>(
            (BinaryProcedure<? super Map<K, V>, ? super K>) new Throw<K, V>(
                    new UnsupportedOperationException())));
    setOnClear(new Throw<K, V>(new UnsupportedOperationException()));
}

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);/* www  .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());
                }
            }
        }
    });
}