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.onkyo.internal;
15 import java.util.Arrays;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.library.types.StringType;
23 * Helper to parse messages.
25 * @author Marcel Verpaalen - Initial contribution
28 public final class OnkyoParserHelper {
31 * Slices the string, removing empty values
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
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(", ", "", "")));
45 return StringType.EMPTY;