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

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

Introduction

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

Prototype

UnaryFunction

Source Link

Usage

From source file:com.aw.support.collection.ListUtils.java

public static java.util.Map<Object, Object> toMap(Collection<Object[]> list, final int propertyKeyIdx,
        final int propertyValueIdx) {
    final HashMap<Object, Object> map = new HashMap();
    UnaryFunction unaryFunction = new UnaryFunction() {
        public Object evaluate(Object o) {
            Object[] row = (Object[]) o;

            Object key = row[propertyKeyIdx];
            Object value = propertyValueIdx < 0 ? row : row[propertyValueIdx];
            map.put(key, value);//from w ww  .  j a  v a 2s  .co m
            return null;
        }
    };
    apply(list, null, unaryFunction);
    return map;
}

From source file:com.aw.support.collection.ListUtils.java

public static <E> List<E> columnFindList(Collection<E> list, final String[] propertyNames,
        final Object[] propertyValues) {
    final List result = new ArrayList();
    apply(list, null, new UnaryFunction() {

        @Override//w w  w .  j  a  v  a  2  s . co  m
        public Object evaluate(Object o) {
            BeanWrapper wrapper = new BeanWrapperImpl(o);
            for (int i = 0; i < propertyNames.length; i++) {
                String propName = propertyNames[i];
                Object propVal = wrapper.getPropertyValue(propName);
                if (!BeanUtils.equals(propVal, propertyValues[i]))
                    return null;
            }
            result.add(o);
            return null;
        }
    });
    return result;
}

From source file:org.mili.core.cache.DefaultCacheTest.java

@Test
public void testCreate_Collection() {
    class Foo {/* ww w  .  ja  v  a 2  s .c o  m*/
        String s;
        int i;

        Foo(String s, int i) {
            this.s = s;
            this.i = i;
        }
    }
    List<Foo> l = new ArrayList<Foo>();
    l.add(new Foo("a", 1));
    l.add(new Foo("b", 2));
    l.add(new Foo("c", 3));
    Cache<String, Foo> c = DefaultCache.create(l, new UnaryFunction<Foo, String>() {
        @Override
        public String evaluate(Foo f) {
            return f.s;
        }
    });
    assertEquals(1, c.get("a").i);
    assertEquals(2, c.get("b").i);
    assertEquals(3, c.get("c").i);
}

From source file:org.mili.core.collection.MapUtilTest.java

@Test(expected = IllegalStateException.class)
public void shouldFailBecauseTransformationFunctionThrowsException() {
    UnaryFunction<FooId, Integer> f = new UnaryFunction<FooId, Integer>() {
        @Override/* ww w.  jav a2s  .  c o  m*/
        public Integer evaluate(FooId o) {
            throw new IllegalStateException("fails");
        }
    };
    List<FooId> l = new ArrayList<FooId>();
    FooId fi0 = new FooId(4711, "lala");
    l.add(fi0);
    MapUtil.listAsMap(l, f);
}

From source file:org.mili.core.collection.MapUtilTest.java

@Test
public void testListAsMap_List_Function() {
    UnaryFunction<FooId, Integer> f = new UnaryFunction<FooId, Integer>() {
        @Override/*w w w  . jav  a2s.com*/
        public Integer evaluate(FooId o) {
            return o.id;
        }
    };
    // negativ
    try {
        MapUtil.listAsMap(null, null);
        fail("exception expected!");
    } catch (IllegalArgumentException e) {
    }
    try {
        MapUtil.listAsMap(null, f);
        fail("exception expected!");
    } catch (IllegalArgumentException e) {
    }
    try {
        MapUtil.listAsMap(new ArrayList<Object>(), null);
        fail("exception expected!");
    } catch (IllegalArgumentException e) {
    }
    // positiv
    List<FooId> l = new ArrayList<FooId>();
    FooId fi0 = new FooId(4711, "lala");
    FooId fi1 = new FooId(4712, "tinki");
    FooId fi2 = new FooId(4713, "tele");
    FooId fi3 = new FooId(4714, "abbas");
    l.add(fi0);
    l.add(fi1);
    l.add(fi2);
    l.add(fi3);
    Map<Integer, FooId> m = MapUtil.listAsMap(l, f);
    assertEquals(4, m.size());
    assertEquals(m.get(4711), fi0);
    assertEquals(m.get(4712), fi1);
    assertEquals(m.get(4713), fi2);
    assertEquals(m.get(4714), fi3);
    // positiv, double adding
    l.add(fi0);
    l.add(fi1);
    l.add(fi2);
    l.add(fi3);
    m = MapUtil.listAsMap(l, f);
    assertEquals(4, m.size());
    assertEquals(m.get(4711), fi0);
    assertEquals(m.get(4712), fi1);
    assertEquals(m.get(4713), fi2);
    assertEquals(m.get(4714), fi3);
    // multiple check
    try {
        m = MapUtil.listAsMap(false, l, f);
        fail("exception expected!");
    } catch (ArrayStoreException e) {
    }
}

From source file:org.mili.core.io.DirectoryCleanerTest.java

@Test
public void shouldChecksBeforeDelete() {
    final List<File> list = new ArrayList<File>();
    cleaner = DirectoryCleaner.create(new UnaryFunction<File, Boolean>() {
        @Override/*  w ww .  jav a2  s  . co m*/
        public Boolean evaluate(File file) {
            list.add(file);
            return true;
        }
    });
    cleaner.delete(file_01);
    assertFalse(file_01.exists());
    assertEquals(1, list.size());
    assertEquals(file_01, list.get(0));
}