package de.siphalor.was.content.resource;

import org.jetbrains.annotations.Nullable;

import java.io.InputStream;
import java.util.function.Supplier;

/**
 * An implementation for {@link Resource} that runs a callback to get the input stream.
 */
public class CallbackResource extends Resource {
	/**
	 * The callback to run for the input stream
	 */
	Supplier<InputStream> supplier;

	/**
	 * Constructs a new callback resource.
	 * @param id The id
	 * @param supplier The supplier that will provide the input stream
	 */
	public CallbackResource(String id, Supplier<InputStream> supplier) {
		super(id);
		this.supplier = supplier;
	}

	/**
	 * Runs the callback to get the input stream.
	 * @return An input stream or <code>null</code> if the callback failed
	 */
	@Override
	public @Nullable InputStream getInputStream() {
		return supplier.get();
	}
}
