Ad/2010-08-01 Concrete Syntax for a uml action Language for Foundational uml (Alf) Second Revised Submission


Alf Standard Library Collection Classes Implementation



Download 1.74 Mb.
Page59/62
Date28.01.2017
Size1.74 Mb.
#9041
1   ...   54   55   56   57   58   59   60   61   62

Alf Standard Library Collection Classes Implementation


This example provides a sample implementation for the Alf Standard Library CollectionClasses package in Alf itself. As discussed in Subclause 11.6, implementations for these classes are supplied in the nested CollectionClasses::Impl package. Annex B.4.1 presents the Alf representation of the CollectionClasses::Impl ipackage, with the following subclauses giving Alf units for each of the classes contained in this package.

Note that this is not intended to be an efficient, production implementation of the collection class package. Rather, it is intended to provide a concrete example of the use of the Impl mechanism for the collection classes and to demonstrate that all the behavior specified for the collection classes can be implemented in Alf using other features.


      1. CollectionClasses::Impl


The CollectionClasses::Impl package is required to contain the classes shown in Figure 11 -78. The Alf implementation given for the package below also includes two additional classes that are used to factor out common behavior from the collection classes (other than Map). The package is also marked as a model library, so that it does not automatically import the Alf standard library itself (see Subclause 10.1).

namespace Alf::Library::Collections::CollectionClasses;


@ModelLibrary

package Impl {


private class CollectionImpl;

private class OrderedCollectionImpl specializes CollectionImpl;


public class Set

specializes CollectionImpl, CollectionClasses::Set;

public class Bag

specializes CollectionImpl, CollectionClasses::Bag;


public class OrderedSet

specializes OrderedCollectionImpl, CollectionClasses::OrderedSet;

public class List

specializes OrderedCollectionImpl, CollectionClasses::List;

public class Queue

specializes CollectionImpl, CollectionClasses::Queue;

public class Deque

specializes CollectionImpl, CollectionClasses::Deque;

public class Map specializes CollectionClasses::Map;

}

      1. CollectionClasses::Impl::CollectionImpl


The abstract CollectionImpl class provides implementations for the operations (other than toSequence) common to all collection classes other than Map. The actual collection content is expected to be supplied by the concrete subclasses of CollectionImpl. The CollectionImpl operation method use abstract methods setContent and toSequence, to be defined in the subclasses, to set and get this content.

Note that, since the Impl package is stereotyped as a model library, the usual importation of standard library packages is suppressed. Therefore, the CollectionFunctions package must be explicitly imported in order to make the collection functions usable without qualification. The collection functions are imported privately, as required by the rules of Subclause 11.6 for the implementation of collection classes.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**


The base concrete implementation for all the standard library collection classes.

*/

abstract class CollectionImpl {



@Create protected CollectionImpl(in seq: T[0..*] sequence) {

this.setContent(seq);

}
protected abstract setContent (in seq: T[0..*] sequence);
public abstract toSequence (): T[0..*] sequence;
public add (in element: T): Boolean {

result = this.excludes(element);

this.setContent(this.toSequence()->including(element));

return result;

}
public addAll (in seq: T[0..*] sequence): Boolean {

preSize = this.size();

this.setContent(this.toSequence()->union(seq));

return this.size() > preSize;

}
public clear () {

this.setContent(null);

}
public count (in element: T): Integer {

return this.toSequence()->count(element);

}
public equals (in seq: T[0..*] sequence): Boolean {

return this.toSequence()->equals(seq);

}
public excludes (in element: T): Boolean {

return this.toSequence()->excludes(element);

}
public excludesAll (in seq: T[0..*] sequence): Boolean {

return this.toSequence()->excludesAll(element);

}
public includes (in element: T): Boolean {

return this.toSequence()->includes(element);

}
public includesAll (in seq: T[0..*] sequence): Boolean {

return this.toSequence()->includesAll(element);

}
public isEmpty (): Boolean {

return this.toSequence()->isEmpty();

}
public notEmpty (): Boolean {

return this.toSequence()->notEmpty();

}
public remove (in element: T): Integer {

result = this.count(element);

this.setContent(this.toSequence()->excluding(element));

return result;

}
public removeAll (in seq: T[0..*] sequence): Boolean {

preSize = this.size();

this.setContent(this.toSequence()->difference(seq));

return this.size() < preSize;

}
public removeOne (in element: T): Boolean {

result = self.includes(element);

this.setContent(this.toSequence()->excludingOne(element));

return result;

}
public replace (in element: T, in newElement: T): Integer {

result = this.count(element);

this.setContent(this.toSequence()->replacing(element, newElement));

return result;

}
public replaceOne (in element: T, in newElement: T): Boolean {

result = this.includes(element);

this.setContent(this.toSequence()->replacingOne(element, newElement));

return result;

}
public retainAll (in seq: T[0..*] sequence): Boolean {

preSize = this.size();

this.setContent(this.toSequence()->intersection(seq));

return this.size() < preSize();

}
public size (): Integer {

return this.toSequence()->size();

}

}

      1. CollectionClasses::Impl::OrderedCollectionImpl


