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

com.autonomouslogic.commons.cache.CachedSupplier Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package com.autonomouslogic.commons.cache;

import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;

/**
 * Caches the result of a supplier, ensuring the underlying delegate is only called once.
 * @param  the type
 */
@RequiredArgsConstructor
public class CachedSupplier implements Supplier {
	private final Supplier delegate;
	private boolean fetched;
	private T value;

	/**
	 * Returns the result of the delegate supplier, or the cached value if present.
	 * @return the supplier value
	 */
	@Override
	public T get() {
		if (!fetched) {
			value = delegate.get();
			fetched = true;
		}
		return value;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy