-
Notifications
You must be signed in to change notification settings - Fork 3
/
TreeMapCollectionExtractor.java
168 lines (147 loc) · 5.02 KB
/
TreeMapCollectionExtractor.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG, IBM Corporation and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
* IBM Corporation - enhancements and fixes
* James Livingston - expose collection utils as API
*******************************************************************************/
package org.eclipse.mat.internal.collectionextract;
import java.util.Stack;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.ArrayInt;
import org.eclipse.mat.collect.SetInt;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.IObjectArray;
public class TreeMapCollectionExtractor extends MapCollectionExtractorBase
{
protected final String sizeField;
public TreeMapCollectionExtractor(String sizeField, String keyField, String valueField)
{
super(keyField, valueField);
if (sizeField == null)
throw new IllegalArgumentException();
this.sizeField = sizeField;
}
public boolean hasExtractableArray()
{
return false;
}
public IObjectArray extractEntries(IObject coll) throws SnapshotException
{
throw new IllegalArgumentException();
}
public boolean hasExtractableContents()
{
return true;
}
public int[] extractEntryIds(IObject treeMap) throws SnapshotException
{
ArrayInt result = new ArrayInt();
String rootf = "root"; //$NON-NLS-1$
// For TreeSet
int dot = sizeField.lastIndexOf("."); //$NON-NLS-1$
if (dot > 0)
{
rootf = sizeField.substring(0, dot + 1) + rootf;
}
IObject root = (IObject) treeMap.resolveValue(rootf);
boolean noFieldNames = root == null;
if (root == null)
{
if (dot > 0)
{
root = ExtractionUtils.followOnlyOutgoingReferencesExceptLast(rootf, treeMap);
if (root != null)
root = ExtractionUtils.followOnlyNonArrayOutgoingReference(root);
}
else
{
root = ExtractionUtils.followOnlyNonArrayOutgoingReference(treeMap);
}
if (root == null || !root.getClazz().getName().endsWith("$Entry")) //$NON-NLS-1$
return new int[0];
}
Stack<IObject> stack = new Stack<IObject>();
stack.push(root);
IObject current = root;
SetInt visited = new SetInt();
/* traverse the TreeMap entries in-order */
while (stack.size() > 0)
{
current = stack.peek();
/* go left */
IObject left = (IObject) current.resolveValue("left"); //$NON-NLS-1$
if (left != null && !visited.contains(left.getObjectId()))
{
stack.push(left);
}
else
{
/* process node */
result.add(current.getObjectId());
visited.add(current.getObjectId());
stack.pop();
/* go right */
IObject right = (IObject) current.resolveValue("right"); //$NON-NLS-1$
if (right != null && !visited.contains(right.getObjectId()))
{
stack.push(right);
}
else if (noFieldNames)
{
// All nodes of same type as root
for (int o : current.getSnapshot().getOutboundReferentIds(current.getObjectId()))
{
if (current.getSnapshot().getClassOf(o).equals(root.getClazz()))
{
if (!visited.contains(o))
stack.push(current.getSnapshot().getObject(o));
}
}
}
}
}
return result.toArray();
}
public boolean hasSize()
{
return true;
}
public Integer getSize(IObject coll) throws SnapshotException
{
Integer ret = ExtractionUtils.toInteger(coll.resolveValue(sizeField));
if (ret != null)
return ret;
int e[] = this.extractEntryIds(coll);
if (e != null)
return e.length;
return null;
}
public Integer getNumberOfNotNullElements(IObject coll) throws SnapshotException
{
return getSize(coll);
}
public boolean hasCollisionRatio()
{
return false;
}
public Double getCollisionRatio(IObject collection)
{
return null;
}
public boolean hasFillRatio()
{
return false;
}
public Double getFillRatio(IObject coll) throws SnapshotException
{
return null;
}
}