]> git.basschouten.com Git - openhab-addons.git/blob
40cff3a0ffb0d9626b52fa85f81ab38c14e5a382
[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.homematic.internal.misc;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Global utility class with helper methods.
20  *
21  * @author Gerhard Riegler - Initial contribution
22  */
23 public class MiscUtils {
24     private static final Logger LOGGER = LoggerFactory.getLogger(MiscUtils.class);
25
26     /**
27      * Replaces invalid characters of the text to fit into an openHAB UID.
28      */
29     public static String validateCharacters(String text, String textType, String replaceChar) {
30         if (text == null) {
31             return "EMPTY";
32         }
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);
36         }
37         return cleanedText;
38     }
39
40     /**
41      * Returns true, if the value is not null and true.
42      */
43     public static boolean isTrueValue(Object value) {
44         return Boolean.TRUE.equals(value);
45     }
46
47     /**
48      * Returns true, if the value is not null and false.
49      */
50     public static boolean isFalseValue(Object value) {
51         return Boolean.FALSE.equals(value);
52     }
53
54     /**
55      * Returns true, if str starts with search. Check is done case-insensitive.
56      */
57     public static boolean strStartsWithIgnoreCase(String str, String search) {
58         if (str == null || search == null || search.length() > str.length()) {
59             return false;
60         }
61         return str.substring(0, search.length()).equalsIgnoreCase(search);
62     }
63
64     /**
65      * Returns true if address is a device
66      */
67     public static boolean isDevice(String address) {
68         return isDevice(address, false);
69     }
70
71     /**
72      * Returns true if address is a device. If allowBidCos ist true then addresses starting with "BidCos" classified as
73      * devices, too.
74      */
75     public static boolean isDevice(String address, boolean allowBidCos) {
76         if (address == null) {
77             return false;
78         }
79         if (address.contains(":")) {
80             return false;
81         }
82         if (allowBidCos && strStartsWithIgnoreCase(address.trim(), "BidCos")) {
83             return true;
84         }
85         return !strStartsWithIgnoreCase(address.trim(), "BidCos");
86     }
87
88     /**
89      * Changes all characters after whitespace to upper-case and all other character to lower case.
90      */
91     public static String capitalize(String value) {
92         if (value == null) {
93             return null;
94         }
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;
100             } else {
101                 if (capitalizeNextChar) {
102                     chars[i] = Character.toTitleCase(chars[i]);
103                     capitalizeNextChar = false;
104                 } else {
105                     chars[i] = Character.toLowerCase(chars[i]);
106                 }
107             }
108         }
109         return new String(chars);
110     }
111 }