src/main/java/com/nibado/example/java8command/Command.java
static Command parse(String cmd) {
        if(cmd == null || cmd.trim().equals("")) {
            return VOID_COMMAND;
        }
        String[] parts = cmd.split(" ");

        switch(parts[0]) {
            case "add":
                return i ->  i.addAndGet(Integer.parseInt(parts[1]));
            case "sub":
                return i -> i.addAndGet(-Integer.parseInt(parts[1]));
            case "set":
                return i -> i.set(Integer.parseInt(parts[1]));
            case "rev":
                return new ReverseCommand();
            default:
                return VOID_COMMAND;
        }
    }
/**
     * Parses a string into a command. Returns VOID_COMMAND for invalid commands.
     *
     * @param cmd the command to parse
     * @return the command which can be applied to the model
     */
Parses a string into a command. Returns VOID_COMMAND for invalid commands.
@param cmd the command to parse
@return the command which can be applied to the model
Parses a string into a command . Returns VOID_COMMAND for invalid commands . @param cmd the command to parse @return the command which can be applied to the model
static Command parse(String cmd) {
        if(cmd == null || cmd.trim().equals("")) {
            return VOID_COMMAND;
        }
        String[] parts = cmd.split(" ");
        switch(parts[0]) {
            case "add":
                return i ->  i.addAndGet(Integer.parseInt(parts[1]));
            case "sub":
                return i -> i.addAndGet(-Integer.parseInt(parts[1]));
            case "set":
                return i -> i.set(Integer.parseInt(parts[1]));
            case "rev":
                return new ReverseCommand();
            default:
                return VOID_COMMAND;
        }
    }
static Command parse ( String cmd ) { if ( cmd == null || cmd . trim ( ) . equals ( "" ) ) { return VOID_COMMAND ; } String [ ] parts = cmd . split ( " " ) ; switch ( parts [ 0 ] ) { case "add" : return i -> i . addAndGet ( Integer . parseInt ( parts [ 1 ] ) ) ; case "sub" : return i -> i . addAndGet ( - Integer . parseInt ( parts [ 1 ] ) ) ; case "set" : return i -> i . set ( Integer . parseInt ( parts [ 1 ] ) ) ; case "rev" : return new ReverseCommand ( ) ; default : return VOID_COMMAND ; } }
Parses a string into a command.
Parses a string into a command .