/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package de.steamwar.command;
import lombok.AllArgsConstructor;
import lombok.Setter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
class CommandPart {
private static final String[] EMPTY_ARRAY = new String[0];
@AllArgsConstructor
private static class CheckArgumentResult {
private final boolean success;
private final Object value;
}
private AbstractSWCommand command;
private AbstractTypeMapper typeMapper;
private AbstractValidator validator;
private AbstractGuardChecker guardChecker;
private Class> varArgType;
private String optional;
private GuardCheckType guardCheckType;
private CommandPart next = null;
@Setter
private boolean ignoreAsArgument = false;
public CommandPart(AbstractSWCommand command, AbstractTypeMapper typeMapper, AbstractValidator validator, AbstractGuardChecker guardChecker, Class> varArgType, String optional, GuardCheckType guardCheckType) {
this.command = command;
this.typeMapper = typeMapper;
this.validator = validator;
this.guardChecker = guardChecker;
this.varArgType = varArgType;
this.optional = optional;
this.guardCheckType = guardCheckType;
validatePart();
}
public void setNext(CommandPart next) {
if (varArgType != null) {
throw new IllegalArgumentException("There can't be a next part if this is a vararg part!");
}
this.next = next;
}
private void validatePart() {
if (guardCheckType == GuardCheckType.TAB_COMPLETE) {
throw new IllegalArgumentException("Tab complete is not allowed as a guard check type!");
}
if (optional != null && varArgType != null) {
throw new IllegalArgumentException("A vararg part can't have an optional part!");
}
if (optional != null) {
try {
typeMapper.map(null, EMPTY_ARRAY, optional);
} catch (Exception e) {
throw new IllegalArgumentException("The optional part is not valid!");
}
}
}
public void generateArgumentArray(Consumer errors, List