/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/ext/Nth.java#1 $ // 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.runtime; import saffron.AggregatorExtender; /** * <code>Nth</code> is an example of a custom aggregator. It returns the the * <i>n</i><sup>th</sup> of a set of rows, or returns null (0 for a primitive * type) if there are not that many rows. * * It implements the overloaded <code>T aggregate(T)</code> method for T = * {<code>int</code>, <code>double</code>, <code>Object</code>}. There is a * start, next, and result method for each. * * @author jhyde * @since 11 February, 2002 * @version $Id: //guest/julian_hyde/saffron/src/main/saffron/ext/Nth.java#1 $ **/ public class Nth implements AggregatorExtender { private int n; public Nth(int n) { this.n = n; } // int methods public Object start(int value) { return new Holder_int(); } public Object next(int value, Object accumulator) { Holder_int holder = (Holder_int) accumulator; if (holder.count++ == n) { holder.result = value; } return holder; } public int result(int value, Object accumulator) { Holder_int holder = (Holder_int) accumulator; return holder.result; } private static class Holder_int { int count; int result; }; // double methods public Object start(double value) { return new Holder_double(); } public Object next(double value, Object accumulator) { Holder_double holder = (Holder_double) accumulator; if (holder.count++ == n) { holder.result = value; } return holder; } public double result(double value, Object accumulator) { Holder_double holder = (Holder_double) accumulator; return holder.result; } private static class Holder_double { int count; double result; }; // Object methods public Object start(Object value) { return new Holder_Object(); } public Object next(Object value, Object accumulator) { Holder_Object holder = (Holder_Object) accumulator; if (holder.count++ == n) { holder.result = value; } return holder; } public Object result(Object value, Object accumulator) { Holder_Object holder = (Holder_Object) accumulator; return holder.result; } private static class Holder_Object { int count; Object result; }; } // End Nth.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/Nth.java | |||||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |