[ERA-CVS] src/org/jdaemon/util/attribute ComparableAttribute.java, NONE, 1.1 Attributes.java, NONE,
Brought to you by:
jessex
Update of /cvsroot/era/src/org/jdaemon/util/attribute In directory sc8-pr-cvs17.sourceforge.net:/tmp/cvs-serv24608/attribute Added Files: ComparableAttribute.java Attributes.java NumericAttribute.java AttributeComparator.java Attribute.java Log Message: First commit of attributes package --- NEW FILE: ComparableAttribute.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.attribute; import java.util.Comparator; /** An attribute which has a comparable type * * Basically just implements getComparator() using Comparable.compareTo; Didn't do this in * the base Attribute class because then all Attribute objects would have to be for Comparable * Types - usually (but not always) the case. * * @author jonathan */ public abstract class ComparableAttribute<T extends Comparable<? super T>,C> extends Attribute<T,C> { private static class DefaultComparator<T extends Comparable<? super T>> implements Comparator<T> { public int compare(T a, T b) { return a.compareTo(b); } } public Comparator<T> getComparator() { return new DefaultComparator<T>(); } } --- NEW FILE: Attributes.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.attribute; import org.jdaemon.util.comparator.CompoundComparator; import org.jdaemon.util.functional.*; import java.util.List; import java.util.LinkedList; import java.util.Collections; import java.util.Collection; import java.util.Comparator; /** Utility class Attributes provides similar functions to Collections except * enhanced with the functionality provided by the attributes package. * * @author jonathan */ public class Attributes { /** Converts a list of attributes into a comparator. * * The comparator created will order data items of the type referenced by the * attribute in order of the given attribute values. * * @param attrs A list of attributes of C * @return A comparator implmenting an ordering of C according the the values of the given attributes of C */ public static <C> Comparator<C> newComparator(List<Attribute<?,C>> attrs) { List<Comparator<C>> comparators = new LinkedList<Comparator<C>>(); for (Attribute<?,C> attr : attrs ) { comparators.add(attr.getContainerComparator()); } return new CompoundComparator<C>(comparators); } /** Sort a list, ordering by the given attribute. * * If we have a list of objects of type C, for which we have defined an * attribute attr, then the list will be re-ordered according to the values * referenced by the attribute. * * @param list A list of items of type C * @param attr an attribute of C */ public static <T,C> void sort(List<? extends C> list, Attribute<T,C> attr) { Collections.sort(list, new AttributeComparator<T,C>(attr)); } /** Sort a list, ordering by the given attributes. * * If we have a list of objects of type C, for which we have defined an * attribute attr, then the list will be re-ordered according to the values * referenced by the attributes in <I>attrs</I> * * @param list A list of items of type C * @param attrs A list of attributes of C */ public static <C> void sort(List<? extends C> list, List<Attribute<?,C>> attrs) { Collections.sort(list, newComparator(attrs)); } /** Calculate an aggregate value. * * If attr is an attribute referencing values in C, acc should be one of attr.newMax, newMin, etc. * This method will call acc.accumulate(i) on all elements in <i>collection</i>, so calculating the relavent * aggregate value. * * @param collection * @param acc * @return acc.getResult() after calling acc.accumulate on all elements in collection */ public static <T,C> T aggregate(Collection<? extends C> collection, Accumulator<C,T> acc) { for (C i : collection) { acc.accumulate(i); } return acc.getResult(); } /** Calculate several aggregate values. * * accum acc should be one of attr.newMax, newMin, etc. * This method will call acc.accumulate(i) on all elements in <i>collection</i>, so calculating the relavent * aggregate value. * * @param collection * @param acc * @return acc.getResult() after calling acc.accumulate on all elements in collection */ public static <C> void aggregates(Collection<? extends C> collection, List<Accumulator<C,?>> accumulators) { for (C i : collection) { for (Accumulator<C,?> acc : accumulators) { acc.accumulate(i); } } } 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(); } } --- NEW FILE: NumericAttribute.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.attribute; import org.jdaemon.util.functional.*; /** Attribute representing Numeric (and, by extension, Comparable) types. * * Adds the SUM function to the set of constant functions provided by Attribute, * and the newSum method to the set of Accumulator creators similary provided. * * @author jonathan */ public abstract class NumericAttribute<T extends Number & Comparable<? super T>, C> extends ComparableAttribute<T,C> { public final BinaryFunction<Number,T,Number> SUM; public NumericAttribute() { SUM = new SumFunction<T>(); } public Accumulator<C,Number> newSum() { return new FunctionAccumulator<C,Number>(null, Operations.ChainSecond(accessor, SUM)); } } --- NEW FILE: AttributeComparator.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.attribute; import java.util.Comparator; /** Compares two objects based on the value of some attribute * * @author jonathan */ public class AttributeComparator<T,C> implements Comparator<C> { private Attribute<T,C> attr; public int compare(C a, C b) { return attr.getComparator().compare(attr.getValue(a), attr.getValue(b)); } public AttributeComparator(Attribute<T,C> attr) { this.attr = attr; } } --- NEW FILE: Attribute.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.attribute; import java.util.Comparator; import java.lang.Comparable; 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 * this attribute. The methods newMin, newMax, and newCount provide suitable accumulators * which can operate over collections of the container object to calculate the Minimum and Maxmum * values of this attribute within the collection, and the number of non-null values of this attribute * within the collection. * * * * @author jonathan * @param T type of attribute value * @param C type of container object */ public abstract class Attribute<T ,C> { public final BinaryFunction<T,T,T> MAX; public final BinaryFunction<T,T,T> MIN; public final BinaryFunction<Long,T,Long> COUNT; protected final UnaryFunction<C,T> accessor = new UnaryFunction<C,T>() { public T operate(C container) { return getValue(container); } }; public Attribute() { MAX = new MaxFunction<T>(getComparator()); MIN = new MinFunction<T>(getComparator()); COUNT = new CountFunction<T>(); } /** get the value of this Attribute from its container. */ public abstract T getValue(C container); /** set the value of this Attribute in its container. */ public abstract void setValue(T value, C container); /** get a comparator for values of this attribute * * Arg! This should return a shared static instance. F**k type erasure. * * @return a comarator suitable for values of this attribute */ public abstract Comparator<T> getComparator(); /** get a comparator which will compare objects containing this attribute based * on the value of this attribute. * * NOTE: Not totally happy about the existance of this function -- but we need * some way to create a suitable comparator without knowing the type of the attribute * - e.g. when we have a List<Attribute<?,C>> and we want to convert it into a * List<Comparator<C>>. * * Essentially therefore this function achieves nothing beyond the type-safe creation * of the right kind of AttributeComparator. So after type erasure, it's a bit pointless. * * @return */ public Comparator<C> getContainerComparator() { return new AttributeComparator<T,C>(this); } public Accumulator<C,T> newMax() { return new FunctionAccumulator<C,T>(null, Operations.ChainSecond(accessor, MAX)); } public Accumulator<C,T> newMin() { 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)); } } |