]> git.basschouten.com Git - openhab-addons.git/blob
ccc91f76cc130f37380f7507316c1faeb3c9b3bf
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 a 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 value != null && value == Boolean.TRUE;
45     }
46
47     /**
48      * Returns true, if the value is not null and false.
49      */
50     public static boolean isFalseValue(Object value) {
51         return value != null && value == Boolean.FALSE;
52     }
53 }