/* ********************************************************************** ** ** 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 */ public abstract class BatchedBuffer { private int fBatchInterval; private List fBuffer; private Job fBufferProcessor; /** * @param batchInterval */ protected BatchedBuffer(int batchInterval) { fBatchInterval = batchInterval; fBuffer = new ArrayList(); fBufferProcessor = createBufferProcessor(); } private Job createBufferProcessor() { Job job = new Job("Batched Buffer Processor") { @Override protected IStatus run(IProgressMonitor monitor) { synchronized (fBuffer) { receive(new HashSet(fBuffer)); fBuffer.clear(); } return Status.OK_STATUS; } }; job.setSystem(true); job.setUser(false); return job; } /** * @param objects */ public abstract void receive(Set objects); /** * @param objects */ public void add(Set objects) { synchronized (fBuffer) { /* New Batch */ if (fBuffer.isEmpty()) { fBuffer.addAll(objects); fBufferProcessor.schedule(fBatchInterval); } /* Existing Batch */ else { fBuffer.addAll(objects); } } } }