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.homematic.internal.misc;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Global utility class with helper methods.
21 * @author Gerhard Riegler - Initial contribution
23 public class MiscUtils {
24 private static final Logger logger = LoggerFactory.getLogger(MiscUtils.class);
27 * Replaces invalid characters of the text to fit into an openHAB UID.
29 public static String validateCharacters(String text, String textType, String replaceChar) {
33 String cleanedText = text.replaceAll("[^A-Za-z0-9_-]", replaceChar);
34 if (!text.equals(cleanedText)) {
35 logger.info("{} '{}' contains invalid characters, new {} '{}'", textType, text, textType, cleanedText);
41 * Returns true, if the value is not null and true.
43 public static boolean isTrueValue(Object value) {
44 return Boolean.TRUE.equals(value);
48 * Returns true, if the value is not null and false.
50 public static boolean isFalseValue(Object value) {
51 return Boolean.FALSE.equals(value);
55 * Returns true, if str starts with search. Check is done case-insensitive.
57 public static boolean strStartsWithIgnoreCase(String str, String search) {
58 if (str == null || search == null || search.length() > str.length()) {
61 return str.substring(0, search.length()).equalsIgnoreCase(search);
65 * Returns true if address is a device
67 public static boolean isDevice(String address) {
68 return isDevice(address, false);
72 * Returns true if address is a device. If allowBidCos ist true then addresses starting with "BidCos" classified as
75 public static boolean isDevice(String address, boolean allowBidCos) {
76 if (address == null) {
79 if (address.contains(":")) {
82 if (allowBidCos && strStartsWithIgnoreCase(address.trim(), "BidCos")) {
85 return !strStartsWithIgnoreCase(address.trim(), "BidCos");
89 * Changes all characters after whitespace to upper-case and all other character to lower case.
91 public static String capitalize(String value) {
95 char[] chars = value.toCharArray();
96 boolean capitalizeNextChar = true;
97 for (int i = 0; i < chars.length; i++) {
98 if (Character.isWhitespace(chars[i])) {
99 capitalizeNextChar = true;
101 if (capitalizeNextChar) {
102 chars[i] = Character.toTitleCase(chars[i]);
103 capitalizeNextChar = false;
105 chars[i] = Character.toLowerCase(chars[i]);
109 return new String(chars);