/** * Converts all the object in the iterable extracting the property defined by the given argument. * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable containing the objects to be converted * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return A list containing the argument's value extracted from the object in the given iterable */ public static <F, T> List<T> extract(Object iterable, T argument) { return convert(iterable, new ArgumentConverter<F, T>(argument)); }
/** * Indexes the objects in the given iterable based on the value of their argument. * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable containing the objects to be indexed * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return A map having as keys the argument value extracted from the objects in the given iterable and as values the corresponding objects */ public static <F, T> Map<T, F> index(Object iterable, T argument) { return map(iterable, new ArgumentConverter<F, T>(argument)); }
/** * Converts all the object in the iterable extracting the property defined by the given argument. * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Unlike the method {@link #extract(Object, Object)} this one doesn't build a new collection, and the * extraction is done only when someone iterates over the returned iterator. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable containing the objects to be converted * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return A list containing the argument's value extracted from the object in the given iterable */ public static <F, T> Iterator<T> extractIterator(Object iterable, T argument) { return convertIterator(iterable, new ArgumentConverter<F, T>(argument)); }
/** * Converts all the values in the map extracting the property defined by the given argument. * @param map The map containing the values to be converted * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return A Map containing the same keys of the original one and the argument's value extracted from the value * in the corresponding entry of the map */ public static <K, F, T> Map<K, T> convertMap(Map<K, F> map, T argument) { return convertMap(map, new ArgumentConverter<F, T>(argument)); }
public ConstructorArgumentConverter(Class<T> clazz, Object... arguments) { for (Constructor<?> c : clazz.getConstructors()) { if (isCompatible(c, arguments)) { this.constructor = (Constructor<T>)c; break; } } if (constructor == null) throw new IntrospectionException("Unable to find a constructor of " + clazz.getName() + " compatible with the given arguments"); if (arguments != null) for (Object argument : arguments) { argumentConverters.add(new ArgumentConverter<F, Object>(argument)); } }
/** * Sums the property values of the items in the given iterable defined by the given argument. * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable of items containing the property of which the values have to be summed. * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return The sum of the property values extracted from all the items in the given iterable * @throws IllegalArgumentException if the iterable is not an Iterable */ public static <T> T sum(Object iterable, T argument) { return (T)typedSum(convertIterator(iterable, new ArgumentConverter<Object, T>(argument)), argument.getClass()); }
/** * Calculates the average of the property values of the items in the given iterable defined by the given argument. * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable of items containing the property for which the average of its the values has to be calculated. * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return The average of the property values extracted from all the items in the given iterable * @throws IllegalArgumentException if the iterable is not an Iterable */ public static <T> T avg(Object iterable, T argument) { return (T)typedAvg(convertIterator(iterable, new ArgumentConverter<Object, T>(argument)), argument.getClass()); }
/** * For each item in the given iterable collects the value defined by the given argument and * then aggregates them iterable using the given {@link Aggregator}. * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable of numbers to be summed * @param aggregator The function that defines how the objects in this iterable have to be aggregated * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return The result of the aggregation of all the items in the given iterable * @throws RuntimeException if the iterable is not an Iterable */ public static <T, A> T aggregate(Object iterable, Aggregator<T> aggregator, A argument) { return aggregate(convertIterator(iterable, new ArgumentConverter<T, A>(argument)), aggregator); }
/** * For each item in the given iterable collects the value defined by the given argument and put them in a List. * For example the following code: * <pre> * List<Person> myFriends = asList(new Person("Biagio", 39), new Person("Luca", 29), new Person("Celestino", 29)); * List<Integer> ages = collect(meAndMyFriends, on(Person.class).getAge()); * </pre> * extracts the ages of all the Persons in the list and put them in a List of Integer. * <p/> * Actually it handles also Maps, Arrays and Iterator by collecting their values. * Note that this method accepts an Object in order to be used in conjunction with the {@link Lambda#forEach(Iterable)}. * @param iterable The iterable of which the items should be collected * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return A List containing all the items collected from the give iterable * @throws RuntimeException if the iterable is not an Iterable or a Map */ public static <T> List<T> collect(Object iterable, T argument) { return (List<T>)collect(convert(iterable, new ArgumentConverter<Object, T>(argument))); }