/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/ext/Median.java#2 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.ext; import saffron.AggregationExtender; import saffron.runtime.SaffronUtil; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; /** * <code>Median</code> is an aggregation which returns the Median of a set of * points. * * @author jhyde * @since 26 January, 2001 * @version $Id: //guest/julian_hyde/saffron/src/main/saffron/ext/Median.java#2 $ **/ public class Median implements AggregationExtender { // Comparable methods public Comparable aggregate(Comparable value) { throw new UnsupportedOperationException(); } public Object start(Comparable value) { return new ArrayList(); } public Object next(Comparable value, Object accumulator) { ((ArrayList) accumulator).add(value); return accumulator; } public Object merge(Comparable value, Object accumulator1, Object accumulator2) { ((ArrayList) accumulator1).addAll((ArrayList) accumulator2); return accumulator1; } public Object result(Comparable value, Object accumulator) { Object[] a = ((ArrayList) accumulator).toArray(); Arrays.sort(a); if (a.length < 1) { return null; } int i = a.length / 2; return a[i]; } // int methods public int aggregate(int value) { throw new UnsupportedOperationException(); } public Object start(int value) { return new ArrayList(); } public Object next(int value, Object accumulator) { ((ArrayList) accumulator).add(new Integer(value)); return accumulator; } public Object merge(int value, Object accumulator1, Object accumulator2) { ((ArrayList) accumulator1).addAll((ArrayList) accumulator2); return accumulator1; } public int result(int value, Object accumulator) { return SaffronUtil.intValue(result(null, accumulator)); } // {Comparable,Comparable} methods public Comparable[] aggregate(Comparable v0,Comparable v1) { throw new UnsupportedOperationException(); } public Object start(Comparable v0,Comparable v1) { return new ArrayList(); } public Object next(Comparable v0,Comparable v1, Object accumulator) { ((ArrayList) accumulator).add(new Comparable[] {v0,v1}); return accumulator; } public Object merge(Comparable v0,Comparable v1, Object accumulator1, Object accumulator2) { ((ArrayList) accumulator1).addAll((ArrayList) accumulator2); return accumulator1; } public Comparable[] result(Comparable v0,Comparable v1, Object accumulator) { Object[] a = ((ArrayList) accumulator).toArray(); Arrays.sort(a, new ArrayComparator()); if (a.length < 1) { return null; } int i = a.length / 2; return (Comparable[]) a[i]; } private static class ArrayComparator implements Comparator { // implement Comparator public int compare(Object o, Object p) { Object[] a = (Object[]) o, b = (Object[]) p; for (int i = 0; i < a.length; i++) { int c = ((Comparable) a[i]).compareTo(b[i]); if (c != 0) { return c; } } return 0; } }; } // End Median.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1475 | Julian Hyde |
Saffron. Re-integrate Median, etc. |
||
#1 | 1474 | Julian Hyde |
saffron: Aggregations are working. Renamed 'aggregator' to 'aggregation'. |
||
//guest/julian_hyde/saffron/src/main/saffron/runtime/Median.java | |||||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |