Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ddc9a4f238050ccfb55b2e307ef094425b7ac269 (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
/*******************************************************************************
 * Copyright (c) 2014, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Brajesh K Rathore <brrathor@linux.vnet.ibm.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.oprofile.ui.model;

import java.text.Collator;
import java.util.Comparator;

/**
 * Comparator for sorting tree elements.
 *
 * @since 3.0
 */
public class UiModelSorting implements Comparator<IUiModelElement> {
    private static UiModelSorting instance = new UiModelSorting();

    private Collator collator;

    private UiModelSorting() {
        collator = Collator.getInstance();
    }

    public static UiModelSorting getInstance() {
        return instance;
    }

    @Override
    public int compare(IUiModelElement o1, IUiModelElement o2) {

        // compare line no.
        if (o1 instanceof UiModelSample && o2 instanceof UiModelSample) {
            return ((UiModelSample) o1).getLine()
                    - ((UiModelSample) o2).getLine();
        } else if (o1 instanceof UiModelSymbol && o2 instanceof UiModelSymbol) {
            // compare function name
            return collator.compare(((UiModelSymbol) o1).getFunctionName(),
                    ((UiModelSymbol) o2).getFunctionName());
        } else if (o1 instanceof UiModelImage && o2 instanceof UiModelImage) {
            // comapre lib name
            return collator.compare(getLibraryName(o1.getLabelText()),
                    getLibraryName(o2.getLabelText()));
        }
        // default comparison based on display label
        return collator.compare(o1.getLabelText(), o2.getLabelText());
    }

    private String getLibraryName(String lib) {
        // /lib64/libc-2.12.so - libc-2.12.s0
        String libName = ""; //$NON-NLS-1$
        int index = 0;
        if (null != lib && lib.trim().length() != 0) {
            index = lib.lastIndexOf('/');
            if (index != -1) {
                libName = lib.substring(index + 1, lib.length());
            }

        }
        return libName;
    }
}

Back to the top