net.jqwik.api.lifecycle.Store Maven / Gradle / Ivy
package net.jqwik.api.lifecycle;
import java.util.function.*;
import org.apiguardian.api.*;
import net.jqwik.api.*;
import static org.apiguardian.api.API.Status.*;
/**
* Experimental feature. Not ready for public usage yet.
*/
@API(status = EXPERIMENTAL, since = "1.2.3")
public interface Store {
T get();
Lifespan lifespan();
void update(Function updater);
void reset();
@API(status = EXPERIMENTAL, since = "1.2.4")
Store onClose(Consumer onCloseCallback);
@API(status = INTERNAL)
abstract class StoreFacade {
private static final Store.StoreFacade implementation;
static {
implementation = FacadeLoader.load(Store.StoreFacade.class);
}
public abstract Store create(Object identifier, Lifespan visibility, Supplier initializer);
public abstract Store get(Object identifier);
public abstract Store free(Supplier initializer);
}
/**
* Create a new store for storing and retrieving values and objects in lifecycle
* hooks and lifecycle-dependent methods.
*
* @param The type of object to store
* @param identifier Any object to identify a store. Must be globally unique.
* @param lifespan A stored object's lifespan
* @param initializer Supplies the value to be used for initializing the store depending on its lifespan
* @return New store instance
*/
static Store create(Object identifier, Lifespan lifespan, Supplier initializer) {
return StoreFacade.implementation.create(identifier, lifespan, initializer);
}
/**
* Find an existing store or create a new one if it doesn't exist
*
* @param The type of object to store
* @param identifier Any object to identify a store. Must be globally unique.
* @param lifespan
* @param initializer Supplies the value to be used for initializing the store depending on its lifespan
* @return New or existing store instance
*/
static Store getOrCreate(Object identifier, Lifespan lifespan, Supplier initializer) {
try {
Store store = Store.get(identifier);
if (!store.lifespan().equals(lifespan)) {
String message = String.format(
"Trying to recreate existing store [%s] with different lifespan [%s]",
store,
lifespan
);
throw new JqwikException(message);
}
return store;
} catch (CannotFindStoreException cannotFindStore) {
return Store.create(identifier, lifespan, initializer);
}
}
/**
* Retrieve a store that must be created somewhere else.
*
* @param identifier Any object to identify a store. Must be globally unique.
* @param The type of object to store
* @return Existing store instance
* @throws CannotFindStoreException
*/
static Store get(Object identifier) {
return StoreFacade.implementation.get(identifier);
}
/**
* Create a "free" store, i.e. one that lives independently from a test run, property or try.
*
* @param The type of object to store
* @param initializer Supplies the value to be used for initializing the store depending on its lifespan
* @return New store instance
*/
@API(status = EXPERIMENTAL, since = "1.5.0")
static Store free(Supplier initializer) {
return StoreFacade.implementation.free(initializer);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy