com.sangupta.jerry.util.StringUtils Maven / Gradle / Ivy
/**
*
* jerry - Common Java Functionality
* Copyright (c) 2012-2014, Sandeep Gupta
*
* http://sangupta.com/projects/jerry
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.sangupta.jerry.util;
import java.nio.charset.Charset;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility functions around {@link String} objects.
*
* @author sangupta
*
*/
public class StringUtils {
/**
* Logger instance
*/
public static final Logger LOGGER = LoggerFactory.getLogger(StringUtils.class);
/**
* UTF-8 charset object
*/
public static final Charset CHARSET_UTF8 = Charset.forName("UTF8");
/**
* An empty string object containing nothing.
*/
public static final String EMPTY_STRING = "";
/**
* A blank string containing one blank white-space character.
*/
public static final String BLANK_STRING = " ";
/**
* Defines the platform dependent line encoding
*/
public static final String SYSTEM_NEW_LINE = System.getProperty("line.separator");
/**
* Function to give a HEX representation of the byte array in small-case.
*
* @param bytes
* the source byte array
*
* @return the HEX coded string representing the byte array
*/
public static String asHex(byte bytes[]) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; ++i) {
sb.append(Integer.toHexString((bytes[i] & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
/**
* Function to return a HEX representation of the given byte in small-case.
*
* @param b
* the byte value
*
* @return the hex representation
*
*/
public static String asHex(byte b) {
return Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3);
}
/**
* Return the {@link String} representation of the byte array. A
* null byte array is converted back to null, an
* empty array to an empty string or the bytes to the actual string based on
* platform encoding.
*
* @param bytes
* the byte array
*
* @return its string representation
*/
public static String asString(byte[] bytes) {
if(bytes == null) {
return null;
}
if(bytes.length == 0) {
return EMPTY_STRING;
}
return new String(bytes);
}
/**
* Return the {@link String} representation of the byte array. A
* null byte array is converted back to null, an
* empty array to an empty string or the bytes to the actual string in UTF-8
* encoding.
*
* @param bytes
* the byte array
*
* @return its string representation
*/
public static String asStringUTF8(byte[] bytes) {
if(bytes == null) {
return null;
}
if(bytes.length == 0) {
return EMPTY_STRING;
}
return new String(bytes, CHARSET_UTF8);
}
/**
* Return the actual array that this hex string represents.
*
* @param hex
* the hex string
*
* @return the actual byte array for the hex string
*/
public static byte[] fromHex(String hex) {
if(AssertUtils.isEmpty(hex)) {
return null;
}
int len = hex.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i+1), 16));
}
return data;
}
/**
* Return true if the string is equal to "true" or "yes" otherwise return
* false.
*
* @param boolString
* string whose content is checked for true or false
*
* @return boolean flag indicating whether the input was true or false
*/
public static boolean getBoolean(String boolString) {
return getBoolean(boolString, false);
}
/**
* Parse the string and return the boolean value for the same. Text such as
* yes and true is treated as {@link Boolean#TRUE}
* . If the string parsing fails, the default value is returned.
*
* @param boolString
* the string to be parsed
*
* @param defaultValue
* the value to be returned, if the string does not match
*
* @return the boolean value as read by parsing the string
*/
public static boolean getBoolean(String boolString, boolean defaultValue) {
if (AssertUtils.isNotEmpty(boolString)) {
boolString = boolString.toLowerCase();
if ("yes".equals(boolString) || "true".equals(boolString)) {
return true;
}
return false;
}
return defaultValue;
}
/**
* Parse and return the int value of the given string. If the string cannot
* be parsed, returns the default value provided
*
* @param string
* the string to be parsed
*
* @param defaultValue
* the value to be returned if parsing string fails
*
* @return the 32-bit value of the string
*/
public static int getIntValue(String string, int defaultValue) {
try {
if(AssertUtils.isNotEmpty(string)) {
return Integer.parseInt(string);
}
} catch(NumberFormatException e) {
LOGGER.debug("error getting integer from string: " + string, e);
}
return defaultValue;
}
/**
* Parse and return the long value of the given string. If the string cannot
* be parsed, returns the default value provided
*
* @param string
* the string to be parsed
*
* @param defaultValue
* the value to be returned if parsing string fails
*
* @return the 64-bit value of the string
*/
public static long getLongValue(String string, long defaultValue) {
try {
if(AssertUtils.isNotEmpty(string)) {
return Long.parseLong(string);
}
} catch(NumberFormatException e) {
LOGGER.debug("error getting long from string: " + string, e);
}
return defaultValue;
}
/**
* Parse and return the float value of the given string. If the string cannot
* be parsed, returns the default value provided
*
* @param string
* the string to be parsed
*
* @param defaultValue
* the value to be returned if parsing string fails
*
* @return the float value of the string
*/
public static float getFloatValue(String string, float defaultValue) {
try {
if(AssertUtils.isNotEmpty(string)) {
return Float.parseFloat(string);
}
} catch(NumberFormatException e) {
LOGGER.debug("error getting long from string: " + string, e);
}
return defaultValue;
}
/**
* Parse and return the double value of the given string. If the string cannot
* be parsed, returns the default value provided
*
* @param string
* the string to be parsed
*
* @param defaultValue
* the value to be returned if parsing string fails
*
* @return the double value of the string
*/
public static double getDoubleValue(String string, double defaultValue) {
try {
if(AssertUtils.isNotEmpty(string)) {
return Double.parseDouble(string);
}
} catch(NumberFormatException e) {
LOGGER.debug("error getting long from string: " + string, e);
}
return defaultValue;
}
/**
* Generate a given appender delimited string of all items in the list. If
* the list has no elements, an empty string is returned back.
*
* @param list
* the list containing objects to be joined
*
* @param appender
* the appender to be used
*
* @return the string representation of list using appender as the delimiter
*/
public String fromList(List © 2015 - 2025 Weber Informatics LLC | Privacy Policy