Repository /Debellor/debellor-1.0.jar:org.debellor.base.Buffer


Back

No file description

Source code

/*
 *  Debellor
 *
 *  Copyright (C) 2008-2009 by Marcin Wojnarski
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package org.debellor.base;

import java.util.ArrayList;

import org.debellor.core.Cell;
import org.debellor.core.Sample;
import org.debellor.core.Sample.SampleType;

/**
 * Buffers all input data during learning.
 * On next() iterates through these buffered samples.
 * 
 * @author Marcin Wojnarski
 *
 */
public class Buffer extends Cell {

	private SampleType type;
	private ArrayList<Sample> samples = new ArrayList<Sample>();
	private int pos;
	private Stream input;

	@Override
	protected void onLearn() throws Exception {
		input = openInputStream();
		type = input.sampleType;
		Sample s;
		while((s = input.next()) != null)
			samples.add(s);		
		input.close();
	}

	@Override
	protected void onErase() throws Exception {
		type = null;
		samples.clear();
	}


	@Override
	protected SampleType onOpen() throws Exception {
		pos = 0;
		return type;
	}

	@Override
	protected Sample onNext() throws Exception {
		if(pos >= samples.size()) 
			return null;
		return samples.get(pos++);
	}

	@Override
	protected void onClose() throws Exception {
	}
	
}

Copyright © 2008-2011 by TunedIT
Design by luksite