Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #43: Fix Sequence()->insertAt() #44

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions manual/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -3438,14 +3438,20 @@ If `self` is a sequence of collections, then this operation
returns the sequence concatenation of all its elements.
**Notation:** `self->flatten()`

#### Append elements
#### Adding elements

`append(y:T):Sequence(T) :=
self->union(Sequence{y})` results
in the sequence which consists of all elements of `y` with `y`
in the sequence which consists of all elements of `self` with `y`
appended.
**Notation:** `self->append(y)`

`insertAt(index:Integer,y:T):Sequence(T)` results
in the sequence which consists of all elements of `self` with `y`
inserted at position `index`. All elements that were at a position `p` with `p >= index`
are afterwards at position `p + 1`, i.e., they moved right.
**Notation:** `self->insertAt(1, y)`

#### Prepend elements

`prepend(y:T):Sequence(T) :=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,12 @@

package org.tzi.use.uml.ocl.value;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.tzi.use.uml.ocl.type.CollectionType;
import org.tzi.use.uml.ocl.type.TupleType;
import org.tzi.use.uml.ocl.type.*;
import org.tzi.use.uml.ocl.type.TupleType.Part;
import org.tzi.use.uml.ocl.type.Type;
import org.tzi.use.uml.ocl.type.TypeFactory;
import org.tzi.use.uml.ocl.type.UniqueLeastCommonSupertypeDeterminator;
import org.tzi.use.util.collections.CollectionComparator;

import java.util.*;

/**
* Base class for collection values.
*
Expand Down Expand Up @@ -85,7 +74,7 @@ public Type getRuntimeType() {
protected abstract CollectionType getRuntimeType(Type elementType);

/**
* Returns the element type of the <code>resultType</code> if
* Returns the type of the elements of <code>resultType</code>, if
* it is a collection type.
* Otherwise, a runtime exception is thrown.
* @param resultType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@

package org.tzi.use.uml.ocl.value;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.tzi.use.uml.ocl.type.CollectionType;
import org.tzi.use.uml.ocl.type.Type;
import org.tzi.use.uml.ocl.type.Type.VoidHandling;
import org.tzi.use.uml.ocl.type.TypeFactory;
import org.tzi.use.util.StringUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
* Sequence values.
*
Expand Down Expand Up @@ -208,13 +208,22 @@ public SequenceValue prepend(Type resultType, Value v) {
return res;
}

/**
* Constructs a new {@link SequenceValue} with {@link Value} <code>v</code> inserted
* at the position <code>index</code>.
* @param resultType The {link Type} of the newly created {@link SequenceValue}
* @param index The position to insert the element at
* @param v The value that is inserted at position <code>index</code>
* @return A new {@link SequenceValue} with <code>v</code> inserted at position <code>index</code> or
* <code>null</code> if <code>index</code> is not a valid index.
*/
public SequenceValue insertAt(Type resultType, IntegerValue index, Value v) {
if (index.value() < 1 || index.value() >= fElements.size())
if (index.value() < 1 || index.value() > fElements.size() + 1)
return null;

SequenceValue res = new SequenceValue(getResultElementType(resultType));
res.addAll(fElements);
res.fElements.add(index.value(), v);
res.fElements.add(index.value() - 1, v);

return res;
}
Expand Down
18 changes: 18 additions & 0 deletions use-gui/src/it/resources/testfiles/shell/t001.in
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,24 @@
? Sequence{1,2,2,3}->asSet
*-> Set{1,2,3} : Set(Integer)

? Sequence{}->insertAt(1, 'Z')
*-> Sequence{'Z'} : Sequence(String)

? Sequence{'A','B'}->insertAt(1, 'Z')
*-> Sequence{'Z','A','B'} : Sequence(String)

? Sequence{'A','B'}->insertAt(2, 'Z')
*-> Sequence{'A','Z','B'} : Sequence(String)

? Sequence{'A','B'}->insertAt(3, 'Z')
*-> Sequence{'A','B','Z'} : Sequence(String)

? Sequence{'A','B'}->insertAt(4, 'Z')
*-> null : OclVoid

? Sequence{'A','B'}->insertAt(0, 'Z')
*-> null : OclVoid

#
## Flattening
#
Expand Down