
org.jsimpledb.parse.EnumNameParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsimpledb-parse Show documentation
Show all versions of jsimpledb-parse Show documentation
JSimpleDB classes for parsing Java expressions.
The newest version!
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package org.jsimpledb.parse;
import com.google.common.base.Preconditions;
import java.util.function.Function;
import java.util.regex.Matcher;
import org.dellroad.stuff.java.EnumUtil;
import org.jsimpledb.util.ParseContext;
public class EnumNameParser> implements Parser {
private final Class type;
private final Function nameFunction;
public EnumNameParser(Class type) {
this(type, true);
}
public EnumNameParser(Class type, boolean lowerCase) {
Preconditions.checkArgument(type != null, "null type");
this.type = type;
this.nameFunction = value -> {
String name = value.name();
if (lowerCase)
name = name.toLowerCase();
return name;
};
}
@Override
public T parse(ParseSession session, ParseContext ctx, boolean complete) {
// Get enum value name
final Matcher matcher = ctx.tryPattern("[^\\s;]*");
if (matcher == null) {
final ParseException e = new ParseException(ctx);
if (ctx.isEOF() && complete)
e.addCompletions(EnumUtil.getValues(this.type).stream().map(this.nameFunction));
throw e;
}
final String valueName = matcher.group();
// Find corresponding enum value
return EnumUtil.getValues(this.type).stream()
.filter(value -> valueName.equals(this.nameFunction.apply(value)))
.findAny().orElseThrow(() -> new ParseException(ctx, "unknown value `" + valueName + "'")
.addCompletions(ParseUtil.complete(EnumUtil.getValues(this.type).stream().map(this.nameFunction), valueName)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy