Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a426ec7333d48206894a0f0e300de592772d9c4 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**********************************************************************
 * Copyright (c) 2005, 2014 IBM Corporation, Ericsson
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM - Initial API and implementation
 *     Bernd Hufmann - Updated for TMF
 **********************************************************************/

package org.eclipse.tracecompass.tmf.ui.views.uml2sd.util;

import java.io.Serializable;
import java.util.Comparator;

import org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.AsyncMessage;
import org.eclipse.tracecompass.tmf.ui.views.uml2sd.core.GraphNode;

/**
 * Asynchronous message comparator.
 *
 * Compares two asyncMessages only taking into account the event occurrence when their
 * appear.<br>
 *
 * Used to order the AsyncMessage list insuring that the previous node has both of his ends smaller than the current node
 *
 * @version 1.0
 * @author sveyrier
 *
 */
public class SortAsyncForBackward implements Comparator<GraphNode>, Serializable {

    // ------------------------------------------------------------------------
    // Constants
    // ------------------------------------------------------------------------
    /**
     * Serial version UID
     */
    private static final long serialVersionUID = 603959931263853359L;

    // ------------------------------------------------------------------------
    // Methods
    // ------------------------------------------------------------------------

    @Override
    public int compare(GraphNode arg0, GraphNode arg1) {
        if (arg0 instanceof AsyncMessage && arg1 instanceof AsyncMessage) {
            AsyncMessage m1 = (AsyncMessage) arg0;
            AsyncMessage m2 = (AsyncMessage) arg1;
            int m1Max, m2Max;
            // AsyncMessage has two ends which may have different event occurrences
            // Search for the greater event occurrence for each messages
            if (m1.getStartOccurrence() > m1.getEndOccurrence()) {
                m1Max = m1.getStartOccurrence();
            } else {
                m1Max = m1.getEndOccurrence();
            }
            if (m2.getStartOccurrence() > m2.getEndOccurrence()) {
                m2Max = m2.getStartOccurrence();
            } else {
                m2Max = m2.getEndOccurrence();
            }

            int m1Min, m2Min;
            // Search for the smaller event occurrence for each messages
            if (m1.getStartOccurrence() > m1.getEndOccurrence()) {
                m1Min = m1.getEndOccurrence();
            } else {
                m1Min = m1.getStartOccurrence();
            }
            if (m2.getStartOccurrence() > m2.getEndOccurrence()) {
                m2Min = m2.getEndOccurrence();
            } else {
                m2Min = m2.getStartOccurrence();
            }

            if (m1Max > m2Max) {
                return 1;
            } else if (m1Max == m2Max) {
                if (m1Min == m2Min) {
                    return 0;
                } else if (m1Min > m2Min) {
                    return -1;
                } else {
                    return 1;
                }
            } else {
                return -1;
            }
        }
        return 0;
    }

}

Back to the top