/* ********************************************************************** **
** Copyright notice **
** **
** (c) 2005-2006 RSSOwl Development Team **
** http://www.rssowl.org/ **
** **
** All rights reserved **
** **
** This program and the accompanying materials are made available under **
** the terms of the Eclipse Public License v1.0 which accompanies this **
** distribution, and is available at: **
** http://www.rssowl.org/legal/epl-v10.html **
** **
** A copy is found in the file epl-v10.html and important notices to the **
** license from the team is found in the textfile LICENSE.txt distributed **
** in this package. **
** **
** This copyright notice MUST APPEAR in all copies of the file! **
** **
** Contributors: **
** RSSOwl Development Team - initial API and implementation **
** **
** ********************************************************************** */
package org.rssowl.core.util;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author bpasero
* @param <T>
*/
public abstract class BatchedBuffer<T> {
private int fBatchInterval;
private List<T> fBuffer;
private Job fBufferProcessor;
/**
* @param batchInterval
*/
protected BatchedBuffer(int batchInterval) {
fBatchInterval = batchInterval;
fBuffer = new ArrayList<T>();
fBufferProcessor = createBufferProcessor();
}
private Job createBufferProcessor() {
Job job = new Job("Batched Buffer Processor") {
@Override
protected IStatus run(IProgressMonitor monitor) {
synchronized (fBuffer) {
receive(new HashSet<T>(fBuffer));
fBuffer.clear();
}
return Status.OK_STATUS;
}
};
job.setSystem(true);
job.setUser(false);
return job;
}
/**
* @param objects
*/
public abstract void receive(Set<T> objects);
/**
* @param objects
*/
public void add(Set<T> objects) {
synchronized (fBuffer) {
/* New Batch */
if (fBuffer.isEmpty()) {
fBuffer.addAll(objects);
fBufferProcessor.schedule(fBatchInterval);
}
/* Existing Batch */
else {
fBuffer.addAll(objects);
}
}
}
}