com.facebook.presto.benchmark.driver.RegexTemplate Maven / Gradle / Ivy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.benchmark.driver;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.Objects.requireNonNull;
public class RegexTemplate
{
private static final Method NAMED_GROUPS_METHOD;
static {
try {
NAMED_GROUPS_METHOD = Pattern.class.getDeclaredMethod("namedGroups");
NAMED_GROUPS_METHOD.setAccessible(true);
}
catch (NoSuchMethodException e) {
throw Throwables.propagate(e);
}
}
private final String template;
private final Pattern pattern;
private final List fieldNames;
public RegexTemplate(String template)
{
this.template = requireNonNull(template, "template is null");
try {
this.pattern = Pattern.compile(template);
}
catch (Exception e) {
throw new IllegalArgumentException("Invalid template: " + template, e);
}
Map namedGroups;
try {
namedGroups = (Map) NAMED_GROUPS_METHOD.invoke(pattern);
}
catch (Exception e) {
throw Throwables.propagate(e);
}
ImmutableSortedMap sortedGroups = ImmutableSortedMap.copyOf(ImmutableBiMap.copyOf(namedGroups).inverse());
this.fieldNames = ImmutableList.copyOf(sortedGroups.values());
}
public List getFieldNames()
{
return fieldNames;
}
public Optional