/** * Finds the maximum item 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 objects on which the maximum should be found * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return The maximum of all the Object in the given iterable * @throws IllegalArgumentException if the iterable is not an Iterable */ public static <T> T max(Object iterable, T argument) { return (T)aggregate(iterable, MAX, argument); }
/** * Finds the minimum item in the given iterable. * 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 * @return The minimum of all the Object in the given iterable * @throws IllegalArgumentException if the iterable is not an Iterable */ public static <T> T min(Object iterable) { return (T) aggregate(iterable, MIN); }
/** * Finds the maximum item in the given iterable. * 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 objects on which the maximum should be found * @return The maximum of all the Object in the given iterable * @throws IllegalArgumentException if the iterable is not an Iterable */ public static <T> T max(Object iterable) { return (T) aggregate(iterable, MAX); }
/** * Finds the minimum item 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 objects on which the minimum should be found * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return The minimum of all the Object in the given iterable * @throws IllegalArgumentException if the iterable is not an Iterable */ public static <T> T min(Object iterable, T argument) { return (T)aggregate(iterable, MIN, argument); }
/** * Selects the item in the given iterable having the lowest value on the given argument defined using the on method. * 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 objects to be filtered * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return The item in the given iterable with the minimum value on the given argument */ public static <T, A> T selectMin(Object iterable, A argument) { return aggregate(iterable, new MinOnArgument<T, A>(argument)); }
/** * Aggregates the items in this iterable using the given {@link Aggregator}. * @param aggregator The function that defines how the objects in this iterable have to be aggregated * @return The result of the aggregation of all the items in this iterable */ public T aggregate(Aggregator<T> aggregator) { return Lambda.aggregate(getInner(), aggregator); }
/** * Selects the item in the given iterable having the highest value on the given argument defined using the on method. * 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 objects to be filtered * @param argument An argument defined using the {@link Lambda#on(Class)} method * @return The item in the given iterable with the maximum value on the given argument */ public static <T, A> T selectMax(Object iterable, A argument) { return aggregate(iterable, new MaxOnArgument<T, A>(argument)); }
/** * 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}. * @param argument An argument defined using the {@link Lambda#on(Class)} method * @param aggregator The function that defines how the objects in this iterable have to be aggregated * @return The result of the aggregation of all the items in this iterable */ public <A> A aggregate(A argument, Aggregator<A> aggregator) { return Lambda.aggregate(getInner(), aggregator, argument); }
/** * Joins all the object in the given iterable by concatenating all their String representation. * It invokes toString() an all the objects and concatening them using the given separator. * 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 joined * @param separator The String used to separe the item's String representation * @return The concatenation of the String representation of all the objects in the given iterable or an empty String if the iterable is null or empty */ public static String join(Object iterable, String separator) { if (iterable == null) return ""; try { return (String)aggregate(iterable, new Concat(separator)); } catch (IllegalArgumentException e) { return iterable.toString(); } }
private static Number typedSum(Object iterable, Class<?> numberClass) { if (iterable instanceof Number) return (Number)iterable; Iterator<?> iterator = asIterator(iterable); return iterator.hasNext() ? aggregate(iterator, getSumAggregator(iterator.next())) : typedZero(numberClass); }
/** * 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); }
private static Number typedAvg(Object iterable, Class<?> numberClass) { if (iterable instanceof Number) return (Number)iterable; Iterator<?> iterator = asIterator(iterable); return iterator.hasNext() ? aggregate(iterator, new Avg()) : typedZero(numberClass); }