public interface ILcdModel extends Serializable, ILcdDisposable
Models can be created and populated by hand, or they can be created by an ILcdModelDecoder
from a data source.
ILcdModel
provides basic methods for retrieving, adding and removing elements. The basic method for
retrieving is sequential. The identities of the elements are determined by the equals()
method.
ILcdModelReference
associated with it. This model reference defines the common coordinate
system in which the geometry of its elements is expressed. All elements of a model should thus share the same
coordinate system.
ILcdModelDescriptor
associated with it. This model descriptor provides meta information
about the model and information common to all elements contained in this model. The actual contents of this
information may vary from model to model.
read lock
:
try (TLcdLockUtil.Lock lock = TLcdLockUtil.readLock(model);
Stream<Object> elements = model.query(all())) {
elements.forEach(consumer);
}
This applies to operations such as query
, elements
or applyOnInteract2DBounds
.
This also applies to accessing element internals, such as looping over points of a polyline.
Consult the javadoc of your specific model implementation for additional considerations.
add
and remove
elements.
External changes to the elements of the model (for example, the bounds of an element) have to be reported to the
model through elementChanged
, so it can update its internal structures and send
out events to its listeners.
When you do changes to the model or one of its elements, you must take a write lock
:
try (TLcdLockUtil.Lock lock = TLcdLockUtil.writeLock(model)) {
model.addElement(element, FIRE_LATER);
} finally {
model.fireCollectedModelChanges();
}
By default, you have to do this on the view's painting thread, but if this is a performance problem you can use another
thread.
Refer to the Advanced threading and locking guide for more details.
One or more ILcdModelListener
objects can be registered to a model, to be notified of changes in the model.
The model will notify its listeners whenever one or more elements are added, removed and/or changed.
These events can be sent individually, grouped or silently suppressed,depending on the
event mode that was specified with the change. Two modes for firing events are supported:
FIRE_LATER
: applies the change but delays the model change event. You have to flush the events through fireCollectedModelChanges()
outside the write lock. This is the recommended mode.NO_EVENT
: applies the change but doesn't send or delay the model change event. This can be used if the model being constructed and is not yet in a layer.
The use of ILcdFireEventMode
constants for specifying event modes in the methods of this interface has been
deprecated in favor of the constants that are defined within this interface.
See ALcdModel
for info on debugging missing or wrong model locks.
ILcdModel
and its
extensions are also provided in this package, each optimized for different types of usages.
Choosing the right interface or implementation of ILcdModel
is important for achieving proper performance.
See the package documentation
for an overview of all available extension interfaces and
implementations.
ILcdBounded
. Implementations should clearly specify in
their documentation any restrictions on the elements they may contain. The methods canAddElement(Object)
and canRemoveElement(Object)
reflect these constraints programmatically.Modifier and Type | Interface and Description |
---|---|
static class |
ILcdModel.Query
A query to be used with
query(Query) . |
Modifier and Type | Field and Description |
---|---|
static int |
FIRE_LATER
Store the event for later.
|
static int |
FIRE_NOW
Deprecated.
Don't use this event mode always use
FIRE_LATER or NO_EVENT .
If the model isn't used yet outside the code block you can use NO_EVENT .
Otherwise take a TLcdLockUtil.writeLock(Object) , use FIRE_LATER and finally call fireCollectedModelChanges()
For more information on how to work with models see the tutorial |
static int |
NO_EVENT
Don't signal the event at all.
|
Modifier and Type | Method and Description |
---|---|
void |
addElement(Object aElement,
int aEventMode)
Adds the specified element to this model.
|
void |
addElements(Vector aElements,
int aEventMode)
Adds all of the elements in the specified vector to this model.
|
void |
addModelListener(ILcdModelListener aModelListener)
Registers the specified model listener to receive notifications of model changes on this model.
|
static ILcdModel.Query |
all()
Create a query indicating that all elements should be retrieved.
|
boolean |
canAddElement(Object aElement)
Returns
true if the specified element can be added to this model, false otherwise. |
boolean |
canRemoveElement(Object aElement)
Returns
true if the specified element can be removed from this model, false otherwise. |
void |
dispose()
Disposes of this model and allows it to release any system resources that it is holding.
|
void |
elementChanged(Object aElement,
int aEventMode)
Notifies this model that the specified element has changed.
|
Enumeration |
elements()
Returns an enumeration over all elements of this model.
|
void |
elementsChanged(Vector aElements,
int aEventMode)
Notifies this model that the elements in the specified vector have changed.
|
static ILcdModel.Query |
filter(ILcdOGCCondition aCondition)
Create a query with the given condition to filter on.
|
void |
fireCollectedModelChanges()
Notifies all listeners that are registered on this model of all the changes that have been collected between the
previous notification and now.
|
ILcdModelDescriptor |
getModelDescriptor()
Returns the
ILcdModelDescriptor providing meta information about this model and
its elements. |
ILcdModelEncoder |
getModelEncoder()
Returns, if available, a model encoder that is capable of encoding this model,
(
encoder.canEncode(this) ), null otherwise. |
default TLcdModelMetadata |
getModelMetadata()
Collects and returns metadata about the model.
|
ILcdModelReference |
getModelReference()
Returns the
ILcdModelReference describing how the geometry of the elements contained in this
model should be interpreted. |
default <T> Stream<T> |
query(ILcdModel.Query aQuery)
Provides a
Stream of all elements in this model that match the given query. |
void |
removeAllElements(int aEventMode)
Removes all elements from this model.
|
void |
removeElement(Object aElement,
int aEventMode)
Removes the specified element from this model.
|
void |
removeElements(Vector aElements,
int aEventMode)
Removes all of the elements in the specified vector from this model.
|
void |
removeModelListener(ILcdModelListener aModelListener)
Unregisters the specified model listener so that it no longer receives notifications of model changes on this model.
|
close
static final int FIRE_NOW
FIRE_LATER
or NO_EVENT
.
If the model isn't used yet outside the code block you can use NO_EVENT
.
Otherwise take a TLcdLockUtil.writeLock(Object)
, use FIRE_LATER
and finally call fireCollectedModelChanges()
For more information on how to work with models see the tutorial
This mode is not recommended, as the event happens within the write lock.
Use FIRE_LATER
instead.
This constant has the same value as ILcdFireEventMode.FIRE_NOW
, for backwards compatibility.
static final int FIRE_LATER
Use fireCollectedModelChanges()
to flush any events that have been collected this way but not yet sent.
Note that you have to use fireCollectedModelChanges()
outside the write lock
.
This constant has the same value as ILcdFireEventMode.FIRE_LATER
, for backwards compatibility.
static final int NO_EVENT
Use this only if the model is not yet in use by another component. For example it is being built and not yet in a layer.
This constant has the same value as ILcdFireEventMode.NO_EVENT
, for backwards compatibility.
ILcdModelReference getModelReference()
ILcdModelReference
describing how the geometry of the elements contained in this
model should be interpreted.ILcdModelReference
of this ILcdModel
.ILcdModelDescriptor getModelDescriptor()
ILcdModelDescriptor
providing meta information about this model and
its elements.ILcdModelDescriptor
of this ILcdModel
. Should not be null
.default TLcdModelMetadata getModelMetadata()
TLcdModelMetadata
instance every time it is called.
The default implementation uses TLcdModelMetadata.Builder.fromModel(com.luciad.model.ILcdModel)
.
Implementations can recreate
or extend
the metadata.
The following example adds a data category and source file mime type:
public TLcdModelMetadata getModelMetadata() {
String source = this.getModelDescriptor().getSourceName();
return this.getModelMetadata().asBuilder()
.entryPoint(new TLcdModelMetadata.Source(source, MIME_XML))
.addDataCategory(TLcdModelMetadata.DataCategory.VECTOR)
.build();
}
ILcdModelEncoder getModelEncoder()
encoder.canEncode(this)
), null
otherwise.null
otherwise.Enumeration elements()
void addElement(Object aElement, int aEventMode)
Models that support this operation may place limitations on what elements may be added to this model. For example,
implementations that are based on a spatial indexing structure will require that elements implement
ILcdBounded
.
When adding an element, the user should make sure that canAddElement(aElement)
holds, and that the
element's geometry is expressed in the same model reference as this model. It is generally undefined what happens
if an invalid element is added.
Implementations of this interface should clearly specify in their documentation any restrictions on what elements
may be added. Although it is unspecified what happens if the preconditions are not met, implementations are
encouraged to throw meaningful exceptions (for example, NullPointerException, IllegalArgumentException,
ClassCastException, UnsupportedOperationException
), whenever possible.
aElement
- the element to be added to this model.aEventMode
- the mode for sending out the model change event. This can be FIRE_LATER
or NO_EVENT
.canAddElement(Object)
canAddElement(aElement)
).boolean canAddElement(Object aElement)
true
if the specified element can be added to this model, false
otherwise.
Note that this method generally does not validate whether the specified element is expressed in the same model reference as this model. It is the responsibility of the user of this model to make sure this precondition is fulfilled when an element is added to this model.
aElement
- the element to be verified.true
if the specified element can be added to this model, false
otherwise.void addElements(Vector aElements, int aEventMode)
ILcdBounded
. Implementations of this interface should clearly specify in their documentation any
restrictions on what elements may be added.
The behavior of this operation is undefined if the specified vector is modified while the operation is in progress.
The specified elements will be added to this model in the order they are specified in the vector. If an element cannot be added, this method will return at the first failure. Succeeding elements won't be added.
Although it is unspecified what happens if the preconditions are not met, implementations are encouraged to
throw meaningful exceptions (for example, NullPointerException, IllegalArgumentException, ClassCastException,
UnsupportedOperationException
), whenever possible.
aElements
- the vector of elements to be added to this model.aEventMode
- the mode for sending the model change events This can be FIRE_LATER
or NO_EVENT
.canAddElement(Object)
canAddElement(aElement)
).void removeElement(Object aElement, int aEventMode)
Although it is unspecified what happens if the preconditions are not met, implementations are encouraged to
throw meaningful exceptions (for example, NullPointerException, IllegalArgumentException, ClassCastException,
UnsupportedOperationException
), whenever possible.
aElement
- the element to be removed from this model.aEventMode
- the mode for sending out the model change event. This can be FIRE_LATER
or NO_EVENT
.canRemoveElement(Object)
canRemoveElement(aElement)
).boolean canRemoveElement(Object aElement)
true
if the specified element can be removed from this model, false
otherwise.
Note that this method generally does not check whether the specified element is actually contained in this model.aElement
- the element to be verified.true
if the specified element can be removed from this model, false
otherwise.void removeElements(Vector aElements, int aEventMode)
The behavior of this operation is undefined if the specified vector is modified while the operation is in progress.
The specified elements will be removed from this model in the order they are specified in the vector. If an element cannot be removed, this method will return at the first failure. Succeeding elements won't be removed.
Although it is unspecified what happens if the preconditions are not met, implementations are encouraged to
throw meaningful exceptions (for example, NullPointerException, IllegalArgumentException, ClassCastException,
UnsupportedOperationException
), whenever possible.
aElements
- the vector of elements to be removed from this model.aEventMode
- the mode for sending out the model change event. This can be FIRE_LATER
or NO_EVENT
.canRemoveElement(Object)
canRemoveElement(aElement)
).void removeAllElements(int aEventMode)
If an element cannot be removed, this method will return at the first failure. Succeeding elements won't be removed.
Although it is unspecified what happens if the preconditions are not met, implementations are encouraged to
throw meaningful exceptions (for example, NullPointerException, IllegalArgumentException, ClassCastException,
UnsupportedOperationException
), whenever possible.
aEventMode
- the mode for sending out the model change event. This can be FIRE_LATER
or NO_EVENT
.void elementChanged(Object aElement, int aEventMode)
aElement
- the element that has changed.aEventMode
- the mode for sending out the model change event. This can be FIRE_LATER
or NO_EVENT
.void elementsChanged(Vector aElements, int aEventMode)
aElements
- the vector of elements that have changed.aEventMode
- the mode for sending out the model change event. This can be FIRE_LATER
or NO_EVENT
.NullPointerException
- if the specified vector is null
.void fireCollectedModelChanges()
void addModelListener(ILcdModelListener aModelListener)
Registers the specified model listener to receive notifications of model changes on this model.
Model changes are sent out when an element has been added, removed or changed. Model changes can be sent out
individually, grouped or silently applied without notifications, depending on the ILcdFireEventMode
that was specified with the change.
In case you need to register a listener which keeps a reference to an object with a shorter life-time than this model,
you can use a ALcdWeakModelListener
instance as model listener.
aModelListener
- the ILcdModelListener
to register on this model.NullPointerException
- if the specified listener is null
.removeModelListener(com.luciad.model.ILcdModelListener)
,
ILcdModelListener
void removeModelListener(ILcdModelListener aModelListener)
aModelListener
- the ILcdModelListener
to remove.NullPointerException
- if the specified listener is null
.addModelListener(com.luciad.model.ILcdModelListener)
,
ILcdModelListener
void dispose()
finalize
) on this model subsequent to a call to
this method is undefined.dispose
in interface ILcdDisposable
default <T> Stream<T> query(ILcdModel.Query aQuery)
Stream
of all elements in this model that match the given query.
The query aspects are applied in this specific order, regardless of the order used to create the query:
filter
is applied first (if any).sorting
is applied second (if any).limit
is applied last (if any).condition
and sort-by
must never change after creation.
Examples:
query
(all
());query
(filter
(new TLcdOGCBBoxOperator(...)));query
(filter
(eq(property("name"), literal("Leuven")));query
(all
().sorted
(comparing(property("population"), DESC));query
(all
().limit
(150));query
(filter
(and(bboxCondition, ogcCondition)).sorted
(sortByPopulation).limit
(120));
A stream
is closeable
, and it depends on the implementation whether the stream
has to be closed or not.
You should use this template to ensure proper model locking and stream closing:
try (TLcdLockUtil.Lock autoUnlock = TLcdLockUtil.readLock(model);
Stream elements = model.query(all())) {
elements.forEach(System.err::println);
}
Model implementations:
By default, this method will invoke elements()
, loop over all elements and apply the filter, sorting and limit.
For ILcd2DBoundsIndexedModel
, this method will invoke applyOnInteract2DBounds()
if the condition contains a spatial component (either a bounding-box operator, or a spatial operator), and apply the rest of the filter on those elements.
Model implementers can override this method to apply the query more efficiently on their back-end format.
For example, if your back-end is a service or database that accepts certain queries, you can override this method and
transform the condition into a query on that service.
You can inspect the condition using instanceof
on the various condition classes.
aQuery
- The query, cannot be null
. Pass all()
if you want all elements.TLcdOGCFilterFactory to easily create conditions
,
TLcdOGCSortBy "comparing" and "thenComparing" factory methods to easily create sorting orders
static ILcdModel.Query all()
filter
, sorted
or limit
.static ILcdModel.Query filter(ILcdOGCCondition aCondition)
sorted
or limit
.aCondition
- The condition, or null
if no filtering should be done.TLcdOGCFilterFactory to easily create conditions