2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mpd.internal.protocol;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * Class for encapsulating an MPD command
25 * @author Stefan Röllin - Initial contribution
29 public class MPDCommand {
31 private final String command;
32 private final List<String> parameters = new ArrayList<>();
35 * Create an MPD command without parameters
37 * @param command the command to send
39 public MPDCommand(String command) {
40 this.command = command;
44 * Create an MPD command with one Integer parameter
46 * @param command the command to send
47 * @param value parameter of the command
49 public MPDCommand(String command, Integer value) {
50 this.command = command;
51 parameters.add(Integer.toString(value));
55 * Create an MPD command with parameters
57 * @param command the command to send
58 * @param parameters the parameters of the command to send
60 public MPDCommand(String command, String... parameters) {
61 this.command = command;
62 Collections.addAll(this.parameters, Arrays.copyOf(parameters, parameters.length));
66 * Returns the command.
70 public String getCommand() {
75 * Returns the command as one line, including the parameters
77 * @return the command and parameters
79 public String asLine() {
80 StringBuilder builder = new StringBuilder(command);
82 for (String param : parameters) {
85 builder.append(param.replaceAll("\"", "\\\\\"").replaceAll("'", "\\\\'"));
89 return builder.toString();
93 public String toString() {
94 StringBuilder builder = new StringBuilder(command);
96 for (String param : parameters) {
100 if ("password".equals(command)) {
101 builder.append(param.replaceAll(".", "."));
103 builder.append(param.replaceAll("\"", "\\\\\"").replaceAll("'", "\\\\'"));
105 builder.append("\"");
108 return builder.toString();