The class OrderedCollectionImpl extends CollectionImpl with implementation of additional operations found in the ordered collection classes (i.e., OrderedSet and List).

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The base concrete implementation for the standard library ordered collection



classes

*/

abstract class OrderedCollectionImpl specializes CollectionImpl {


@Create protected OrderedCollectionImpl(in seq: T[0..*] sequence) {

super(seq);

}
public addAllAt (in index: Integer, in seq: T[0..*] sequence): Boolean {

preSize = this.size();

this.setContent(this.toSequence()->includeAllAt(index, seq));

return this.size() > preSize;

}

public addAt (in index: Integer, in element: T): Boolean {



return addAllAt(index, element);

}

public at (in index: Integer): T[0..1] {



return this.toSequence()->at(index);

}

public first (): T[0..1] {



return this.at(1);

}

public indexOf (in element: T) : Integer[0..1] {



return this.toSequence()->indexof(element);

}

public last (): T[0..1] {



return this.at(this.size());

}

public removeAt (in index: Integer): T[0..1] {



result = this.at(index);

this.setContent(this.toSequence()->excludeAt(index));

return result;

}

public replaceAt (in index: Integer, in element: T): T[0..1] {



result = this.at(index);

this.setContent(this.toSequence()->replacingAt(index, element));

return result;

}

}


      1. CollectionClasses::Impl::Set


The Set class given below implements the abstract CollectionClasses::Set class. It does this by defining a content attribute of the appropriate type with unbounded multiplicity. By default, the attribute is unique and unordered—that is, a set. Operation methods are provided by specializing the CollectionImpl class.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The concrete implementation of the standard library template Set class.



*/

class Set specializes CollectionImpl, CollectionClasses::Set {


private content: T[0..*];
@Create public Set (in seq: T[0..*] sequence): Set {

super.CollectionImpl(seq);

}
@Destroy public destroy () {

super.CollectionImpl::destroy();

}
private setContent (in seq: T[0..*] sequence) {

this.content = seq;

}
public addAll (in seq: T[0..*] sequence): Boolean {

return super.CollectionImpl::addAll(seq->toOrderedSet());

}

public equals (in seq: T[0..*] sequence): Boolean {



set = seq->toOrderedSet();

return this.size() == set->size() && this.includesAll(set);

}

public toSequence (): T[0..*] sequence {



return this.content;

}

}


      1. CollectionClasses::Impl::OrderedSet


The OrderedSet class given below implements the abstract CollectionClasses::OrderedSet class. It does this by defining a content attribute of the appropriate type with unbounded multiplicity. The attribute is specifically defined to be ordered and is unqiue by default—that is, it is an ordered set. Operation methods are provided by specializing the OrderedCollectionImpl class.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The concrete implementation of the standard library template OrderedSet class.



*/

class OrderedSet

