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

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

Introduction

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

Prototype

BinaryFunction

Source Link

Usage

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

public FixedSizeMap(Map<K, V> map) {
    super(map);//from  w  w w. ja v  a 2  s .c  o 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.LazyMap.java

public LazyMap(Map<K, V> map, final Function<K, V> factory) {
    super(map);//from  w  ww  .ja v a 2s.  co  m
    setOnGet(new BinaryFunction<Map<K, V>, K, V>() {
        public V evaluate(Map<K, V> map, K key) {
            if (map.containsKey(key)) {
                return map.get(key);
            } else {
                V value = factory.evaluate(key);
                map.put(key, value);
                return value;
            }
        }
    });
}