public class NoopDataContext extends java.lang.Object implements DataContext
DataContext
used for read-only screens like entity browsers.DataContext.ChangeEvent, DataContext.PostCommitEvent, DataContext.PreCommitEvent
Constructor and Description |
---|
NoopDataContext() |
Modifier and Type | Method and Description |
---|---|
Subscription |
addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
Adds a listener to
DataContext.ChangeEvent . |
Subscription |
addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
Adds a listener to
DataContext.PostCommitEvent . |
Subscription |
addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
Adds a listener to
DataContext.PreCommitEvent . |
void |
clear()
Evicts all tracked entities.
|
EntitySet |
commit()
Commits changed and removed instances to the middleware.
|
boolean |
contains(Entity entity)
Returns true if the context contains the given entity (distinguished by its class and id).
|
<T extends Entity> |
create(java.lang.Class<T> entityClass)
Creates an entity instance and merge it into the context.
|
void |
evict(Entity entity)
Removes the entity from the context so the context stops tracking it.
|
void |
evictModified()
Clears the lists of created/modified/deleted entities and evicts these entities.
|
<T extends Entity<K>,K> |
find(java.lang.Class<T> entityClass,
K entityId)
Returns an entity instance by its class and id.
|
<T extends Entity> |
find(T entity)
Returns the instance of entity with the same id if it exists in this context.
|
java.util.function.Function<CommitContext,java.util.Set<Entity>> |
getCommitDelegate()
Returns a function which will be used to commit data instead of standard implementation.
|
java.util.Set<Entity> |
getModified()
Returns an immutable set of entities registered as modified.
|
DataContext |
getParent()
Returns a parent context, if any.
|
java.util.Set<Entity> |
getRemoved()
Returns an immutable set of entities registered for removal.
|
boolean |
hasChanges()
Returns true if the context has detected changes in the tracked entities.
|
boolean |
isModified(Entity entity)
Returns true if the context has detected changes in the given entity.
|
boolean |
isRemoved(Entity entity)
Returns true if the context has registered removal of the given entity.
|
EntitySet |
merge(java.util.Collection<? extends Entity> entities)
Merge the given entities into the context.
|
EntitySet |
merge(java.util.Collection<? extends Entity> entities,
MergeOptions options)
Merge the given entities into the context.
|
<T extends Entity> |
merge(T entity)
Same as
DataContext.merge(Entity, MergeOptions) with default options. |
<T extends Entity> |
merge(T entity,
MergeOptions options)
Merge the given entity into the context.
|
void |
remove(Entity entity)
Removes the entity from the context and registers it as deleted.
|
void |
setCommitDelegate(java.util.function.Function<CommitContext,java.util.Set<Entity>> delegate)
Sets a function which will be used to commit data instead of standard implementation.
|
void |
setModified(Entity entity,
boolean modified)
Registers or unregisters the given entity as modified.
|
void |
setParent(DataContext parentContext)
Sets the parent context.
|
@Nullable public <T extends Entity<K>,K> T find(java.lang.Class<T> entityClass, K entityId)
DataContext
find
in interface DataContext
public <T extends Entity> T find(T entity)
DataContext
find
in interface DataContext
public boolean contains(Entity entity)
DataContext
contains
in interface DataContext
public <T extends Entity> T merge(T entity)
DataContext
DataContext.merge(Entity, MergeOptions)
with default options.merge
in interface DataContext
public <T extends Entity> T merge(T entity, MergeOptions options)
DataContext
If an entity with the same identifier already exists in the context, the passed entity state is copied into it and the existing instance is returned. Otherwise, a copy of the passed instance is registered in the context and returned.
If the given instance is new and the context doesn't contain an instance with the same identifier, the context
will save the new instance on DataContext.commit()
. Otherwise, even if some attributes of the merged instance are changed
as a result of copying the state of the passed instance, the merged instance will not be committed. Such modifications
are considered as a result of loading more fresh state from the database.
WARNING: use the returned value because it is always a different object instance.
The only case when you get the same instance is if the input was previously returned from the same context as a
result of DataContext.find(Class, Object)
or merge()
.
merge
in interface DataContext
entity
- instance to mergeoptions
- merge optionspublic EntitySet merge(java.util.Collection<? extends Entity> entities)
DataContext
Same as DataContext.merge(Entity, MergeOptions)
but for a collection of instances.
merge
in interface DataContext
public EntitySet merge(java.util.Collection<? extends Entity> entities, MergeOptions options)
DataContext
Same as DataContext.merge(Entity)
but for a collection of instances.
merge
in interface DataContext
public void remove(Entity entity)
DataContext
DataContext.commit()
.
If the given entity is not in the context, nothing happens.
remove
in interface DataContext
public void evict(Entity entity)
DataContext
If the given entity is not in the context, nothing happens.
evict
in interface DataContext
public void evictModified()
DataContext
evictModified
in interface DataContext
public void clear()
DataContext
clear
in interface DataContext
DataContext.evict(Entity)
public <T extends Entity> T create(java.lang.Class<T> entityClass)
DataContext
Same as:
Foo foo = dataContext.merge(metadata.create(Foo.class));
create
in interface DataContext
entityClass
- entity classpublic boolean hasChanges()
DataContext
hasChanges
in interface DataContext
public boolean isModified(Entity entity)
DataContext
isModified
in interface DataContext
public void setModified(Entity entity, boolean modified)
DataContext
setModified
in interface DataContext
entity
- entity instance which is already merged into the contextmodified
- true to register or false to unregisterpublic java.util.Set<Entity> getModified()
DataContext
getModified
in interface DataContext
public boolean isRemoved(Entity entity)
DataContext
isRemoved
in interface DataContext
public java.util.Set<Entity> getRemoved()
DataContext
getRemoved
in interface DataContext
public EntitySet commit()
DataContext
commit
in interface DataContext
DataContext.setParent(DataContext)
public DataContext getParent()
DataContext
DataContext.commit()
method merges the changed instances
to it instead of sending to the middleware.getParent
in interface DataContext
public void setParent(DataContext parentContext)
DataContext
DataContext.commit()
method merges the changed instances
to it instead of sending to the middleware.setParent
in interface DataContext
public Subscription addChangeListener(java.util.function.Consumer<DataContext.ChangeEvent> listener)
DataContext
DataContext.ChangeEvent
.addChangeListener
in interface DataContext
public Subscription addPreCommitListener(java.util.function.Consumer<DataContext.PreCommitEvent> listener)
DataContext
DataContext.PreCommitEvent
.
You can also add an event listener declaratively using a controller method annotated with Subscribe
:
@Subscribe(target = Target.DATA_CONTEXT) protected void onPreCommit(DataContext.PreCommitEvent event) { // handle event here }
addPreCommitListener
in interface DataContext
listener
- listenerpublic Subscription addPostCommitListener(java.util.function.Consumer<DataContext.PostCommitEvent> listener)
DataContext
DataContext.PostCommitEvent
.
You can also add an event listener declaratively using a controller method annotated with Subscribe
:
@Subscribe(target = Target.DATA_CONTEXT) protected void onPostCommit(DataContext.PostCommitEvent event) { // handle event here }
addPostCommitListener
in interface DataContext
listener
- listenerpublic java.util.function.Function<CommitContext,java.util.Set<Entity>> getCommitDelegate()
DataContext
getCommitDelegate
in interface DataContext
public void setCommitDelegate(java.util.function.Function<CommitContext,java.util.Set<Entity>> delegate)
DataContext
setCommitDelegate
in interface DataContext