specializes OrderedCollectionImpl, CollectionClasses::OrderedSet {

private content: T[0..*] ordered;

@Create public OrderedSet (in seq: T[0..*] sequence): OrderedSet {

super.OrderedCollectionImpl(seq->toOrderedSet());

}
@Destroy public destroy () {

super.OrderedCollectionImpl::destroy();

}
private setContent(in seq: T[0..*] sequence) {

this.content = seq;

}
public equals (in seq: T[0..*] sequence): Boolean {

return super.OrdredCollcetionImpl::equals(seq->toOrderedSet());

}
public addAll (in seq: T[0..*] sequence): Boolean {

return super.OrderedCollectionImpl::addAll(seq->toOrderedSet());

}

public addAllAt (in index: Integer, in seq: T[0..*] sequence): Boolean {



return

super.OrderedCollectionImpl::addAllAt(index, seq->toOrderedSet());

}

public replaceAt (in index: Integer, in element: T): T[0..1] {



result = this.at(index);

if (result->notEmpty()) {

this.remove(result);

this.addAt(index, element);

}

return result;



}

public subOrderedSet (in lower: Integer, in upper: Integer): OrderedSet {

return new OrderedSet(this.toSequence()->subsequence(lower, upper));

}

public toSequence(): T[0..*] sequence {



return this.content;

}

}


      1. CollectionClasses::Impl::Bag


The Bag class given below implements the abstract CollectionClasses::Set class. It does this by defining a content attribute of the appropriate type with unbounded multiplicity. The attribute is specifically defined to be nonunique and is ordered by default—that is, it is a bag. Operation methods are provided by specializing the CollectionImpl class.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The concrete implementation of the standard library template Bag class.



*/

class Bag specializes CollectionImpl, CollectionClasses::Bag {


private content: T[0..*] nonunique;
@Create public Bag (in seq: T[0..*] sequence): Bag {

super.OrderedCollectionImpl(seq);

}
@Destroy public destroy () {

super.CollectionImpl::destroy();

}

private setContent(in seq: T[0..*] sequence) {



this.content = seq;

}
public equals (in seq: T[0..*] sequence): Boolean {

return this.size() == seq->size() && this.includesAll(seq);

}

public toSequence(): T[0..*] sequence {



return this.content;

}

}


      1. CollectionClasses::Impl::List


The List class given below implements the abstract CollectionClasses::List class. It does this by defining a content attribute of the appropriate type with unbounded multiplicity defined as a sequence—that is, a nonunique, ordered list. Operation methods are provided by specializing the OrderedCollectionImpl class.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The concrete implementation of the standard library template List class.



*/

class List

specializes OrderedCollectionImpl, CollectionClasses::OrderedSet {

private content: T[0..*] sequence;


@Create public List (in seq: T[0..*] sequence): List {

super.OrderedCollectionImpl(seq);

}
@Destroy public destroy () {

super.OrderedCollectionImpl::destroy();

}
private setContent(in seq: T[0..*]) {

this.content = seq;

}

public subList (in lower: Integer, in upper: Integer): OrderedSet {



return new List(this.toSequence()->subsequence(lower, upper));

}

public toSequence(): T[0..*] sequence {



return this.content;

}

}


      1. CollectionClasses::Impl::Queue


The List class given below implements the abstract CollectionClasses::List class. It does this by defining a content attribute of the appropriate type with unbounded multiplicity defined as a sequence—that is, a nonunique, ordered list. Operation methods are provided by specializing the CollectionImpl class. Even though a queue is effectively ordered, it does not provide all the operations defined for other ordered classes and, therefore, does not specialize the OrderedCollectionImpl class.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The concrete implementation of the standard library template Queue class.



*/

