Add named parsers
This commit is contained in:
parent
96fcd03a75
commit
c336a2d7e8
4 changed files with 71 additions and 9 deletions
|
|
@ -52,6 +52,16 @@ public interface ParserRegistry<C> {
|
|||
<T> void registerParserSupplier(@Nonnull TypeToken<T> type,
|
||||
@Nonnull Function<ParserParameters, ArgumentParser<C, ?>> supplier);
|
||||
|
||||
/**
|
||||
* Register a named parser supplier
|
||||
*
|
||||
* @param name Parser name
|
||||
* @param supplier The function that generates the parser. The map supplied my contain parameters used
|
||||
* to configure the parser, many of which are documented in {@link StandardParameters}
|
||||
*/
|
||||
void registerNamedParserSupplier(@Nonnull String name,
|
||||
@Nonnull Function<ParserParameters, ArgumentParser<C, ?>> supplier);
|
||||
|
||||
/**
|
||||
* Register a mapper that maps annotation instances to a map of parameter-object pairs
|
||||
*
|
||||
|
|
@ -88,4 +98,17 @@ public interface ParserRegistry<C> {
|
|||
<T> Optional<ArgumentParser<C, T>> createParser(@Nonnull TypeToken<T> type,
|
||||
@Nonnull ParserParameters parserParameters);
|
||||
|
||||
/**
|
||||
* Attempt to create a {@link ArgumentParser} for a specified type, using
|
||||
* an instance of {@link ParserParameter} to configure the parser settings
|
||||
*
|
||||
* @param name Parser
|
||||
* @param parserParameters Parser parameters
|
||||
* @param <T> Generic type
|
||||
* @return Parser, if one can be created
|
||||
*/
|
||||
@Nonnull
|
||||
<T> Optional<ArgumentParser<C, T>> createParser(@Nonnull String name,
|
||||
@Nonnull ParserParameters parserParameters);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
.put(boolean.class, Boolean.class)
|
||||
.build();
|
||||
|
||||
private final Map<String, Function<ParserParameters, ArgumentParser<C, ?>>> namedParsers = new HashMap<>();
|
||||
private final Map<TypeToken<?>, Function<ParserParameters, ArgumentParser<C, ?>>> parserSuppliers = new HashMap<>();
|
||||
private final Map<Class<? extends Annotation>, BiFunction<? extends Annotation, TypeToken<?>, ParserParameters>>
|
||||
annotationMappers = new HashMap<>();
|
||||
|
|
@ -109,6 +110,12 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
this.parserSuppliers.put(type, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerNamedParserSupplier(@Nonnull final String name,
|
||||
@Nonnull final Function<ParserParameters, ArgumentParser<C, ?>> supplier) {
|
||||
this.namedParsers.put(name, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A extends Annotation, T> void registerAnnotationMapper(@Nonnull final Class<A> annotation,
|
||||
@Nonnull final BiFunction<A, TypeToken<?>,
|
||||
|
|
@ -160,6 +167,19 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
return Optional.of(parser);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public <T> Optional<ArgumentParser<C, T>> createParser(@Nonnull final String name,
|
||||
@Nonnull final ParserParameters parserParameters) {
|
||||
final Function<ParserParameters, ArgumentParser<C, ?>> producer = this.namedParsers.get(name);
|
||||
if (producer == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@SuppressWarnings("unchecked") final ArgumentParser<C, T> parser = (ArgumentParser<C, T>) producer.apply(
|
||||
parserParameters);
|
||||
return Optional.of(parser);
|
||||
}
|
||||
|
||||
|
||||
private static final class RangeMapper<T> implements BiFunction<Range, TypeToken<?>, ParserParameters> {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue