[ERA-CVS] src/org/jdaemon/era/helper AbstractCube.java,1.2,1.3
Brought to you by:
jessex
|
From: <je...@23...> - 2008-10-06 10:23:16
|
Update of /cvsroot/era/src/org/jdaemon/era/helper In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv12559/era/helper Modified Files: AbstractCube.java Log Message: Various changes for ERA v2 - 'This time it's Generic...' Index: AbstractCube.java =================================================================== RCS file: /cvsroot/era/src/org/jdaemon/era/helper/AbstractCube.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** AbstractCube.java 13 Feb 2008 21:40:15 -0000 1.2 --- AbstractCube.java 6 Oct 2008 10:22:14 -0000 1.3 *************** *** 17,20 **** --- 17,21 ---- import org.jdaemon.util.comparator.ReverseComparator; import org.jdaemon.util.QuickList; + import org.jdaemon.util.attribute.Attribute; import java.util.Comparator; *************** *** 35,182 **** * @author Jonathan Essex */ ! public abstract class AbstractCube extends java.util.AbstractCollection implements Cube { ! ! ! /** Helper for calculation of aggregate values on a Cube. ! */ ! private abstract class Accumulator<T> { ! ! /** Current value of calculated aggregate ! */ ! private T value; ! ! /** Attribute of cube on which aggregate calculation is to be performed ! */ ! private String attribute; ! ! /** Function used to calculate aggregate value. ! * <P> ! * To calculate aggregate value A, for each of 1..i values of attribute a in cube, perform: ! * </P><P> ! * A<sub>i</sub> = operate(a<sub>i</sub>, A<sub>i-1</sub>) ! * </P><P> ! * The final value of A<sub>i</sub> is the aggregate value; The initial value A<sub>0</sub> ! * is set in the constructor. ! * </P> ! * @param value a<sub>i</sub> ! * @param accumulator A<sub>i-1</sub> ! * @return A<sub>i</sub> ! */ ! protected abstract T operate(Object value, T accumulator); ! ! /** Get the calculated aggregate value. ! * ! * @return The value of the calculated aggregate. ! */ ! public T getValue() { ! return value; ! } ! ! /** Get an attribute value from a point reference within this cube. ! * ! * @param point_ref A point reference (returned by Cube.iterator()) ! * @return The value of the attribute specified in the constructor at the given point ! */ ! private Object getAttr(Object point_ref) { ! return getAttribute(attribute, point_ref); ! } ! ! /** Perform one step of calculation. ! * ! * Given an Accumulator accum and a cube iterator i, the value of an aggregate can be calculated with: ! * <code> ! * while i.hasNext() accum.accumulate(i.next()); ! * </code> ! * ! * @param point_ref a point reference within a cube. ! */ ! public void accumulate(Object point_ref) { ! value = operate(getAttr(point_ref), value); ! } ! ! /** Create a new Accumulator ! * ! * @param value initial value for aggregate (A<sub>0</sub>) ! * @param attribute Attribute on which aggregation is to be calculated ! */ ! protected Accumulator(T value, String attribute) { ! this.value = value; ! this.attribute = attribute; ! } ! } ! ! /** Accumulator for determining max value of an attribute. ! * ! * Note assumption that the attribute value can be cast to T, and ! * also that getComparator(attribute) can be cast to Comparator<T> ! * ! * @param T type of maximum value to calculate ! */ ! private class MaxAccumulator<T> extends Accumulator<T> { ! protected Comparator<T> comparator; ! ! protected T operate(Object value, T accumulator) { ! if (accumulator == null) ! return (T)value; ! else ! return comparator.compare((T)value, accumulator) > 0 ? (T)value : accumulator; ! } ! ! public MaxAccumulator(String attribute) { ! super(null, attribute); ! comparator = (Comparator<T>)getComparator(attribute); ! } ! } ! ! /** Accumulator for determining min value of an attribute. ! * ! * Note assumption that the attribute value can be cast to T, and ! * also that getComparator(attribute) can be cast to Comparator<T> ! * ! * @param T type of maximum value to calculate ! */ ! private class MinAccumulator<T> extends Accumulator<T> { ! protected Comparator<T> comparator; ! ! protected T operate(Object value, T accumulator) { ! if (accumulator == null) ! return (T)value; ! else ! return comparator.compare((T)value, accumulator) < 0 ? (T)value : accumulator; ! } ! ! public MinAccumulator(String attribute) { ! super(null, attribute); ! comparator = (Comparator<T>)getComparator(attribute); ! } ! } ! ! /** Accumulator for determining sum of an attribute. ! * ! * Note assumption that the attribute value can be cast to Number ! * ! */ ! private class SumAccumulator extends Accumulator<Double> { ! ! protected Double operate(Object value, Double accumulator) { ! return new Double(((Number)value).doubleValue() + accumulator.doubleValue()); ! } ! ! public SumAccumulator(String attribute) { ! super(new Double(0.0), attribute); ! } ! } ! ! private class CountAccumulator extends Accumulator<Long> { ! ! protected Long operate(Object value, Long accumulator) { ! return value == null ? accumulator : new Long(accumulator.longValue() + 1); ! } ! public CountAccumulator(String attribute) { ! super(new Long(0), attribute); ! } ! } ! /** Add all the data items in the given Cube * --- 36,42 ---- * @author Jonathan Essex */ ! public abstract class AbstractCube<C> extends java.util.AbstractCollection<C> implements Cube<C> { ! /** Add all the data items in the given Cube * *************** *** 187,217 **** * @author Jonathan Essex */ ! public void addAll(Cube cube) { ! } ! ! /** Get a comparator object which orders an attribute of this cube ! * ! * The default implementation returns a default comparator which assumes the attribute ! * is of a type that implements Comparable. ! * ! * @param attribute The name of an attribute ! * @returns A comparator which orders the named attribute ! */ ! public Comparator<?> getComparator(String attribute) { ! return getDescriptor().getComparator(attribute); ! } ! ! /** Return a set of all the values of a given attribute across all contained objects ! * ! * @param attribute Attribute name for which set of values is to be retrieved ! * @returns A set of all the values of the given attribute within this cube ! * @author Jonathan Essex ! */ ! public SortedSet getAttributeSet(String attribute) { ! Iterator i = iterator(); ! SortedSet attribs = new TreeSet(getComparator(attribute)); ! while (i.hasNext()) attribs.add(getAttribute(attribute, i.next())); ! return attribs; } /** Create a new cube containing some subset of the contents of this cube --- 47,53 ---- * @author Jonathan Essex */ ! public void addAll(Cube<C> cube) { } + /** Create a new cube containing some subset of the contents of this cube *************** *** 227,231 **** * @returns A cube containing a subset of the objects in this cube */ ! public Cube constrain(String attribute, int constraint_type, Object value) { FilterFactory filter_factory = new FilterFactory(getDescriptor()); --- 63,67 ---- * @returns A cube containing a subset of the objects in this cube */ ! public <T extends Comparable<? super T>> Cube<C> constrain(Attribute<T,C> attribute, int constraint_type, T value) { FilterFactory filter_factory = new FilterFactory(getDescriptor()); |