All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.emc.mongoose.api.common.supply.BasicUpdatingValueSupplier Maven / Gradle / Ivy

There is a newer version: 4.0.0-alpha5
Show newest version
package com.emc.mongoose.api.common.supply;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 Created by kurila on 10.02.16.
 */
public class BasicUpdatingValueSupplier
implements BatchSupplier {
	
	protected final T initialValue;
	protected volatile T lastValue = null;
	private Callable updateAction;
	
	public BasicUpdatingValueSupplier(final T initialValue, final Callable updateAction) {
		this.initialValue = initialValue;
		this.updateAction = updateAction;
		reset();
	}

	@Override
	public T get()  {
		final T prevValue = lastValue;
		try {
			lastValue = updateAction.call();
		} catch(final Exception e) {
			throw new RuntimeException(e);
		}
		return prevValue ;
	}
	
	@Override
	public int get(final List buffer, final int limit) {
		int count = 0;
		try {
			for(; count < limit; count ++) {
				buffer.add(lastValue);
				lastValue = updateAction.call();
			}
		} catch(final Exception e) {
			throw new RuntimeException(e);
		}
		return count;
	}
	
	@Override
	public long skip(final long count) {
		try {
			for(int i = 0; i < count; i++) {
				lastValue = updateAction.call();
			}
		} catch(final Exception e) {
			throw new RuntimeException(e);
		}
		return count;
	}
	
	@Override
	public void reset() {
		lastValue = initialValue;
	}
	
	@Override
	public void close()
	throws IOException {
		lastValue = null;
		updateAction = null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy