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.bticinosmarther.internal.util;
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;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
27 * The {@code StringUtil} class defines common string utility functions used across the whole binding.
29 * @author Fabio Possieri - Initial contribution
32 public final class StringUtil {
34 private static final int EOF = -1;
35 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
38 * Checks if a string is whitespace, empty ("") or {@code null}.
41 * the string to check, may be {@code null}
43 * @return {@code true} if the string is {@code null}, empty or whitespace
45 public static boolean isBlank(@Nullable String str) {
46 return (str == null || str.trim().isEmpty());
50 * Returns either the passed in string or, if the string is {@code null}, an empty string ("").
53 * the string to check, may be {@code null}
55 * @return the passed in string, or the empty string if it was {@code null}
58 public static final String defaultString(@Nullable String str) {
59 return (str == null) ? "" : str;
63 * Returns either the passed in string or, if the string is whitespace, empty ("") or {@code null}, a default value.
66 * the string to check, may be {@code null}
68 * the default string to return
70 * @return the passed in string, or the default one
72 public static String defaultIfBlank(String str, String defaultStr) {
73 return StringUtil.isBlank(str) ? defaultStr : str;
77 * Strips whitespace from the start and end of a string returning {@code null} if the string is empty ("") after the
81 * the string to be stripped, may be {@code null}
83 * @return the stripped string, {@code null} if whitespace, empty or {@code null} input string
85 public static @Nullable String stripToNull(@Nullable String str) {
89 String s = str.trim();
90 return (s.isEmpty()) ? null : s;
94 * Capitalizes a string changing the first letter to title case as per {@link Character#toTitleCase(char)}. No other
95 * letters are changed.
98 * the string to capitalize, may be {@code null}
100 * @return the capitalized string, {@code null} if {@code null} input string
102 public static @Nullable String capitalize(@Nullable String str) {
103 if (str == null || str.isEmpty()) {
106 return str.substring(0, 1).toUpperCase() + str.substring(1);
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.
114 * the string to capitalize, may be {@code null}
116 * @return the capitalized string, {@code null} if {@code null} input string
118 public static @Nullable String capitalizeAll(@Nullable String str) {
119 if (str == null || str.isEmpty()) {
123 return Arrays.stream(str.split("\\s+")).map(t -> t.substring(0, 1).toUpperCase() + t.substring(1).toLowerCase())
124 .collect(Collectors.joining(" "));
126 // return Pattern.compile("\\b(.)(.*?)\\b").matcher(str)
127 // .replaceAll(match -> match.group(1).toUpperCase() + match.group(2).toLowerCase());
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}.
135 * the {@code InputStream} to read from
137 * @return the string read from stream
139 * @throws {@link IOException}
140 * if an I/O error occurs
142 public static String streamToString(InputStream input) throws IOException {
143 InputStreamReader reader = new InputStreamReader(input);
145 final StringWriter writer = new StringWriter();
146 char[] buffer = new char[DEFAULT_BUFFER_SIZE];
149 while ((n = reader.read(buffer)) != EOF) {
150 writer.write(buffer, 0, n);
153 return writer.toString();
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.
161 * the {@code Reader} to read from
163 * @return the string read from stream
165 * @throws {@link IOException}
166 * if an I/O error occurs
168 public static String readerToString(Reader reader) throws IOException {
169 final StringWriter writer = new StringWriter();
172 while ((c = reader.read()) != EOF) {
176 return writer.toString();