Package org.eclipse.m2m.atl.emftvm.util
Class LazyCollection<E>
- java.lang.Object
-
- org.eclipse.m2m.atl.emftvm.util.LazyCollection<E>
-
- Type Parameters:
E- the collection element type
- All Implemented Interfaces:
java.lang.Iterable<E>,java.util.Collection<E>
- Direct Known Subclasses:
LazyBag,LazyList,LazyOrderedSet,LazySet
public abstract class LazyCollection<E> extends java.lang.Object implements java.util.Collection<E>ImmutableCollectionthat supports lazy evaluation. Based on the OCL 2.2 specification (formal/2010-02-01).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classLazyCollection.AppendIteratorIteratorthat appends an object to the underlying collection.classLazyCollection.CachingIteratorLazyCollection.ReadOnlyIteratorthat caches values of the underlying collection.classLazyCollection.CachingSetIteratorLazyCollection.ReadOnlyIteratorthat removes duplicate values from the underlying collection.static classLazyCollection.CollectIterator<T>LazyCollection.ReadOnlyIteratorthat collects the results of evaluating a function on each of the elements from the underlying collection.classLazyCollection.ExcludingIteratorLazyCollection.ReadOnlyIteratorthat excludes a given object.classLazyCollection.FilterIteratorLazyCollection.ReadOnlyIteratorthat filters elements from the underlying collection by evaluating a condition function on them.static classLazyCollection.FlattenIteratorRecursively flattens any nestedIterables by iterating over their elements as well.static classLazyCollection.FlattenSetIteratorRecursively flattens any nestedIterables by iterating over their elements as well.static classLazyCollection.IntegerRangeListIteratorLazyCollection.ReadOnlyIteratorforIntegerranges.classLazyCollection.IntersectionIteratorLazyCollection.ReadOnlyIteratorthat returns only elements contained in both underlying collections.classLazyCollection.IteratorToListIteratorConverts anIteratorto aListIterator.static classLazyCollection.LongRangeListIteratorLazyCollection.ReadOnlyIteratorforLongranges.static classLazyCollection.ReadOnlyIterator<E>IteratorwithoutLazyCollection.ReadOnlyIterator.remove().static classLazyCollection.ReadOnlyListIterator<E>classLazyCollection.RejectIteratorLazyCollection.FilterIteratorthat rejects elements from the underlying collection by evaluation a condition function on them.classLazyCollection.ReverseIteratorLazyCollection.ReadOnlyIteratorthat returns the values of the underlying collection in reverse order.classLazyCollection.ReverseListIteratorLazyCollection.ReadOnlyIteratorthat returns the values of the underlying collection in reverse order.classLazyCollection.SelectIteratorLazyCollection.FilterIteratorthat selects elements from the underlying collection by evaluation a condition function on them.classLazyCollection.SubListIteratorIteratorthat iterates over a subrange of the underlyingLazyList's contents.classLazyCollection.SubListListIteratorIteratorthat iterates over a subrange of the underlyingLazyList's contents.classLazyCollection.SubtractionIteratorLazyCollection.ReadOnlyIteratorthat returns elements contained in this underlying collection, but not in the other underlying collection.classLazyCollection.UnionIteratorIteratorthat returns first the elements of the underlying collection, then the elements of the other collection.classLazyCollection.UnionSetIteratorIteratorthat returns first the elements of the underlying collection, then the elements of the other collection, with all duplicates removed.classLazyCollection.WrappedIteratorAbstractLazyCollection.ReadOnlyIteratorthat wraps around the underlying collection.classLazyCollection.WrappedListIteratorAbstractLazyCollection.ReadOnlyListIteratorthat wraps around the underlying collection.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Collection<E>cacheElement cache.protected java.lang.Iterable<E>dataSourceThe inner data source for this collection.protected java.util.Map<E,java.lang.Integer>occurrencesCached element occurrence map.
-
Constructor Summary
Constructors Constructor Description LazyCollection()Creates an emptyLazyCollection.LazyCollection(java.lang.Iterable<E> dataSource)Creates aLazyCollectionarounddataSource.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanadd(E o)Unsupported.booleanaddAll(java.util.Collection<? extends E> c)Unsupported.Eany(CodeBlock body)Returns any element in the source collection for whichbodyevaluates totrue.protected java.lang.StringBufferappendElements(java.lang.StringBuffer buf, ExecEnv env)Appends the elements of this collection tobuf.LazyBag<E>asBag()Returns the Bag containing all the elements from self, including duplicates.LazyOrderedSet<E>asOrderedSet()Returns an OrderedSet that contains all the elements from self, in undefined order, with duplicates removed.LazyList<E>asSequence()Returns a Sequence that contains all the elements from self, in undefined order.LazySet<E>asSet()Returns the Set containing all the elements from self, with duplicated removed.abstract java.lang.StringasString(ExecEnv env)Evaluates the collection as an OCL String.voidclear()Unsupported.booleancontains(java.lang.Object o)booleancontainsAll(java.util.Collection<?> c)booleancontainsAny(java.util.Collection<?> c)Checks if this collection contains any of the elements in the specified collection.intcount(java.lang.Object object)Returns the number of times thatobjectoccurs in the collection self.protected voidcreateCache()Creates the cache collections.booleanexcludes(java.lang.Object object)Returnstrueifobjectis not an element of self,falseotherwise.booleanexcludesAll(java.util.Collection<?> c2)Does self contain none of the elements ofc2?abstract LazyCollection<E>excluding(java.lang.Object object)Returns the collection containing all elements of self apart from all occurrences ofobject.abstract LazyCollection<E>excludingAll(java.util.Collection<?> coll)Returns the collection containing all elements of self minuscoll.booleanexists(CodeBlock condition)Checks ifconditionholds for at least one element in the underlying collection.booleanexists2(CodeBlock condition)Checks ifconditionholds for at least one element pair in the cartesian product of the underlying collection.booleanforAll(CodeBlock condition)Checks ifconditionholds for all elements in the underlying collection.booleanforAll2(CodeBlock condition)Checks ifconditionholds for all element pairs in the cartesian product of the underlying collection.booleanincludes(java.lang.Object object)Returnstrueifobjectis an element of self,falseotherwise.booleanincludesAll(java.util.Collection<?> c2)Does self contain all the elements ofc2?abstract LazyCollection<E>including(E object)Returns the collection containing all elements of self plusobject.abstract LazyCollection<E>including(E object, int index)Returns the collection containing all elements of self plusobject.abstract LazyCollection<E>includingAll(java.util.Collection<? extends E> coll)Returns the collection containing all elements of self pluscoll.abstract LazyCollection<E>includingAll(java.util.Collection<? extends E> coll, int index)Returns the collection containing all elements of self pluscoll.abstract LazyCollection<E>includingRange(E first, E last)Returns the collection containing all elements of self plus the collection offirstrunning tolast.booleanisEmpty()booleanisUnique(CodeBlock body)Results intrueifbodyevaluates to a different value for each element in the source collection; otherwise, result isfalse.<T> Titerate(T initialValue, CodeBlock updater)Builds an accumulated value by calling theupdaterfunction for each element in the underlying collection and the previous accumulated value.java.util.Iterator<E>iterator()java.util.Map<java.lang.Object,LazySet<E>>mappedBy(CodeBlock body)Returns a Map indexed by the return value(s)xof the body expression, containing the Set of elements for which the body expression returnsx.java.util.Map<java.lang.Object,E>mappedBySingle(CodeBlock body)Returns a Map indexed by the return value(s)xof the body expression, containing a single element for which the body expression returnsx.Emax()The element with the maximum value of all elements in self.Emin()The element with the minimum value of all elements in self.booleannotEmpty()Is self not the empty collection?booleanone(CodeBlock body)Results intrueif there is exactly one element in the source collection for whichbodyistrue.<T> LazySet<Tuple>product(java.lang.Iterable<T> c2)The cartesian product operation ofselfandc2.booleanremove(java.lang.Object o)Unsupported.booleanremoveAll(java.util.Collection<?> c)Unsupported.booleanretainAll(java.util.Collection<?> c)Unsupported.intsize()abstract LazyCollection<E>sortedBy(CodeBlock body)Results in the Collection containing all elements of the source collection.Esum()The addition of all elements in self.java.lang.Object[]toArray()<T> T[]toArray(T[] a)java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
dataSource
protected java.lang.Iterable<E> dataSource
The inner data source for this collection. Is set tonullwhen cache is complete.
-
cache
protected java.util.Collection<E> cache
Element cache.
-
occurrences
protected java.util.Map<E,java.lang.Integer> occurrences
Cached element occurrence map.
-
-
Constructor Detail
-
LazyCollection
public LazyCollection(java.lang.Iterable<E> dataSource)
Creates aLazyCollectionarounddataSource.- Parameters:
dataSource- the underlying collection
-
LazyCollection
public LazyCollection()
Creates an emptyLazyCollection.
-
-
Method Detail
-
createCache
protected void createCache()
Creates the cache collections.
-
add
public boolean add(E o)
Unsupported.- Specified by:
addin interfacejava.util.Collection<E>- Parameters:
o- the element to add- Returns:
- nothing
- Throws:
java.lang.UnsupportedOperationException
-
addAll
public boolean addAll(java.util.Collection<? extends E> c)
Unsupported.- Specified by:
addAllin interfacejava.util.Collection<E>- Parameters:
c- the collection to add- Returns:
- nothing
- Throws:
java.lang.UnsupportedOperationException
-
clear
public void clear()
Unsupported.- Specified by:
clearin interfacejava.util.Collection<E>- Throws:
java.lang.UnsupportedOperationException
-
contains
public boolean contains(java.lang.Object o)
- Specified by:
containsin interfacejava.util.Collection<E>
-
containsAll
public boolean containsAll(java.util.Collection<?> c)
- Specified by:
containsAllin interfacejava.util.Collection<E>
-
containsAny
public boolean containsAny(java.util.Collection<?> c)
Checks if this collection contains any of the elements in the specified collection.- Parameters:
c- collection to be checked for containment in this collection.- Returns:
trueif this collection contains any of the elements in the specified collection.
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Collection<E>
-
iterator
public java.util.Iterator<E> iterator()
-
remove
public boolean remove(java.lang.Object o)
Unsupported.- Specified by:
removein interfacejava.util.Collection<E>- Parameters:
o- the element to remove- Returns:
- nothing
- Throws:
java.lang.UnsupportedOperationException
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
Unsupported.- Specified by:
removeAllin interfacejava.util.Collection<E>- Parameters:
c- the collection to remove- Returns:
- nothing
- Throws:
java.lang.UnsupportedOperationException
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
Unsupported.- Specified by:
retainAllin interfacejava.util.Collection<E>- Parameters:
c- the collection to retain- Returns:
- nothing
- Throws:
java.lang.UnsupportedOperationException
-
size
public int size()
- Specified by:
sizein interfacejava.util.Collection<E>
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection<E>
-
toArray
public <T> T[] toArray(T[] a)
- Specified by:
toArrayin interfacejava.util.Collection<E>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
asString
public abstract java.lang.String asString(ExecEnv env)
Evaluates the collection as an OCL String.- Parameters:
env- the execution environment- Returns:
- the String representation of this
LazyCollection.
-
appendElements
protected java.lang.StringBuffer appendElements(java.lang.StringBuffer buf, ExecEnv env)Appends the elements of this collection tobuf.- Parameters:
buf- theStringBufferto add the elements' string representation toenv- the execution environment- Returns:
- buf
-
includes
public boolean includes(java.lang.Object object)
Returnstrueifobjectis an element of self,falseotherwise.- Parameters:
object- the object to check for- Returns:
trueifobjectis an element of self,falseotherwise.
-
excludes
public boolean excludes(java.lang.Object object)
Returnstrueifobjectis not an element of self,falseotherwise.- Parameters:
object- the object to check for- Returns:
trueifobjectis not an element of self,falseotherwise.
-
count
public int count(java.lang.Object object)
Returns the number of times thatobjectoccurs in the collection self.- Parameters:
object- the object to check for- Returns:
- The number of times that
objectoccurs in the collection self.
-
includesAll
public boolean includesAll(java.util.Collection<?> c2)
Does self contain all the elements ofc2?- Parameters:
c2- the collection to check- Returns:
trueiff self contains all elements ofc2.
-
excludesAll
public boolean excludesAll(java.util.Collection<?> c2)
Does self contain none of the elements ofc2?- Parameters:
c2- the collection to check- Returns:
trueiff self contains no elements ofc2.
-
notEmpty
public boolean notEmpty()
Is self not the empty collection?- Returns:
falseiff self contains no elements.
-
max
public E max()
The element with the maximum value of all elements in self. Elements must be of a type supporting the max operation. The max operation - supported by the elements - must take one parameter of typeEand be both associative and commutative. Integer and Real fulfill this condition.- Returns:
- The element with the maximum value of all elements in self.
-
min
public E min()
The element with the minimum value of all elements in self. Elements must be of a type supporting the min operation. The min operation - supported by the elements - must take one parameter of type T and be both associative and commutative. Integer and Real fulfill this condition.- Returns:
- The element with the minimum value of all elements in self.
-
sum
public E sum()
The addition of all elements in self. Elements must be of a type supporting the + operation. The + operation must take one parameter of typeEand be both associative: (a+b)+c = a+(b+c), and commutative: a+b = b+a. Integer and Real fulfill this condition.- Returns:
self->iterate( elem; acc : E = 0 | acc + elem )
-
product
public <T> LazySet<Tuple> product(java.lang.Iterable<T> c2)
The cartesian product operation ofselfandc2.Lazy operation.
- Type Parameters:
T- the element type- Parameters:
c2- the other factor in the cartesian product- Returns:
self->iterate (e1; acc: Set(Tuple(first: T, second: T2)) = Set{} |
c2->iterate (e2; acc2: Set(Tuple(first: T, second: T2)) = acc |
acc2->including (Tuple{first = e1, second = e2}) ) )
-
asBag
public LazyBag<E> asBag()
Returns the Bag containing all the elements from self, including duplicates.Lazy operation.
- Returns:
- The Bag containing all the elements from self, including duplicates.
-
asSequence
public LazyList<E> asSequence()
Returns a Sequence that contains all the elements from self, in undefined order.Lazy operation.
- Returns:
- A Sequence that contains all the elements from self, in undefined order.
-
asSet
public LazySet<E> asSet()
Returns the Set containing all the elements from self, with duplicated removed.Lazy operation.
- Returns:
- The Set containing all the elements from self, with duplicated removed.
-
asOrderedSet
public LazyOrderedSet<E> asOrderedSet()
Returns an OrderedSet that contains all the elements from self, in undefined order, with duplicates removed.Lazy operation.
- Returns:
- An OrderedSet that contains all the elements from self, in undefined order, with duplicates removed.
-
including
public abstract LazyCollection<E> including(E object)
Returns the collection containing all elements of self plusobject.Lazy operation.
- Parameters:
object- the object to include- Returns:
- The collection containing all elements of self plus
object.
-
including
public abstract LazyCollection<E> including(E object, int index)
Returns the collection containing all elements of self plusobject.Lazy operation.
- Parameters:
object- the object to includeindex- the index at which to insertcoll(starting at 1)- Returns:
- The collection containing all elements of self plus
object.
-
includingAll
public abstract LazyCollection<E> includingAll(java.util.Collection<? extends E> coll)
Returns the collection containing all elements of self pluscoll.Lazy operation.
- Parameters:
coll- the collection to include- Returns:
- The collection containing all elements of self plus
coll.
-
includingAll
public abstract LazyCollection<E> includingAll(java.util.Collection<? extends E> coll, int index)
Returns the collection containing all elements of self pluscoll.Lazy operation.
- Parameters:
coll- the collection to includeindex- the index at which to insertcoll(starting at 1)- Returns:
- The collection containing all elements of self plus
coll.
-
excluding
public abstract LazyCollection<E> excluding(java.lang.Object object)
Returns the collection containing all elements of self apart from all occurrences ofobject.Lazy operation.
- Parameters:
object- the object to exclude- Returns:
- The collection containing all elements of self apart from all occurrences of
object.
-
excludingAll
public abstract LazyCollection<E> excludingAll(java.util.Collection<?> coll)
Returns the collection containing all elements of self minuscoll.Lazy operation.
- Parameters:
coll- the collection to exclude- Returns:
- The collection containing all elements of self minus
coll.
-
includingRange
public abstract LazyCollection<E> includingRange(E first, E last)
Returns the collection containing all elements of self plus the collection offirstrunning tolast.Lazy operation.
- Parameters:
first- the first object of the range to includelast- the last object of the range to include- Returns:
- The collection containing all elements of self plus the collection of
firstrunning tolast
-
forAll
public boolean forAll(CodeBlock condition)
Checks ifconditionholds for all elements in the underlying collection.- Parameters:
condition- the condition function- Returns:
trueiffconditionholds for all elements in the underlying collection.
-
forAll2
public boolean forAll2(CodeBlock condition)
Checks ifconditionholds for all element pairs in the cartesian product of the underlying collection.- Parameters:
condition- the condition function- Returns:
trueiffconditionholds for all element pairs in the cartesian product of the underlying collection.
-
exists
public boolean exists(CodeBlock condition)
Checks ifconditionholds for at least one element in the underlying collection.- Parameters:
condition- the condition function- Returns:
trueiffconditionholds for at least one element in the underlying collection.
-
exists2
public boolean exists2(CodeBlock condition)
Checks ifconditionholds for at least one element pair in the cartesian product of the underlying collection.- Parameters:
condition- the condition function- Returns:
trueiffconditionholds for at least one element pair in the cartesian product of the underlying collection.
-
iterate
public <T> T iterate(T initialValue, CodeBlock updater)Builds an accumulated value by calling theupdaterfunction for each element in the underlying collection and the previous accumulated value.- Type Parameters:
T- the element type- Parameters:
initialValue- the initial accumulated valueupdater- the updater function- Returns:
- The accumulated value
-
isUnique
public boolean isUnique(CodeBlock body)
Results intrueifbodyevaluates to a different value for each element in the source collection; otherwise, result isfalse.- Parameters:
body- the code to execute on each element- Returns:
trueifbodyevaluates to a different value for each element in the source collection.
-
any
public E any(CodeBlock body) throws java.util.NoSuchElementException
Returns any element in the source collection for whichbodyevaluates totrue. If there is more than one element for whichbodyistrue, one of them is returned. There must be at least one element fulfillingbody.- Parameters:
body- the function to evaluate on each element- Returns:
- any element in the source collection for which body evaluates to
true. - Throws:
java.util.NoSuchElementException- if there is no element in the source collection for whichbodyistrue.
-
one
public boolean one(CodeBlock body)
Results intrueif there is exactly one element in the source collection for whichbodyistrue.- Parameters:
body- the function to evaluate on each element- Returns:
trueif there is exactly one element in the source collection for whichbodyistrue.
-
sortedBy
public abstract LazyCollection<E> sortedBy(CodeBlock body)
Results in the Collection containing all elements of the source collection. The element for which body has the lowest value comes first, and so on. The type of the body expression must have the<operation defined. The<operation must return a Boolean value and must be transitive (i.e., ifa < bandb < cthena < c).- Parameters:
body- the function to evaluate on each element- Returns:
- the sorted collection
-
mappedBy
public java.util.Map<java.lang.Object,LazySet<E>> mappedBy(CodeBlock body)
Returns a Map indexed by the return value(s)xof the body expression, containing the Set of elements for which the body expression returnsx.- Parameters:
body- the function to evaluate on each element- Returns:
- the Map
-
mappedBySingle
public java.util.Map<java.lang.Object,E> mappedBySingle(CodeBlock body)
Returns a Map indexed by the return value(s)xof the body expression, containing a single element for which the body expression returnsx.- Parameters:
body- the function to evaluate on each element- Returns:
- the Map
-
-