/* * This file is a part of the SteamWar software. * * Copyright (C) 2022 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.NonNull; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.function.Function; public class PreviousArguments { public final String[] userArgs; public final Object[] mappedArgs; public final boolean hasMoreArguments; private final Function getByNameFunction; private boolean usedOptionalValue = false; public PreviousArguments(String[] userArgs, Object[] mappedArgs, boolean hasMoreArguments, Function getByNameFunction) { this.userArgs = userArgs; this.mappedArgs = mappedArgs; this.hasMoreArguments = hasMoreArguments; this.getByNameFunction = getByNameFunction; } public boolean hasUsedOptionalValue() { return usedOptionalValue; } public void usedOptionalValue() { this.usedOptionalValue = true; } public String getUserArg(int index) { return userArgs[userArgs.length - index - 1]; } public T getMappedArg(int index) { return (T) mappedArgs[mappedArgs.length - index - 1]; } public Optional getFirst(Class clazz) { for (Object o : mappedArgs) { if (clazz.isInstance(o)) { return Optional.of((T) o); } } return Optional.empty(); } public List getAll(Class clazz) { List list = new ArrayList<>(); for (Object o : mappedArgs) { if (clazz.isInstance(o)) { list.add((T) o); } if (o == null) { continue; } if (o.getClass().isArray()) { Object[] array = (Object[]) o; if (array.length == 0) { continue; } for (Object o1 : array) { if (clazz.isInstance(o1)) { list.add((T) o1); } } } } return list; } public T getByName(@NonNull String name) { return (T) getByNameFunction.apply(name); } }