]> git.basschouten.com Git - openhab-addons.git/blob
1607b21fb12ac2ced2f552e60e50525779efaf2e
[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.sensibo.internal.util;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * The {@link StringUtils} class defines some static string utility methods
23  *
24  * @author Leo Siepel - Initial contribution
25  */
26 @NonNullByDefault
27 public class StringUtils {
28
29     public static String capitalizeFully(String input) {
30         final String delimiter = "_";
31         String capitalizedFully = "";
32         for (String str : input.split(delimiter)) {
33             String properlyCapitalized = "";
34             if (str.length() > 0) {
35                 properlyCapitalized = str.substring(0, 1).toUpperCase();
36             }
37             if (str.length() > 1) {
38                 properlyCapitalized = str.substring(1).toLowerCase();
39             }
40             capitalizedFully = capitalizedFully + properlyCapitalized;
41         }
42
43         return capitalizedFully;
44     }
45
46     public static String[] splitByCharacterType(@Nullable String input) {
47         if (input == null) {
48             return new String[0];
49         }
50         if (input.isBlank()) {
51             return new String[0];
52         }
53         List<String> cache = new ArrayList<>();
54         char[] inputAsCharArray = input.toCharArray();
55         int prevType = Character.getType(inputAsCharArray[0]);
56         int prevTypeStart = 0;
57         for (int i = prevTypeStart + 1; i < inputAsCharArray.length; i++) {
58             int curType = Character.getType(inputAsCharArray[i]);
59             if (prevType == curType) {
60                 continue;
61             }
62             if (curType == Character.LOWERCASE_LETTER && prevType == Character.UPPERCASE_LETTER) {
63                 int tmpStart = i - 1;
64                 if (tmpStart != prevTypeStart) {
65                     cache.add(new String(inputAsCharArray, prevTypeStart, tmpStart - prevTypeStart));
66                     prevTypeStart = tmpStart;
67                 }
68             } else {
69                 cache.add(new String(inputAsCharArray, prevTypeStart, i - prevTypeStart));
70                 prevTypeStart = i;
71             }
72             prevType = curType;
73         }
74         cache.add(new String(inputAsCharArray, prevTypeStart, inputAsCharArray.length - prevTypeStart));
75         return cache.toArray(String[]::new);
76     }
77 }