]> git.basschouten.com Git - openhab-addons.git/blob
19c4346d0fd10a86efa3190f15cb307062072d03
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mpd.internal.protocol;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * Class for encapsulating an MPD command
24  *
25  * @author Stefan Röllin - Initial contribution
26  */
27
28 @NonNullByDefault
29 public class MPDCommand {
30
31     private final String command;
32     private final List<String> parameters = new ArrayList<>();
33
34     /**
35      * Create an MPD command without parameters
36      *
37      * @param command the command to send
38      */
39     public MPDCommand(String command) {
40         this.command = command;
41     }
42
43     /**
44      * Create an MPD command with one Integer parameter
45      *
46      * @param command the command to send
47      * @param value parameter of the command
48      */
49     public MPDCommand(String command, Integer value) {
50         this.command = command;
51         parameters.add(Integer.toString(value));
52     }
53
54     /**
55      * Create an MPD command with parameters
56      *
57      * @param command the command to send
58      * @param parameters the parameters of the command to send
59      */
60     public MPDCommand(String command, String... parameters) {
61         this.command = command;
62         Collections.addAll(this.parameters, Arrays.copyOf(parameters, parameters.length));
63     }
64
65     /**
66      * Returns the command.
67      *
68      * @return the command
69      */
70     public String getCommand() {
71         return command;
72     }
73
74     /**
75      * Returns the command as one line, including the parameters
76      *
77      * @return the command and parameters
78      */
79     public String asLine() {
80         StringBuilder builder = new StringBuilder(command);
81
82         for (String param : parameters) {
83             builder.append(" ");
84             builder.append("\"");
85             builder.append(param.replace("\"", "\\\\\"").replace("'", "\\\\'"));
86             builder.append("\"");
87         }
88
89         return builder.toString();
90     }
91
92     @Override
93     public String toString() {
94         StringBuilder builder = new StringBuilder(command);
95
96         for (String param : parameters) {
97
98             builder.append(" ");
99             builder.append("\"");
100             if ("password".equals(command)) {
101                 builder.append(param.replaceAll(".", "."));
102             } else {
103                 builder.append(param.replace("\"", "\\\\\"").replace("'", "\\\\'"));
104             }
105             builder.append("\"");
106         }
107
108         return builder.toString();
109     }
110 }