@Test public void testSwitchMapTransformer() { final Transformer<String, String> a = TransformerUtils.constantTransformer("A"); final Transformer<String, String> b = TransformerUtils.constantTransformer("B"); final Transformer<String, String> c = TransformerUtils.constantTransformer("C"); Map<String, Transformer<String, String>> map = new HashMap<String, Transformer<String,String>>(); map.put("HELLO", a); map.put("THERE", b); assertEquals(null, TransformerUtils.switchMapTransformer(map).transform("WELL")); assertEquals("A", TransformerUtils.switchMapTransformer(map).transform("HELLO")); assertEquals("B", TransformerUtils.switchMapTransformer(map).transform("THERE")); map.put(null, c); assertEquals("C", TransformerUtils.switchMapTransformer(map).transform("WELL")); assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(new HashMap<Object, Transformer<Object, Object>>())); map = new HashMap<String, Transformer<String, String>>(); map.put(null, null); assertSame(ConstantTransformer.NULL_INSTANCE, TransformerUtils.switchMapTransformer(map)); try { TransformerUtils.switchMapTransformer(null); fail(); } catch (final NullPointerException ex) {} }
@SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchMapTransformer( final Map<I, Transformer<I, O>> objectsAndTransformers) { if (objectsAndTransformers == null) { throw new NullPointerException("The object and transformer map must not be null"); } final Transformer<? super I, ? extends O> def = objectsAndTransformers.remove(null); final int size = objectsAndTransformers.size(); final Transformer<? super I, ? extends O>[] trs = new Transformer[size]; final Predicate<I>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); trs[i++] = entry.getValue(); } return TransformerUtils.switchTransformer(preds, trs, def); }
TransformerUtils { @SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchMapTransformer( final Map<I, Transformer<I, O>> objectsAndTransformers) { if (objectsAndTransformers == null) { throw new NullPointerException("The object and transformer map must not be null"); } final Transformer<? super I, ? extends O> def = objectsAndTransformers.remove(null); final int size = objectsAndTransformers.size(); final Transformer<? super I, ? extends O>[] trs = new Transformer[size]; final Predicate<I>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); trs[i++] = entry.getValue(); } return TransformerUtils.switchTransformer(preds, trs, def); } }
TransformerUtils { @SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchMapTransformer( final Map<I, Transformer<I, O>> objectsAndTransformers) { if (objectsAndTransformers == null) { throw new NullPointerException("The object and transformer map must not be null"); } final Transformer<? super I, ? extends O> def = objectsAndTransformers.remove(null); final int size = objectsAndTransformers.size(); final Transformer<? super I, ? extends O>[] trs = new Transformer[size]; final Predicate<I>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); trs[i++] = entry.getValue(); } return TransformerUtils.switchTransformer(preds, trs, def); } private  TransformerUtils(); }
TransformerUtils { @SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchMapTransformer( final Map<I, Transformer<I, O>> objectsAndTransformers) { if (objectsAndTransformers == null) { throw new NullPointerException("The object and transformer map must not be null"); } final Transformer<? super I, ? extends O> def = objectsAndTransformers.remove(null); final int size = objectsAndTransformers.size(); final Transformer<? super I, ? extends O>[] trs = new Transformer[size]; final Predicate<I>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); trs[i++] = entry.getValue(); } return TransformerUtils.switchTransformer(preds, trs, def); } private  TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
            final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
            final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
                                                      final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
                                                         final Transformer<? super I, ? extends O> trueTransformer,
                                                         final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
            final Transformer<? super I, ? extends O> trueTransformer,
            final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
            final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
            final Transformer<? super I, ? extends O>[] transformers,
            final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
            final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
            final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
            final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
                                                              final Object[] args); static Transformer<T, String> stringValueTransformer(); }
TransformerUtils { @SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchMapTransformer( final Map<I, Transformer<I, O>> objectsAndTransformers) { if (objectsAndTransformers == null) { throw new NullPointerException("The object and transformer map must not be null"); } final Transformer<? super I, ? extends O> def = objectsAndTransformers.remove(null); final int size = objectsAndTransformers.size(); final Transformer<? super I, ? extends O>[] trs = new Transformer[size]; final Predicate<I>[] preds = new Predicate[size]; int i = 0; for (final Map.Entry<I, Transformer<I, O>> entry : objectsAndTransformers.entrySet()) { preds[i] = EqualPredicate.<I>equalPredicate(entry.getKey()); trs[i++] = entry.getValue(); } return TransformerUtils.switchTransformer(preds, trs, def); } private  TransformerUtils(); static Transformer<I, O> exceptionTransformer(); static Transformer<I, O> nullTransformer(); static Transformer<T, T> nopTransformer(); static Transformer<T, T> cloneTransformer(); static Transformer<I, O> constantTransformer(final O constantToReturn); static Transformer<T, T> asTransformer(final Closure<? super T> closure); static Transformer<T, Boolean> asTransformer(final Predicate<? super T> predicate); static Transformer<I, O> asTransformer(final Factory<? extends O> factory); static Transformer<T, T> chainedTransformer(
            final Transformer<? super T, ? extends T>... transformers); static Transformer<T, T> chainedTransformer(
            final Collection<? extends Transformer<? super T, ? extends T>> transformers); static Transformer<T, T> ifTransformer(final Predicate<? super T> predicate,
                                                      final Transformer<? super T, ? extends T> trueTransformer); static Transformer<I, O> ifTransformer(final Predicate<? super I> predicate,
                                                         final Transformer<? super I, ? extends O> trueTransformer,
                                                         final Transformer<? super I, ? extends O> falseTransformer); @SuppressWarnings("unchecked") @Deprecated static Transformer<I, O> switchTransformer(final Predicate<? super I> predicate,
            final Transformer<? super I, ? extends O> trueTransformer,
            final Transformer<? super I, ? extends O> falseTransformer); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
            final Transformer<? super I, ? extends O>[] transformers); static Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates,
            final Transformer<? super I, ? extends O>[] transformers,
            final Transformer<? super I, ? extends O> defaultTransformer); static Transformer<I, O> switchTransformer(
            final Map<Predicate<I>, Transformer<I, O>> predicatesAndTransformers); @SuppressWarnings("unchecked") static Transformer<I, O> switchMapTransformer(
            final Map<I, Transformer<I, O>> objectsAndTransformers); static Transformer<Class<? extends T>, T> instantiateTransformer(); static Transformer<Class<? extends T>, T> instantiateTransformer(
            final Class<?>[] paramTypes, final Object[] args); static Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map); static Transformer<I, O> invokerTransformer(final String methodName); static Transformer<I, O> invokerTransformer(final String methodName, final Class<?>[] paramTypes,
                                                              final Object[] args); static Transformer<T, String> stringValueTransformer();  }