Make it possible to extent the argument product types

This commit is contained in:
Alexander Söderberg 2020-09-27 23:45:56 +02:00 committed by Alexander Söderberg
parent 94710c5174
commit cd90665559
2 changed files with 49 additions and 27 deletions

View file

@ -43,13 +43,24 @@ import java.util.function.Function;
*/ */
public final class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O> { public final class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O> {
private ArgumentPair(final boolean required, /**
@Nonnull final String name, * Create a new argument pair.
@Nonnull final Pair<String, String> names, *
@Nonnull final Pair<Class<U>, Class<V>> types, * @param required Whether or not the argument is required
@Nonnull final Pair<ArgumentParser<C, U>, ArgumentParser<C, V>> parserPair, * @param name The argument name
@Nonnull final Function<Pair<U, V>, O> mapper, * @param names Names of the sub-arguments (in order)
@Nonnull final TypeToken<O> valueType) { * @param types Types of the sub-arguments (in order)
* @param parserPair The sub arguments
* @param mapper Mapper that maps the sub-arguments to the output type
* @param valueType The output type
*/
protected ArgumentPair(final boolean required,
@Nonnull final String name,
@Nonnull final Pair<String, String> names,
@Nonnull final Pair<Class<U>, Class<V>> types,
@Nonnull final Pair<ArgumentParser<C, U>, ArgumentParser<C, V>> parserPair,
@Nonnull final Function<Pair<U, V>, O> mapper,
@Nonnull final TypeToken<O> valueType) {
super(required, name, names, parserPair, types, mapper, o -> Pair.of((U) o[0], (V) o[1]), valueType); super(required, name, names, parserPair, types, mapper, o -> Pair.of((U) o[0], (V) o[1]), valueType);
} }
@ -75,12 +86,12 @@ public final class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>,
final ParserRegistry<C> parserRegistry = manager.getParserRegistry(); final ParserRegistry<C> parserRegistry = manager.getParserRegistry();
final ArgumentParser<C, U> firstParser = parserRegistry.createParser(TypeToken.of(types.getFirst()), final ArgumentParser<C, U> firstParser = parserRegistry.createParser(TypeToken.of(types.getFirst()),
ParserParameters.empty()).orElseThrow(() -> ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException( new IllegalArgumentException(
"Could not create parser for primary type")); "Could not create parser for primary type"));
final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(TypeToken.of(types.getSecond()), final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(TypeToken.of(types.getSecond()),
ParserParameters.empty()).orElseThrow(() -> ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException( new IllegalArgumentException(
"Could not create parser for secondary type")); "Could not create parser for secondary type"));
return new ArgumentPairIntermediaryBuilder<>(true, name, names, Pair.of(firstParser, secondaryParser), types); return new ArgumentPairIntermediaryBuilder<>(true, name, names, Pair.of(firstParser, secondaryParser), types);
} }

View file

@ -44,14 +44,25 @@ import java.util.function.Function;
*/ */
public final class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U, V, W>, C, O> { public final class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U, V, W>, C, O> {
private ArgumentTriplet(final boolean required, /**
@Nonnull final String name, * Create a new argument triplet.
@Nonnull final Triplet<String, String, String> names, *
@Nonnull final Triplet<Class<U>, Class<V>, Class<W>> types, * @param required Whether or not the argument is required
@Nonnull final Triplet<ArgumentParser<C, U>, ArgumentParser<C, V>, * @param name The argument name
ArgumentParser<C, W>> parserTriplet, * @param names Names of the sub-arguments (in order)
@Nonnull final Function<Triplet<U, V, W>, O> mapper, * @param types Types of the sub-arguments (in order)
@Nonnull final TypeToken<O> valueType) { * @param parserTriplet The sub arguments
* @param mapper Mapper that maps the sub-arguments to the output type
* @param valueType The output type
*/
protected ArgumentTriplet(final boolean required,
@Nonnull final String name,
@Nonnull final Triplet<String, String, String> names,
@Nonnull final Triplet<Class<U>, Class<V>, Class<W>> types,
@Nonnull final Triplet<ArgumentParser<C, U>, ArgumentParser<C, V>,
ArgumentParser<C, W>> parserTriplet,
@Nonnull final Function<Triplet<U, V, W>, O> mapper,
@Nonnull final TypeToken<O> valueType) {
super(required, name, names, parserTriplet, types, mapper, o -> Triplet.of((U) o[0], (V) o[1], (W) o[2]), valueType); super(required, name, names, parserTriplet, types, mapper, o -> Triplet.of((U) o[0], (V) o[1], (W) o[2]), valueType);
} }
@ -79,16 +90,16 @@ public final class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Tripl
final ParserRegistry<C> parserRegistry = manager.getParserRegistry(); final ParserRegistry<C> parserRegistry = manager.getParserRegistry();
final ArgumentParser<C, U> firstParser = parserRegistry.createParser(TypeToken.of(types.getFirst()), final ArgumentParser<C, U> firstParser = parserRegistry.createParser(TypeToken.of(types.getFirst()),
ParserParameters.empty()).orElseThrow(() -> ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException( new IllegalArgumentException(
"Could not create parser for primary type")); "Could not create parser for primary type"));
final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(TypeToken.of(types.getSecond()), final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(TypeToken.of(types.getSecond()),
ParserParameters.empty()).orElseThrow(() -> ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException( new IllegalArgumentException(
"Could not create parser for secondary type")); "Could not create parser for secondary type"));
final ArgumentParser<C, W> tertiaryParser = parserRegistry.createParser(TypeToken.of(types.getThird()), final ArgumentParser<C, W> tertiaryParser = parserRegistry.createParser(TypeToken.of(types.getThird()),
ParserParameters.empty()).orElseThrow(() -> ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException( new IllegalArgumentException(
"Could not create parser for tertiary type")); "Could not create parser for tertiary type"));
return new ArgumentTripletIntermediaryBuilder<>(true, name, names, return new ArgumentTripletIntermediaryBuilder<>(true, name, names,
Triplet.of(firstParser, secondaryParser, tertiaryParser), types); Triplet.of(firstParser, secondaryParser, tertiaryParser), types);
} }
@ -106,7 +117,7 @@ public final class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Tripl
@Nonnull final String name, @Nonnull final String name,
@Nonnull final Triplet<String, String, String> names, @Nonnull final Triplet<String, String, String> names,
@Nonnull final Triplet<ArgumentParser<C, U>, @Nonnull final Triplet<ArgumentParser<C, U>,
ArgumentParser<C, V>, ArgumentParser<C, W>> parserTriplet, ArgumentParser<C, V>, ArgumentParser<C, W>> parserTriplet,
@Nonnull final Triplet<Class<U>, Class<V>, Class<W>> types) { @Nonnull final Triplet<Class<U>, Class<V>, Class<W>> types) {
this.required = required; this.required = required;
this.name = name; this.name = name;