aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/util/timeline/Diff.java
blob: cec6049e62673b205c5984196805b93081c5a0e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*******************************************************************************
 * Copyright (c) 2010-2019, Tamas Szabo, itemis AG, Gabor Bergmann, IncQuery Labs Ltd.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.viatra.runtime.matchers.util.timeline;

import java.util.ArrayList;

import tools.refinery.viatra.runtime.matchers.util.Signed;

/**
 * The description of a delta that specifies how a {@link Timeline} changes. It consists of {@link Signed} timestamps that
 * depict the moments of insertions and deletions on the timeline.
 * 
 * @author Tamas Szabo
 * @since 2.4
 * @param <Timestamp>
 *            the type representing the timestamps
 */
public class Diff<Timestamp extends Comparable<Timestamp>> extends ArrayList<Signed<Timestamp>> {

    private static final long serialVersionUID = 3853460426655994160L;

    public Diff() {

    }

    public void appendWithCancellation(Signed<Timestamp> item) {
        if (this.isEmpty()) {
            this.add(item);
        } else {
            final Signed<Timestamp> last = this.get(this.size() - 1);
            final int lastMinusItem = last.getPayload().compareTo(item.getPayload());
            if (lastMinusItem == 0) {
                if (last.getDirection() != item.getDirection()) {
                    // cancellation
                    this.remove(this.size() - 1);
                } else {
                    throw new IllegalStateException(
                            "Trying to insert or delete for the second time at the same timestamp! " + item);
                }
            } else if (lastMinusItem > 0) {
                throw new IllegalStateException(
                        "Trying to append a timestamp that is smaller than the last one! " + last + " " + item);
            } else {
                this.add(item);
            }
        }
    }

}