Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15a6a04db2fc6fb3397f310deac25ff306054f8d (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 * Copyright (c) 2008, 2018 Red Hat, Inc.
 * 
 * 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:
 *    Elliott Baron <ebaron@redhat.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.valgrind.massif;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class MassifHeapTreeNode {
    protected MassifHeapTreeNode parent;
    protected String text;
    protected double percent;
    protected long bytes;
    protected String address;
    protected String function;
    protected String filename;
    protected int line;
    protected List<MassifHeapTreeNode> children;

    public MassifHeapTreeNode(MassifHeapTreeNode parent, double percent, long bytes, String address, String function, String filename, int line) {
        this.parent = parent;

        StringBuilder nodeText = new StringBuilder();
        formatBytes(percent, bytes, nodeText);
        nodeText.append(address).append(":"); //$NON-NLS-1$
        if (function.length() > 0) {
            nodeText.append(" "); //$NON-NLS-1$
            nodeText.append(function);
        }
        if (filename != null) {
            nodeText.append(" (").append(filename); //$NON-NLS-1$
            if (line > 0) {
                nodeText.append(":").append(line);//$NON-NLS-1$
            }
            nodeText.append(")"); //$NON-NLS-1$
        }
        this.percent = percent;
        this.bytes = bytes;
        this.address = address;
        this.function = function;
        this.filename = filename;
        this.line = line;
        this.text = nodeText.toString();
        children = new ArrayList<>();
    }

    public MassifHeapTreeNode(MassifHeapTreeNode parent, double percent, long bytes, String text) {
        this.parent = parent;

        StringBuilder nodeText = new StringBuilder();
        formatBytes(percent, bytes, nodeText);
        nodeText.append(text);
        this.percent = percent;
        this.bytes = bytes;
        this.address = null;
        this.function = null;
        this.filename = null;
        this.line = 0;
        this.text = nodeText.toString();
        children = new ArrayList<>();
    }

    private void formatBytes(double percent, long bytes, StringBuilder buffer) {
        buffer.append(new DecimalFormat("0.##").format(percent) + "%"); //$NON-NLS-1$ //$NON-NLS-2$
        buffer.append(" ("); //$NON-NLS-1$
        buffer.append(new DecimalFormat("#,##0").format(bytes) + "B"); //$NON-NLS-1$ //$NON-NLS-2$
        buffer.append(") "); //$NON-NLS-1$
    }

    public void addChild(MassifHeapTreeNode child) {
        children.add(child);
    }

    public MassifHeapTreeNode getParent() {
        return parent;
    }

    public MassifHeapTreeNode[] getChildren() {
        return children.toArray(new MassifHeapTreeNode[children.size()]);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public double getPercent() {
        return percent;
    }

    public long getBytes() {
        return bytes;
    }

    public String getAddress() {
        return address;
    }

    public String getFunction() {
        return function;
    }

    public String getFilename() {
        return filename;
    }

    public int getLine() {
        return line;
    }

    @Override
    public String toString() {
        return text;
    }

    public boolean hasSourceFile() {
        return filename != null && line > 0;
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof MassifHeapTreeNode
        && text.equals(((MassifHeapTreeNode) obj).getText());
    }

    @Override
    public int hashCode() {
        return text.hashCode();
    }
}

Back to the top