125 lines
3.3 KiB
Java
125 lines
3.3 KiB
Java
package de.nclazz.commons.util;
|
|
|
|
import lombok.NonNull;
|
|
import lombok.SneakyThrows;
|
|
|
|
import java.io.InputStream;
|
|
import java.security.MessageDigest;
|
|
|
|
public abstract class StringUtils {
|
|
|
|
private StringUtils() { /* no-op */ }
|
|
|
|
/**
|
|
* Checks if a String is null or does not contain a character
|
|
* other than whitespace.
|
|
*
|
|
* @param s String to check
|
|
* @return {@code true} if String is empty
|
|
*/
|
|
public static boolean isEmpty(String s) {
|
|
if(s == null) {
|
|
return true;
|
|
}
|
|
if(s.isEmpty()) {
|
|
return true;
|
|
}
|
|
for(int i = 0; i < s.length(); i++) {
|
|
if(!Character.isWhitespace(s.charAt(i))) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Capitalizes a String, meaning only the first letter becomes an
|
|
* upper case letter if not already.
|
|
* <p>i.e. {@code fooBar} -> {@code FooBar}</p>
|
|
* @param s String to capitalize
|
|
* @return capitalized String
|
|
* @see #decapitalize(String)
|
|
*/
|
|
public static String capitalize(String s) {
|
|
if(s != null) {
|
|
s = s.substring(0, 1).toUpperCase() + s.substring(1);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
/**
|
|
* Decapitalizes a String, meaning only the first letter becomes a
|
|
*lower case letter if not already.
|
|
* <p>i.e. {@code FooBar} -> {@code fooBar}</p>
|
|
* @param s String to decapitalize
|
|
* @return decapitalized String
|
|
* @see #capitalize(String)
|
|
*/
|
|
public static String decapitalize(String s) {
|
|
if(s != null) {
|
|
s = s.substring(0, 1).toLowerCase() + s.substring(1);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
public static String center(String s, int size) {
|
|
return center(s, size, ' ');
|
|
}
|
|
|
|
public static String center(String s, int size, char pad) {
|
|
if (s == null || size <= s.length())
|
|
return s;
|
|
|
|
StringBuilder sb = new StringBuilder(size);
|
|
for (int i = 0; i < (size - s.length()) / 2; i++) {
|
|
sb.append(pad);
|
|
}
|
|
sb.append(s);
|
|
while (sb.length() < size) {
|
|
sb.append(pad);
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
* Create a hex-{@code String} from given byte array,
|
|
* @param bytes array
|
|
* @return new {@code String}
|
|
* @throws NullPointerException if bytes are {@code null}
|
|
*/
|
|
public static String toHex(@NonNull byte[] bytes) {
|
|
StringBuilder sb = new StringBuilder(bytes.length*2);
|
|
|
|
for (int v : bytes) {
|
|
sb.append(Character.forDigit((v >> 4) & 0xF, 16))
|
|
.append(Character.forDigit((v & 0xF), 16));
|
|
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
* Hashes given String with given hash algorithm
|
|
*
|
|
* @param input to hash
|
|
* @param md hash algorithm
|
|
* @return hash (hex-)String
|
|
*/
|
|
@SneakyThrows
|
|
public static String digestString(@NonNull String input, MessageDigest md) {
|
|
md.update(input.getBytes());
|
|
return toHex(md.digest());
|
|
}
|
|
|
|
@SneakyThrows
|
|
public static String toSHA1(String input) {
|
|
return digestString(input, MessageDigest.getInstance("SHA-1"));
|
|
}
|
|
|
|
@SneakyThrows
|
|
public static String toMD(String input) {
|
|
return digestString(input, MessageDigest.getInstance("MD5"));
|
|
}
|
|
|
|
}
|