/*
* Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
*
* This file is part of the Lock Contention Tracing Subsystem for the HotSpot
* Virtual Machine, which is developed at Christian Doppler Laboratory on
* Monitoring and Evolution of Very-Large-Scale Software Systems. Please
* contact us at if you need additional information
* or have any questions.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work. If not, see .
*
*/
package sun.evtracing.parser.metadata;
import java.util.Arrays;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import sun.evtracing.processing.MetadataStore;
class JavaStackData implements Iterable {
public static final JavaStackData UNKNOWN = new JavaStackData(new long[0], new int[0], null);
private final JavaMethod[] methods;
private final int[] bcis;
public JavaStackData(long[] methods, int[] bcis, MetadataStore metadataStore) {
assert (methods.length == bcis.length);
this.methods = new JavaMethod[methods.length];
for (int i = 0; i < methods.length; i++) {
this.methods[i] = metadataStore.getOrAddMethod(methods[i]);
}
this.bcis = Arrays.copyOf(bcis, bcis.length);
}
public int frameCount() {
return methods.length;
}
public boolean hasMethodFromClassLoader(long classLoader) {
return Arrays.stream(methods).anyMatch(m -> m.clazz().classLoader() == classLoader);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (JavaStackFrame frame : this) {
sb.append(frame.toString());
sb.append(System.lineSeparator());
}
return sb.toString();
}
@Override
public ListIterator iterator() {
return new ListItr(0);
}
public ListIterator lastIterator() {
return new ListItr(methods.length);
}
private class ListItr implements ListIterator {
private int index;
public ListItr(int index) {
this.index = index;
}
@Override
public boolean hasNext() {
return (index < methods.length);
}
@Override
public JavaStackFrame next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
JavaStackFrame jsf = new JavaStackFrame(methods[index], bcis[index]);
index++;
return jsf;
}
@Override
public boolean hasPrevious() {
return (index > 0);
}
@Override
public JavaStackFrame previous() {
if (!hasPrevious()) {
throw new NoSuchElementException();
}
index--;
return new JavaStackFrame(methods[index], bcis[index]);
}
@Override
public int nextIndex() {
return index + 1;
}
@Override
public int previousIndex() {
return index - 1;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(JavaStackFrame e) {
throw new UnsupportedOperationException();
}
@Override
public void add(JavaStackFrame e) {
throw new UnsupportedOperationException();
}
}
}