All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jio.console.AddToListCommand Maven / Gradle / Ivy

There is a newer version: 3.0.0-RC2
Show newest version
package jio.console;


import jio.IO;
import jsonvalues.JsObj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;

class AddToListCommand extends Command {

  private static final String COMMAND_NAME = "var-add";

  public AddToListCommand() {
    super(COMMAND_NAME,
          """
              Add the given value into the the specified list. You can read
              the content of the list with the command var-get {name}.
              Usage: var-add {name} {value}
              Examples:
                  $command names Rafa
                  $command numbers $counter""".replace("$command",
                                                       COMMAND_NAME)
         );
  }

  private static IO addValueToList(State state,
                                           String varName,
                                           String newValue
                                          ) {

    return IO.lazy(() -> {
      if (!state.listsVariables.containsKey(varName)) {
        state.listsVariables.put(varName,
                                 new ArrayList<>());
      }

      state.listsVariables.get(varName)
                          .add(newValue);
      return "added! Size of " + varName + " is " + state.listsVariables.get(varName)
                                                                        .size();
    });
  }

  @Override
  public Function> apply(final JsObj conf,
                                              final State state
                                             ) {
    return tokens -> {
      int nTokens = tokens.length;

      if (nTokens == 1) {
        return Programs.ASK_FOR_PAIR(new Programs.AskForInputParams("Type the name of the list"),
                                     new Programs.AskForInputParams("Type the value to be added")
                                    )
                       .then(pair -> addValueToList(state,
                                                    pair.first(),
                                                    pair.second()));
      }

      if (nTokens == 2) {
        return Programs
            .ASK_FOR_INPUT(new Programs.AskForInputParams("Type the value to be added"))
            .then(value -> addValueToList(state,
                                          tokens[1],
                                          value));
      }

      return addValueToList(state,
                            tokens[1],
                            String.join(" ",
                                        Arrays.stream(tokens)
                                              .toList()
                                              .subList(2,
                                                       tokens.length))
                           );


    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy