]> git.basschouten.com Git - openhab-addons.git/blob
320b674d2ac47824a6ab8e704e71d6c587614d10
[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.bticinosmarther.internal.util;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.io.Reader;
19 import java.io.StringWriter;
20 import java.util.Arrays;
21 import java.util.stream.Collectors;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25
26 /**
27  * The {@code StringUtil} class defines common string utility functions used across the whole binding.
28  *
29  * @author Fabio Possieri - Initial contribution
30  */
31 @NonNullByDefault
32 public final class StringUtil {
33
34     private static final int EOF = -1;
35     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
36
37     /**
38      * Checks if a string is whitespace, empty ("") or {@code null}.
39      *
40      * @param str
41      *            the string to check, may be {@code null}
42      *
43      * @return {@code true} if the string is {@code null}, empty or whitespace
44      */
45     public static boolean isBlank(@Nullable String str) {
46         return (str == null || str.trim().isEmpty());
47     }
48
49     /**
50      * Returns either the passed in string or, if the string is {@code null}, an empty string ("").
51      *
52      * @param str
53      *            the string to check, may be {@code null}
54      *
55      * @return the passed in string, or the empty string if it was {@code null}
56      *
57      */
58     public static final String defaultString(@Nullable String str) {
59         return (str == null) ? "" : str;
60     }
61
62     /**
63      * Returns either the passed in string or, if the string is whitespace, empty ("") or {@code null}, a default value.
64      *
65      * @param str
66      *            the string to check, may be {@code null}
67      * @param defaultStr
68      *            the default string to return
69      *
70      * @return the passed in string, or the default one
71      */
72     public static String defaultIfBlank(String str, String defaultStr) {
73         return StringUtil.isBlank(str) ? defaultStr : str;
74     }
75
76     /**
77      * Strips whitespace from the start and end of a string returning {@code null} if the string is empty ("") after the
78      * strip.
79      *
80      * @param str
81      *            the string to be stripped, may be {@code null}
82      *
83      * @return the stripped string, {@code null} if whitespace, empty or {@code null} input string
84      */
85     public static @Nullable String stripToNull(@Nullable String str) {
86         if (str == null) {
87             return null;
88         }
89         String s = str.trim();
90         return (s.isEmpty()) ? null : s;
91     }
92
93     /**
94      * Capitalizes a string changing the first letter to title case as per {@link Character#toTitleCase(char)}. No other
95      * letters are changed.
96      *
97      * @param str
98      *            the string to capitalize, may be {@code null}
99      *
100      * @return the capitalized string, {@code null} if {@code null} input string
101      */
102     public static @Nullable String capitalize(@Nullable String str) {
103         if (str == null || str.isEmpty()) {
104             return str;
105         }
106         return str.substring(0, 1).toUpperCase() + str.substring(1);
107     }
108
109     /**
110      * Converts all the whitespace separated words in a string into capitalized words, that is each word is made up of a
111      * titlecase character and then a series of lowercase characters.
112      *
113      * @param str
114      *            the string to capitalize, may be {@code null}
115      *
116      * @return the capitalized string, {@code null} if {@code null} input string
117      */
118     public static @Nullable String capitalizeAll(@Nullable String str) {
119         if (str == null || str.isEmpty()) {
120             return str;
121         }
122         // Java 8 version
123         return Arrays.stream(str.split("\\s+")).map(t -> t.substring(0, 1).toUpperCase() + t.substring(1).toLowerCase())
124                 .collect(Collectors.joining(" "));
125         // Ready for Java 9+
126         // return Pattern.compile("\\b(.)(.*?)\\b").matcher(str)
127         // .replaceAll(match -> match.group(1).toUpperCase() + match.group(2).toLowerCase());
128     }
129
130     /**
131      * Get the contents of an {@link InputStream} stream as a string using the default character encoding of the
132      * platform. This method buffers the input internally, so there is no need to use a {@code BufferedInputStream}.
133      *
134      * @param input
135      *            the {@code InputStream} to read from
136      *
137      * @return the string read from stream
138      *
139      * @throws {@link IOException}
140      *             if an I/O error occurs
141      */
142     public static String streamToString(InputStream input) throws IOException {
143         InputStreamReader reader = new InputStreamReader(input);
144
145         final StringWriter writer = new StringWriter();
146         char[] buffer = new char[DEFAULT_BUFFER_SIZE];
147
148         int n = 0;
149         while ((n = reader.read(buffer)) != EOF) {
150             writer.write(buffer, 0, n);
151         }
152
153         return writer.toString();
154     }
155
156     /**
157      * Get the contents of a {@link Reader} stream as a string using the default character encoding of the platform.
158      * This method doesn't buffer the input internally, so eventually {@code BufferedReder} needs to be used externally.
159      *
160      * @param reader
161      *            the {@code Reader} to read from
162      *
163      * @return the string read from stream
164      *
165      * @throws {@link IOException}
166      *             if an I/O error occurs
167      */
168     public static String readerToString(Reader reader) throws IOException {
169         final StringWriter writer = new StringWriter();
170
171         int c;
172         while ((c = reader.read()) != EOF) {
173             writer.write(c);
174         }
175
176         return writer.toString();
177     }
178 }