Skip to content

Collection

Collection operations allow you to query and constrain collections of objects.

AnyExpression

Returns the first element that validates the given expression.

Signature

txt
any(expr : OclExpression) : T

Example

txt
self.collection->any(i < 2)

AppendExpression

Appends the given element to the given collection and returns the extended collection.

Signature

txt
append(elem : T) : Collection<T>

Example

txt
self.collection->append("string")

AsSetExpression

Returns the given collection as a set, containing unique entries.

Signature

txt
asSet() : Collection

Example

txt
self.collection->asSet()

AtExpression

Returns the element of the collection at the given index. Index starts at 1.

Signature

txt
at(index : Number) : T

Example

txt
self.collection->at(2)

CollectExpression

When we want to specify a collection that is derived from some other collection, but which contains different objects from the original collection (i.e., it is not a sub-collection), we can use a collect operation. The collect operation uses the same syntax as the select and reject.

Signature

txt
collect(expr : OclExpression) : Collection

Example

txt
self.children->collect(age)

ExistsExpression

Checks whether a collection contains an element satisfying the given expression.

Signature

txt
exists(expr : OclExpression) : Boolean

Example

txt
self.collection->exists(i | i < 2)

FirstExpression

Returns the first element of the collection.

Signature

txt
collection->first() : T

Example

txt
self.collection->first()

ForAllExpression

Many times a constraint is needed on all elements of a collection. The forAll operation in OCL allows specifying a Boolean expression, which must hold for all objects in a collection.

Signature

txt
forAll(expr : oclExpression)

Example

txt
self.employee->forAll(p | p.age <= 65)

IncludesExpression

Tests if the collection contains the given element. Returns true if the element exists in the collection, false otherwise.

Signature

txt
includes(object : T) : Boolean

Example

txt
self.employees->collect(id)->includes(self.managerId)

Example

txt
context Company inv: self.availableProducts->includes(self.featuredProduct)

IncludesAllExpression

Tests if the collection contains all elements from the given collection. Returns true if all elements from the provided collection exist in the source collection, false otherwise.

This operation checks if the source collection is a superset of the provided collection.

Signature

txt
includesAll(c : Collection(T)) : Boolean

Example

txt
self.employee.skills->includesAll(self.project.requiredSkills)

Example

txt
context Library inv: self.availableBooks->collect(isbn)->includesAll(self.requiredReadingList)

ExcludesExpression

Tests if the collection does not contain the given element. Returns true if the element does not exist in the collection, false otherwise.

This is the inverse of the includes operation.

Signature

txt
excludes(object : T) : Boolean

Example

txt
self.employees->excludes(terminatedEmployee)

Example

txt
context Order inv: self.items->collect(product)->excludes(self.bannedProduct)

ExcludesAllExpression

Tests if the collection does not contain any elements from the given collection. Returns true if none of the elements from the provided collection exist in the source collection, false otherwise.

This operation checks if the source collection and the provided collection are disjoint (have no common elements).

Signature

txt
excludesAll(c : Collection(T)) : Boolean

Example

txt
self.activeEmployees->excludesAll(self.terminatedEmployees)

Example

txt
context Project inv: self.approvedVendors->excludesAll(self.blacklistedVendors)

IsEmptyExpression

Returns true if the collection is empty, false otherwise.

Signature

txt
isEmpty() : Boolean

Example

txt
self.cars->isEmpty()

IsUniqueExpression

Returns true if the given expression evaluates to only different values across the collection.

Signature

txt
isUnique(expr : oclExpression) : boolean

Example

txt
self.collection->isUnique(self > 3)

LastExpression

Returns the last element of the collection.

Signature

txt
last() : T

Example

txt
self.collection->last()

NotEmptyExpression

Returns true if the collection is not empty, false otherwise.

Signature

txt
notEmpty() : Boolean

Example

txt
self.cars->notEmpty()

OneExpression

Returns true if exactly one element matches the given expression.

Signature

txt
one(expr : oclExpression) : boolean

Example

txt
self.collection->one(age < 18)

RejectExpression

The reject operation specifies a subset of a collection. A reject is an operation on a collection and is specified using the arrow-syntax. This results in a collection that removes all the elements from collection for which the boolean-expression evaluates to true.

Signature

txt
reject(expr : oclExpression) : Collection

Example

txt
self.customer->reject(underage)

SelectExpression

The select operation specifies a subset of a collection. A select is an operation on a collection and is specified using the arrow-syntax. This results in a collection that contains all the elements from collection for which the boolean-expression evaluates to true.

Signature

txt
select(expr : oclExpression) : Collection

Example

txt
self.collection->select(item | item.name = "random")

SizeExpression

Returns the number of elements in the collection.

Signature

txt
size() : Number

Example

txt
self.collection->size()

SumExpression

Returns the sum of all elements in the collection (requires + support).

Signature

txt
sum() : Number

Example

txt
self.jobs.salary->sum()

UnionExpression

Returns a collection containing all elements of self and all elements of the passed-in collection.

Signature

txt
union(c : Collection) : Collection

Example

txt
self.collection->union(self.anotherCollection)

Released under the MIT License.