NEXMarkGeneratorSource bids = new NEXMarkGeneratorSource(AUCTION_DATA_DIR,
        CASTOR_MAPPING_FILE, NEXMarkGeneratorSource.BID_STREAM);
bids.setCloseMode(Source.AUTOMATIC_CLOSE);

final long lifespan = 10*60;
TemporalWindow window = new TemporalWindow(bids, lifespan);

final int noOfItems = 1054;
HashGrouper group = new HashGrouper(window, new Function() {
    public Object invoke(Object o) {
        return new Integer((int)((Bid)((TimeStampedObject)o).getObject()).getItemID());
    }
}, noOfItems);

TemporalAggregator[] aggs = new TemporalAggregator[noOfItems];
for (int i = 0; i < noOfItems; i++) {
    aggs[i] = new TemporalAggregator(group.getReferenceToGroup(i), 
            new TemporalAggregatorSA(
                    new Function() {
                        protected Bid bid;
                      
                        public Object invoke(Object agg, Object next) {
                            bid = (Bid)next;
                            if (agg == null) 
                                return new BidCount(bid.getItemID(), 1);
                            return new BidCount(((BidCount)agg).itemID, ((BidCount)agg).count+1);
                        }
                    }
            )
    );
}
TemporalUnion union = new TemporalUnion(aggs, new TemporalUnionSA(noOfItems));
union.setTimeOut(5*lifespan);

TemporalAggregator max = new TemporalAggregator(union, 
        new TemporalAggregatorSA(
                new Function() {
                    protected BidCount bidCount;
                    protected LinkedList maxima;
                    protected long maxValue;
                    
                    public Object invoke (Object agg, Object next) {
                        bidCount = (BidCount)next;
                        if (agg == null) {
                            maxima = new LinkedList();
                            maxima.add(bidCount);
                            return maxima;
                        }
                        maxima = (LinkedList)agg;
                        maxValue = ((BidCount)maxima.getFirst()).count;
                        if (bidCount.count > maxValue) {
                            maxima = new LinkedList();
                            maxima.add(bidCount);
                            return maxima;
                        }
                        if (bidCount.count == maxValue) {
                            List l = (List)maxima.clone();
                            l.add(bidCount);
                            return l;
                        }
                        return maxima.clone();
                    }
                }
        )
);        


sink = new SWTSink(max, "SWTSink", true, tf, SWT.MIN);
sink2 = new SWTSink(window, "Window", true, tf, SWT.MIN);
sink3 = new SWTSink(group.getReferenceToGroup(42), "Group 42", true, tf, SWT.MIN);

union.setMetaDataFactory(
	new MetaDataFactory(union, DEFAULT_METADATA2)
);
	
SWTPerformanceMonitor monitor = new SWTPerformanceMonitor((CompositeMetaData)union.getMetaData(), tf, SWT.NONE);					
