/*
* 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.Iterator;
import java.util.regex.Pattern;
public class MethodFilter {
public static final MethodFilter NO_FILTER = new MethodFilter(new Pattern[0]);
private final Pattern[] patterns;
public MethodFilter(Pattern[] patterns) {
this.patterns = patterns;
}
public boolean matches(JavaMethod method) {
String name = method.prettyName(false);
for (Pattern p : patterns) {
if (p.matcher(name).matches()) {
return true;
}
}
return false;
}
public JavaMethod getFirstUnmatched(JavaStack stack) {
if (!stack.isSet() || stack.frameCount() == 0) {
return null;
}
Iterator it = stack.iterator();
JavaMethod method = it.next().method();
int skipped = 1;
while (it.hasNext() && matches(method)) {
method = it.next().method();
skipped++;
}
// if everything was skipped, return the first one
return skipped == stack.frameCount() ? stack.iterator().next().method() : method;
}
}