public abstract class Repository<I,E extends Entity<I,?>> extends java.lang.Object implements Closeable, io.spine.logging.Logging
Modifier | Constructor and Description |
---|---|
protected |
Repository()
Creates the repository.
|
Modifier and Type | Method and Description |
---|---|
protected static <S extends java.lang.AutoCloseable> |
checkStorage(S storage)
Ensures that the storage is not null.
|
void |
close()
Closes the repository by closing the underlying storage.
|
protected BoundedContext |
context()
Obtains the
BoundedContext to which this repository belongs. |
abstract E |
create(I id)
Create a new entity instance with its default state.
|
protected abstract Storage<I,?,?> |
createStorage()
Creates the storage for this repository.
|
java.lang.Class<E> |
entityClass()
Returns the class of entities managed by this repository.
|
io.spine.type.TypeUrl |
entityStateType()
Obtains the
TypeUrl for the state objects wrapped by entities
managed by this repository. |
EventFilter |
eventFilter()
Creates an
EventFilter for this repository. |
abstract java.util.Optional<E> |
find(I id)
Finds an entity with the passed ID.
|
protected boolean |
hasContext()
Verifies whether the repository is registered with a
BoundedContext . |
java.lang.Class<I> |
idClass()
Returns the class of IDs used by this repository.
|
java.util.Iterator<I> |
index()
Returns an iterator over the identifiers of all the entities managed by this repository.
|
boolean |
isOpen()
Verifies if the repository is open.
|
boolean |
isRegistered()
Tells if the repository is registered in a
BoundedContext . |
java.util.Iterator<E> |
iterator(java.util.function.Predicate<E> filter)
Returns an iterator over the entities managed by the repository that match the passed filter.
|
void |
onRegistered()
The callback is invoked by a
BoundedContext when adding the repository. |
protected void |
open()
Initializes the storage of the repository.
|
protected Storage<I,?,?> |
storage()
Returns the storage assigned to this repository.
|
boolean |
storageAssigned()
Returns
true if the storage is assigned, false otherwise. |
protected abstract void |
store(E obj)
Stores the passed object.
|
public abstract E create(I id)
id
- the id of the entityprotected abstract void store(E obj)
Note: The storage must be assigned before calling this method.
obj
- an instance to storepublic abstract java.util.Optional<E> find(I id)
id
- the ID of the entity to loadOptional.empty()
if there's no entity with such IDpublic java.util.Iterator<E> iterator(java.util.function.Predicate<E> filter)
The returned iterator does not support removal.
Iteration through entities is performed by loading them one by one.
public java.util.Iterator<I> index()
public final java.lang.Class<I> idClass()
public final java.lang.Class<E> entityClass()
public final io.spine.type.TypeUrl entityStateType()
TypeUrl
for the state objects wrapped by entities
managed by this repository.public boolean isRegistered()
BoundedContext
.protected final boolean hasContext()
BoundedContext
.protected final BoundedContext context()
BoundedContext
to which this repository belongs.BoundedContext
java.lang.IllegalStateException
- if the repository has no context assigned@OverridingMethodsMustInvokeSuper public void onRegistered()
BoundedContext
when adding the repository.protected final void open()
protected final Storage<I,?,?> storage()
To verify if the storage is assigned, use Repository.storageAssigned()
.
java.lang.IllegalStateException
- if the storage is not assignedpublic final boolean storageAssigned()
true
if the storage is assigned, false
otherwise.protected static <S extends java.lang.AutoCloseable> S checkStorage(S storage)
java.lang.IllegalStateException
- if the passed instance is nullprotected abstract Storage<I,?,?> createStorage()
Default implementations use Repository.defaultStorageFactory()
invoking its method
which creates a storage compatible with the repository.
Overwrite this method for creating a custom implementation of Storage
.
@OverridingMethodsMustInvokeSuper public void close()
The reference to the storage becomes null after this call.
close
in interface java.lang.AutoCloseable
public final boolean isOpen()
@SPI @Pure public EventFilter eventFilter()
EventFilter
for this repository.
All the events posted by this repository, domain, and system are first passed through this filter.
By default, the filter allows all the events to be posted. For entities which do not allow
state subscription, the EntityStateChanged
event is
filtered out. Override this method to change this behaviour.
EventFilter
to apply to all posted events@Pure
for the details on the expected behaviour.