Hi all and a Happy New Year!
I thought I would suggest a couple of additions to IModel or CachedModel (which would of course be non-abstract methods to avoid breaking existing implementations) for discussion.
default boolean isLoaded() {
return true;
}
Jon, Horacio, Betty and I were discussing the idea of being able to detect whether a model has already been loaded. Currently there isn’t a general way to do this, so in some implementations a call to load() may unnecessary reload the model. CachedModel may override this to return allContentsAreCached if caching is enabled, for example.
protected Iterator<ModelElementType> iterateAllContentsFromModel() {
return allContentsFromModel().iterator();
}
The second idea was proposed by Horacio (albeit not in this exact form) which is presumably motivated by the efficiency of iterators compared to collections. Iterators can produce values lazily (EMF already uses TreeIterator, for example), whilst Collections must be populated eagerly even if only part of the collection is used. Implementations could therefore override this method to provide lazy model iteration which would be more efficient. This method could then be used to replace some allContentsFromModel invocations. We could then also add similar variants for getAllOf* (where * = Kind/Type), e.g.:
default Iterator<?> iterateAllOf*(String type) {
return getAllOf*(type).iterator();
}
Of course this would be overridden by CachedModel to use the iterator-based allContentsFromModel.
Additionally, I propose a complimentary pair of methods for getAllOf* as follows:
default Stream<?> streamAllOf*(String type) {
return getAllOf*(type).stream();
}
With the associated streamAllOf*FromModel() as well in the case of CachedModel. This would also allow for lazy model iteration but with a richer API since many queries and transformations can be performed using Streams both lazily and in parallel if desired. Models that implement iterateAllContentsFromModel and iterateAllOf* can then make use of this to create a stream as follows:
public Stream<?> streamAllOf*(String type) {
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(iterateAllOf*(type), 0),
false
);
}
Please feel free to add your thoughts / suggestions.
Thanks,
Sina