]> git.basschouten.com Git - openhab-addons.git/blob
aad838e5fcb1b101ab3c78f2f76129708d09ac91
[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.onkyo.internal;
14
15 import java.util.Arrays;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.library.types.StringType;
21
22 /**
23  * Helper to parse messages.
24  *
25  * @author Marcel Verpaalen - Initial contribution
26  */
27 @NonNullByDefault
28 public final class OnkyoParserHelper {
29
30     /**
31      * Slices the string, removing empty values
32      *
33      * @param data comma separated string
34      * @param startIndex initial index of the range to be copied
35      * @param endIndex final index of the range to be copied (inclusive)
36      * @return formatted StringType
37      */
38     public static StringType infoBuilder(String data, int startIndex, int endIndex) {
39         String[] params = data.split(",");
40         int toIndex = endIndex < params.length ? endIndex + 1 : params.length;
41         if (params.length >= startIndex) {
42             return new StringType(Stream.of(Arrays.copyOfRange(params, startIndex, toIndex))
43                     .filter(p -> p.trim().length() > 0).map(p -> p.trim()).collect(Collectors.joining(", ", "", "")));
44         }
45         return StringType.EMPTY;
46     }
47 }