class Queue specializes CollectionImpl, CollectionClasses::Set {


private content: T[0..*] sequence;
@Create public Queue (in seq: T[0..*] sequence): Queue {

super.CollectionImpl(seq);

}
@Destroy public destroy () {

super.CollectionImpl::destroy();

}
protected setContent (in seq: T[0..*] sequence) {

this.content = seq;

}

public addLast (in element : T): Boolean {



return this.add(element);

}

public first(): T[0..1] {



return this.toSequence()->first();

}

public removeFirst (): T[0..1] {



result = this.toSequence()->first();

this.setContent(this.toSequence->subsequence(2,this.size()));

return result;

}

public removeFirstOne (in element: T): T[0..*] {



return this.removeOne(element);

}
public toSequence (): T[0..*] sequence {

return this.content;

}

}


      1. CollectionClasses::Impl::Deque


The Deque class given below implements the abstract CollectionClasses::Deque class. It does this by extending the Queue implementation class with the additional operations defined for a Deque.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The concrete implementation of the standard library template Deque class.



*/

class Deque specializes Queue {


@Create public Deque (in seq: T[0..*] sequence): Deque {

super(seq);

}
@Destroy public destroy () {

super.destroy();

}
public addFirst (in element: T): Boolean {

this.setContent(this.toSequence()->includeAt(1, element));

return true;

}

public last (): T[0..1] {



return this.toSequence()->last();

}

public removeLast (): T[0..1] {



result = this.last();

this.setContent(this.toSequence()->subsequence(1,this.size()-1));

return result;

}

public removeLastOne (in element: T): T[0..1] {



result = null;

i = this.size();

while (i > 0 && result == null) {

e = this.toSequence()->at(i);

if (e == element) {

result = e;

this.setContent(this.toSequence()->excludeAt(i));

}

i = i – 1;



}

return result;

}

public toSequence (): T[0..*] sequence {



return this.content;

}

}


      1. CollectionClasses::Impl::Map


The Map class given below implements the abstract CollectionClasses::List Map. It does this by storing a set of entries in an attribute. Lookup of entries is implemented in the private indexOf operation.

namespace Alf::Library::Collections::CollectionClasses::Impl;

private import

Alf::Library::FunctionBehaviors::Collections::CollectionFunctions::*;

/**

The concrete implementation of the standard library template Map class.



*/

class Map specializes CollectionClasses::Map {


private entries: Entry[0..*];
@Create public Map (in entries: Entry[0..*]): Map {

this.putAll(entries);

}
@Destroy public destroy () {

}
public entries (): Set {

return new Set(this.entries);

}
public clear () {

this.entries = null;

}
public excludesAll (in entries: Entry[0..*]): Boolean {

return this.entries->excludesAll(entries);

}
public get (in key: Key): Value[0..1] {

return this.entries->select e (e.key == key)[1].value;

}
public includesAll (in entries: Entry[0..*]): Boolean {

return this.entries->includesAll(entries);

}
public includesKey (in key: Key): Boolean {

return this.entries.key->includes(key);

}
public includesValue (in value: Value[0..1]): Boolean {

return this.entries.value->includes(value);

}
public isEmpty (): Boolean {

return this.entries->isEmpty();

}
public keys (): Set {

return new Set(this.entries.key);

}
public notEmpty (): Boolean {

return this.entries->notEmpty();

}
public put (in key: Key, in value: Value[0..1]): Value[0..1] {

result = this.remove(key);

this.entries->add(new Entry(key,value));

return result;

}
public putAll (in entries: Entry[0..*]) {

entries->iterate e (this.put(e.key, e.value));

}
public remove (in key: Key): Value[0..1] {

result = this.get(key);

this.entries = this.entries->reject e (e.key == key);

return result;

}
public removeAll (in keys: Key[0..*]) {

this.entries = this.entries->reject e (keys->includes(e.key));

}
public size (): Integer {

this.entries->size();

}
public toSequence (): Entry[0..*] sequence {

return this.entries;

}
public values (): Bag {

return new Bag(this.entries.value);

}

}




  1. Download 1.74 Mb.

    Share with your friends:
1   ...   54   55   56   57   58   59   60   61   62




The database is protected by copyright ©ininet.org 2024
send message

    Main page