@Test public void testSwitchMapClosure() { final MockClosure<String> a = new MockClosure<String>(); final MockClosure<String> b = new MockClosure<String>(); final Map<String, Closure<String>> map = new HashMap<String, Closure<String>>(); map.put("HELLO", a); map.put("THERE", b); ClosureUtils.switchMapClosure(map).execute(null); assertEquals(0, a.count); assertEquals(0, b.count); a.reset(); b.reset(); map.clear(); map.put("HELLO", a); map.put("THERE", b); ClosureUtils.switchMapClosure(map).execute("THERE"); assertEquals(0, a.count); assertEquals(1, b.count); a.reset(); b.reset(); map.clear(); final MockClosure<String> c = new MockClosure<String>(); map.put("HELLO", a); map.put("THERE", b); map.put(null, c); ClosureUtils.switchMapClosure(map).execute("WELL"); assertEquals(0, a.count); assertEquals(0, b.count); assertEquals(1, c.count); assertEquals(NOPClosure.INSTANCE, ClosureUtils.switchMapClosure(new HashMap<String, Closure<String>>())); try { ClosureUtils.switchMapClosure(null); fail(); } catch (final NullPointerException ex) {} }
@SuppressWarnings("unchecked") public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures) { if (objectsAndClosures == null) { throw new NullPointerException("The object and closure map must not be null"); } final Closure<? super E> def = objectsAndClosures.remove(null); final int size = objectsAndClosures.size(); final Closure<? super E>[] trs = new Closure[size]; final Predicate<E>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<? extends E, Closure<E>> entry : objectsAndClosures.entrySet()) { preds[i] = EqualPredicate.<E>equalPredicate(entry.getKey()); trs[i] = entry.getValue(); i++; } return ClosureUtils.<E>switchClosure(preds, trs, def); }
ClosureUtils { @SuppressWarnings("unchecked") public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures) { if (objectsAndClosures == null) { throw new NullPointerException("The object and closure map must not be null"); } final Closure<? super E> def = objectsAndClosures.remove(null); final int size = objectsAndClosures.size(); final Closure<? super E>[] trs = new Closure[size]; final Predicate<E>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<? extends E, Closure<E>> entry : objectsAndClosures.entrySet()) { preds[i] = EqualPredicate.<E>equalPredicate(entry.getKey()); trs[i] = entry.getValue(); i++; } return ClosureUtils.<E>switchClosure(preds, trs, def); } }
ClosureUtils { @SuppressWarnings("unchecked") public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures) { if (objectsAndClosures == null) { throw new NullPointerException("The object and closure map must not be null"); } final Closure<? super E> def = objectsAndClosures.remove(null); final int size = objectsAndClosures.size(); final Closure<? super E>[] trs = new Closure[size]; final Predicate<E>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<? extends E, Closure<E>> entry : objectsAndClosures.entrySet()) { preds[i] = EqualPredicate.<E>equalPredicate(entry.getKey()); trs[i] = entry.getValue(); i++; } return ClosureUtils.<E>switchClosure(preds, trs, def); } private  ClosureUtils(); }
ClosureUtils { @SuppressWarnings("unchecked") public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures) { if (objectsAndClosures == null) { throw new NullPointerException("The object and closure map must not be null"); } final Closure<? super E> def = objectsAndClosures.remove(null); final int size = objectsAndClosures.size(); final Closure<? super E>[] trs = new Closure[size]; final Predicate<E>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<? extends E, Closure<E>> entry : objectsAndClosures.entrySet()) { preds[i] = EqualPredicate.<E>equalPredicate(entry.getKey()); trs[i] = entry.getValue(); i++; } return ClosureUtils.<E>switchClosure(preds, trs, def); } private  ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Transformer<? super E, ?> transformer); static Closure<E> forClosure(final int count, final Closure<? super E> closure); static Closure<E> whileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure); static Closure<E> doWhileClosure(final Closure<? super E> closure,
                                                final Predicate<? super E> predicate); static Closure<E> invokerClosure(final String methodName); static Closure<E> invokerClosure(final String methodName, final Class<?>[] paramTypes,
                                                final Object[] args); static Closure<E> chainedClosure(final Closure<? super E>... closures); static Closure<E> chainedClosure(final Collection<? extends Closure<? super E>> closures); static Closure<E> ifClosure(final Predicate<? super E> predicate,
                                           final Closure<? super E> trueClosure); static Closure<E> ifClosure(final Predicate<? super E> predicate,
                                           final Closure<? super E> trueClosure,
                                           final Closure<? super E> falseClosure); static Closure<E> switchClosure(final Predicate<? super E>[] predicates,
                                               final Closure<? super E>[] closures); static Closure<E> switchClosure(final Predicate<? super E>[] predicates,
                                               final Closure<? super E>[] closures,
                                               final Closure<? super E> defaultClosure); static Closure<E> switchClosure(final Map<Predicate<E>, Closure<E>> predicatesAndClosures); @SuppressWarnings("unchecked") static Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures); }
ClosureUtils { @SuppressWarnings("unchecked") public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures) { if (objectsAndClosures == null) { throw new NullPointerException("The object and closure map must not be null"); } final Closure<? super E> def = objectsAndClosures.remove(null); final int size = objectsAndClosures.size(); final Closure<? super E>[] trs = new Closure[size]; final Predicate<E>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<? extends E, Closure<E>> entry : objectsAndClosures.entrySet()) { preds[i] = EqualPredicate.<E>equalPredicate(entry.getKey()); trs[i] = entry.getValue(); i++; } return ClosureUtils.<E>switchClosure(preds, trs, def); } private  ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Transformer<? super E, ?> transformer); static Closure<E> forClosure(final int count, final Closure<? super E> closure); static Closure<E> whileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure); static Closure<E> doWhileClosure(final Closure<? super E> closure,
                                                final Predicate<? super E> predicate); static Closure<E> invokerClosure(final String methodName); static Closure<E> invokerClosure(final String methodName, final Class<?>[] paramTypes,
                                                final Object[] args); static Closure<E> chainedClosure(final Closure<? super E>... closures); static Closure<E> chainedClosure(final Collection<? extends Closure<? super E>> closures); static Closure<E> ifClosure(final Predicate<? super E> predicate,
                                           final Closure<? super E> trueClosure); static Closure<E> ifClosure(final Predicate<? super E> predicate,
                                           final Closure<? super E> trueClosure,
                                           final Closure<? super E> falseClosure); static Closure<E> switchClosure(final Predicate<? super E>[] predicates,
                                               final Closure<? super E>[] closures); static Closure<E> switchClosure(final Predicate<? super E>[] predicates,
                                               final Closure<? super E>[] closures,
                                               final Closure<? super E> defaultClosure); static Closure<E> switchClosure(final Map<Predicate<E>, Closure<E>> predicatesAndClosures); @SuppressWarnings("unchecked") static Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures);  }