/*
 * GRAL: GRAphing Library for Java(R)
 *
 * (C) Copyright 2009-2016 Erich Seifert ,
 * Michael Seifert 
 *
 * This file is part of GRAL.
 *
 * GRAL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GRAL 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GRAL.  If not, see .
 */
package de.erichseifert.gral.data.filters;

import java.util.Iterator;

public class Accumulation> implements Filter {
	private final Iterable data;

	private static class AccumulationIterator implements Iterator {
		private final Iterator wrappedIterator;
		private double accumulatedValue;

		public AccumulationIterator(Iterator wrappedIterator) {
			this.wrappedIterator = wrappedIterator;
			accumulatedValue = 0.0;
		}

		@Override
		public boolean hasNext() {
			return wrappedIterator.hasNext();
		}

		@Override
		public Double next() {
			accumulatedValue += wrappedIterator.next().doubleValue();
			return Double.valueOf(accumulatedValue);
		}

		@Override
		public void remove() {
			wrappedIterator.remove();
		}
	}

	public Accumulation(Iterable data) {
		this.data = data;
	}

	@Override
	public Iterator iterator() {
		return new AccumulationIterator(data.iterator());
	}
}

 

Ads help maintain this website.