[ERA-CVS] src/org/jdaemon/util/attribute Attributes.java, 1.1, 1.2 Attribute.java, 1.1, 1.2
Brought to you by:
jessex
|
From: <je...@us...> - 2008-07-30 23:42:08
|
Update of /cvsroot/era/src/org/jdaemon/util/attribute In directory sc8-pr-cvs17.sourceforge.net:/tmp/cvs-serv15314 Modified Files: Attributes.java Attribute.java Log Message: CompoundComparator - create composite comparators Index: Attributes.java =================================================================== RCS file: /cvsroot/era/src/org/jdaemon/util/attribute/Attributes.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Attributes.java 29 Jul 2008 22:42:38 -0000 1.1 --- Attributes.java 30 Jul 2008 23:42:02 -0000 1.2 *************** *** 17,21 **** /** Utility class Attributes provides similar functions to Collections except ! * enhanced with the functionality provided by the attributes package. * * @author jonathan --- 17,23 ---- /** Utility class Attributes provides similar functions to Collections except ! * enhanced with the functionality provided by the attributes package. Basically, ! * by defining Attribute objects which address individual properties of some object, ! * lists can be sorted and filtered by the values of those properies. * * @author jonathan *************** *** 101,118 **** } public static <T,C> T max(Collection<? extends C> collection, Attribute<T,C> attr) { return aggregate(collection, attr.newMax()); } public static <T,C> T min(Collection<? extends C> collection, Attribute<T,C> attr) { return aggregate(collection, attr.newMin()); } public static <T extends Number & Comparable<? super T>, C> Number sum(Collection<? extends C> collection, NumericAttribute<T,C> attr) { return aggregate(collection, attr.newSum()); } ! public static <T,C> long count(Collection<? extends C> collection, Attribute<T,C> attr) { return aggregate(collection, attr.newCount()).longValue(); ! } } --- 103,149 ---- } + /** Get the maximum value of a property of some objects in a collection. + * + * @param collection + * @param attr + * @return The maximum value of the property addressed by attr within collection. + */ public static <T,C> T max(Collection<? extends C> collection, Attribute<T,C> attr) { return aggregate(collection, attr.newMax()); } + /** Get the minimum value of a property of some objects within a collection. + * + * @param collection + * @param attr + * @return The minimum value of the property addressed by attr within collection. + */ public static <T,C> T min(Collection<? extends C> collection, Attribute<T,C> attr) { return aggregate(collection, attr.newMin()); } + + /** Get the sum of values of a property of some objects within a collection. + * + * @param collection + * @param attr + * @return The sum of values of the property addressed by attr within collection. + */ public static <T extends Number & Comparable<? super T>, C> Number sum(Collection<? extends C> collection, NumericAttribute<T,C> attr) { return aggregate(collection, attr.newSum()); } ! ! /** Get the number of non-null values of a property of some objects within a collection. ! * ! * @param collection ! * @param attr ! * @return The number of non-null values of the property addressed by attr within collection. ! */ public static <T,C> long count(Collection<? extends C> collection, Attribute<T,C> attr) { return aggregate(collection, attr.newCount()).longValue(); ! } ! ! public static <T,C> Collection<C> filter(Collection<? extends C> collection, Attribute<T,C> attr, UnaryFunction<T,Boolean> filter) { ! return Operations.filter(collection, Operations.Chain(attr.accessor, filter)); ! } } Index: Attribute.java =================================================================== RCS file: /cvsroot/era/src/org/jdaemon/util/attribute/Attribute.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Attribute.java 29 Jul 2008 22:42:39 -0000 1.1 --- Attribute.java 30 Jul 2008 23:42:02 -0000 1.2 *************** *** 10,20 **** import org.jdaemon.util.functional.*; ! /** An Attribute addresses a value of type T within some container object of class C. ! * * Implementer must provide getValue and setValue methods which extract and ! * set values of this attribute within the data item. The implementer must also provide * a getComparator method, to provide a comparator suitable for comparing values of ! * this attribute. ! * * The implementation provides several useful features. The constants MAX, MIN, * and COUNT are defined, providing binary functions which can be used to compare values of --- 10,21 ---- import org.jdaemon.util.functional.*; ! /** An Attribute addresses a property of type T within some container object of class C. ! * <p> * Implementer must provide getValue and setValue methods which extract and ! * set values of this property within the container object. The implementer must also provide * a getComparator method, to provide a comparator suitable for comparing values of ! * the property. ! * </p> ! * <p> * The implementation provides several useful features. The constants MAX, MIN, * and COUNT are defined, providing binary functions which can be used to compare values of *************** *** 23,27 **** * values of this attribute within the collection, and the number of non-null values of this attribute * within the collection. ! * * * * @author jonathan --- 24,28 ---- * values of this attribute within the collection, and the number of non-null values of this attribute * within the collection. ! * </p> * * * @author jonathan *************** *** 88,94 **** --- 89,135 ---- return new FunctionAccumulator<C,T>(null, Operations.ChainSecond(accessor, MIN)); } + public Accumulator<C,Long> newCount() { return new FunctionAccumulator<C,Long>(null, Operations.ChainSecond(accessor, COUNT)); } + public UnaryFunction<C,Boolean> isGreaterThan(final T operand) { + return new UnaryFunction<C,Boolean>() { + public Boolean operate(C obj) { + return getComparator().compare(getValue(obj), operand) > 0; + } + }; + } + + public UnaryFunction<C,Boolean> isLessThan(final T operand) { + return new UnaryFunction<C,Boolean>() { + public Boolean operate(C obj) { + return getComparator().compare(getValue(obj), operand) < 0; + } + }; + } + + public UnaryFunction<C,Boolean> isGreaterThanOrEqual(final T operand) { + return new UnaryFunction<C,Boolean>() { + public Boolean operate(C obj) { + return getComparator().compare(getValue(obj), operand) >= 0; + } + }; + } + + public UnaryFunction<C,Boolean> isLessThanOrEqual(final T operand) { + return new UnaryFunction<C,Boolean>() { + public Boolean operate(C obj) { + return getComparator().compare(getValue(obj), operand) <= 0; + } + }; + } + + public UnaryFunction<C,Boolean> isEqual(final T operand) { + return new UnaryFunction<C,Boolean>() { + public Boolean operate(C obj) { + return getComparator().compare(getValue(obj), operand) == 0; + } + }; + } } |