diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/timeline/Diff.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/timeline/Diff.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/timeline/Diff.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/timeline/Diff.java new file mode 100644 index 00000000..cec6049e --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/timeline/Diff.java | |||
@@ -0,0 +1,55 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2019, Tamas Szabo, itemis AG, Gabor Bergmann, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.util.timeline; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | |||
13 | import tools.refinery.viatra.runtime.matchers.util.Signed; | ||
14 | |||
15 | /** | ||
16 | * The description of a delta that specifies how a {@link Timeline} changes. It consists of {@link Signed} timestamps that | ||
17 | * depict the moments of insertions and deletions on the timeline. | ||
18 | * | ||
19 | * @author Tamas Szabo | ||
20 | * @since 2.4 | ||
21 | * @param <Timestamp> | ||
22 | * the type representing the timestamps | ||
23 | */ | ||
24 | public class Diff<Timestamp extends Comparable<Timestamp>> extends ArrayList<Signed<Timestamp>> { | ||
25 | |||
26 | private static final long serialVersionUID = 3853460426655994160L; | ||
27 | |||
28 | public Diff() { | ||
29 | |||
30 | } | ||
31 | |||
32 | public void appendWithCancellation(Signed<Timestamp> item) { | ||
33 | if (this.isEmpty()) { | ||
34 | this.add(item); | ||
35 | } else { | ||
36 | final Signed<Timestamp> last = this.get(this.size() - 1); | ||
37 | final int lastMinusItem = last.getPayload().compareTo(item.getPayload()); | ||
38 | if (lastMinusItem == 0) { | ||
39 | if (last.getDirection() != item.getDirection()) { | ||
40 | // cancellation | ||
41 | this.remove(this.size() - 1); | ||
42 | } else { | ||
43 | throw new IllegalStateException( | ||
44 | "Trying to insert or delete for the second time at the same timestamp! " + item); | ||
45 | } | ||
46 | } else if (lastMinusItem > 0) { | ||
47 | throw new IllegalStateException( | ||
48 | "Trying to append a timestamp that is smaller than the last one! " + last + " " + item); | ||
49 | } else { | ||
50 | this.add(item); | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | } \ No newline at end of file | ||