com.pi4j.library.linuxfs.util.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pi4j-library-linuxfs Show documentation
Show all versions of pi4j-library-linuxfs Show documentation
Pi4J wrapper for the LinuxFS library
package com.pi4j.library.linuxfs.util;
/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: LIBRARY :: JNI Wrapper for LinuxFS Library
* FILENAME : StringUtil.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* StringUtil class.
*
* @author Robert Savage (http://www.savagehomeautomation.com)
* @version $Id: $Id
*/
public class StringUtil {
/** Constant EMPTY=""
*/
public static final String EMPTY = "";
/** Constant DEFAULT_PAD_CHAR=' '
*/
public static final char DEFAULT_PAD_CHAR = ' ';
/**
* isNullOrEmpty.
*
* @param data a {@link String} object.
* @param trim a boolean.
* @return a boolean.
*/
public static boolean isNullOrEmpty(String data, boolean trim){
if(data == null)
return true;
// trim if requested
String test = data;
if(trim)
test = data.trim();
return (test.length() <= 0);
}
/**
* isNullOrEmpty.
*
* @param data a {@link String} object.
* @return a boolean.
*/
public static boolean isNullOrEmpty(String data){
return isNullOrEmpty(data, false);
}
/**
* isNotNullOrEmpty.
*
* @param data a {@link String} object.
* @return a boolean.
*/
public static boolean isNotNullOrEmpty(String data){
return isNotNullOrEmpty(data, false);
}
/**
* isNotNullOrEmpty.
*
* @param data a {@link String} object.
* @param trim a boolean.
* @return a boolean.
*/
public static boolean isNotNullOrEmpty(String data, boolean trim){
return !(isNullOrEmpty(data, trim));
}
/**
* setIfNullOrEmpty.
*
* @param data a {@link String} object.
* @param replacement a {@link String} object.
* @param trim a boolean.
* @return a {@link String} object.
*/
public static String setIfNullOrEmpty(String data, String replacement, boolean trim){
if(isNullOrEmpty(data, trim)) {
return replacement;
}
return data;
}
/**
* setIfNullOrEmpty.
*
* @param data a {@link String} object.
* @param replacement a {@link String} object.
* @return a {@link String} object.
*/
public static String setIfNullOrEmpty(String data, String replacement){
return setIfNullOrEmpty(data, replacement, false);
}
/**
* contains.
*
* @param source a {@link String} object.
* @param target a {@link String} object.
* @return a boolean.
*/
public static boolean contains(String source, String target) {
return (null != source && null != target && source.contains(target));
}
/**
* contains.
*
* @param source a {@link String} object.
* @param targets an array of {@link String} objects.
* @return a boolean.
*/
public static boolean contains(String source, String[] targets) {
if (null != source && null != targets) {
for(var target : targets) {
if (source.contains(target)) {
return true;
}
}
}
return false;
}
/**
* contains.
*
* @param sources an array of {@link String} objects.
* @param target a {@link String} object.
* @return a boolean.
*/
public static boolean contains(String[] sources, String target) {
if (null != sources && null != target) {
for (var source : sources) {
if(contains(source, target))
return true;
}
}
return false;
}
/**
* contains.
*
* @param sources an array of {@link String} objects.
* @param targets an array of {@link String} objects.
* @return a boolean.
*/
public static boolean contains(String[] sources, String[] targets) {
if (null != sources && null != targets) {
for (var source : sources) {
if(contains(source, targets))
return true;
}
}
return false;
}
/**
* create.
*
* @param length a int.
* @return a {@link String} object.
*/
public static String create(int length) {
return create(DEFAULT_PAD_CHAR, length);
}
/**
* create.
*
* @param c a char.
* @param length a int.
* @return a {@link String} object.
*/
public static String create(char c, int length) {
StringBuilder sb = new StringBuilder(length);
for(var index = 0; index < length; index++)
sb.append(c);
return sb.toString();
}
/**
* create.
*
* @param s a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String create(String s, int length) {
StringBuilder sb = new StringBuilder(length * s.length());
for(var index = 0; index < length; index++)
sb.append(s);
return sb.toString();
}
/**
* repeat.
*
* @param c a char.
* @param length a int.
* @return a {@link String} object.
*/
public static String repeat(char c, int length) {
return create(c, length);
}
/**
* repeat.
*
* @param s a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String repeat(String s, int length) {
return create(s, length);
}
/**
* padLeft.
*
* @param data a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String padLeft(String data, int length) {
return padLeft(data, DEFAULT_PAD_CHAR, length);
}
/**
* padLeft.
*
* @param data a {@link String} object.
* @param pad a char.
* @param length a int.
* @return a {@link String} object.
*/
public static String padLeft(String data, char pad, int length) {
var sb = new StringBuilder(data.length() + length);
for(var index = 0; index < length; index++)
sb.append(pad);
sb.append(data);
return sb.toString();
}
/**
* padLeft.
*
* @param data a {@link String} object.
* @param pad a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String padLeft(String data, String pad, int length) {
var sb = new StringBuilder(data.length() + (length * pad.length()));
for(var index = 0; index < length; index++)
sb.append(pad);
sb.append(data);
return sb.toString();
}
/**
* padRight.
*
* @param data a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String padRight(String data, int length) {
return padRight(data, DEFAULT_PAD_CHAR, length);
}
/**
* padRight.
*
* @param data a {@link String} object.
* @param pad a char.
* @param length a int.
* @return a {@link String} object.
*/
public static String padRight(String data, char pad, int length) {
var sb = new StringBuilder(data.length() + length);
sb.append(data);
for(var index = 0; index < length; index++)
sb.append(pad);
return sb.toString();
}
/**
* padRight.
*
* @param data a {@link String} object.
* @param pad a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String padRight(String data, String pad, int length) {
var sb = new StringBuilder(data.length() + (length * pad.length()));
sb.append(data);
for(var index = 0; index < length; index++)
sb.append(pad);
return sb.toString();
}
/**
* pad.
*
* @param data a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String pad(String data, int length) {
return pad(data, DEFAULT_PAD_CHAR, length);
}
/**
* pad.
*
* @param data a {@link String} object.
* @param pad a char.
* @param length a int.
* @return a {@link String} object.
*/
public static String pad(String data, char pad, int length) {
return create(pad, length) + data + create(pad, length);
}
/**
* pad.
*
* @param data a {@link String} object.
* @param pad a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String pad(String data, String pad, int length) {
return create(pad, length) + data + create(pad, length);
}
/**
* padCenter.
*
* @param data a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String padCenter(String data, int length) {
return padCenter(data, DEFAULT_PAD_CHAR, length);
}
/**
* padCenter.
*
* @param data a {@link String} object.
* @param pad a char.
* @param length a int.
* @return a {@link String} object.
*/
public static String padCenter(String data, char pad, int length) {
if(data.length() < length) {
int needed = length - data.length();
int padNeeded = needed / 2;
StringBuilder result = new StringBuilder();
result.append(create(pad, padNeeded));
result.append(data);
result.append(create(pad, padNeeded));
int remaining = length - result.length();
result.append(create(pad, remaining));
return result.toString();
}
return data;
}
/**
* trimLeft.
*
* @param data a {@link String} object.
* @return a {@link String} object.
*/
public static String trimLeft(String data) {
return trimLeft(data, DEFAULT_PAD_CHAR);
}
/**
* trimLeft.
*
* @param data a {@link String} object.
* @param trim a char.
* @return a {@link String} object.
*/
public static String trimLeft(String data, char trim) {
for(var index = 0; index < data.length(); index++)
if(!(data.charAt(index) == trim))
return data.substring(index);
return EMPTY;
}
/**
* trimRight.
*
* @param data a {@link String} object.
* @return a {@link String} object.
*/
public static String trimRight(String data) {
return trimRight(data, DEFAULT_PAD_CHAR);
}
/**
* trimRight.
*
* @param data a {@link String} object.
* @param trim a char.
* @return a {@link String} object.
*/
public static String trimRight(String data, char trim) {
int count = 0;
for(var index = data.length(); index > 0; index--)
if(data.charAt(index-1) == trim)
count++;
else
return data.substring(0, data.length() - count);
return EMPTY;
}
/**
* trim.
*
* @param data a {@link String} object.
* @return a {@link String} object.
*/
public static String trim(String data) {
return trim(data, DEFAULT_PAD_CHAR);
}
/**
* trim.
*
* @param data a {@link String} object.
* @param trim a char.
* @return a {@link String} object.
*/
public static String trim(String data, char trim) {
var result = trimLeft(data, trim);
return trimRight(result, trim);
}
/**
* center.
*
* @param text a {@link String} object.
* @param length a int.
* @return a {@link String} object.
*/
public static String center(String text, int length){
var out = String.format("%"+length+"s%s%"+length+"s", "",text,"");
var mid = (out.length()/2);
var start = mid - (length/2);
var end = start + length;
return out.substring((int) start, (int) end);
}
/**
* concat.
*
* @param data a {@link String} object.
* @return a {@link String} object.
*/
public static String concat(String ... data) {
var sb = new StringBuilder();
for(var d : data){
sb.append(d);
}
return sb.toString();
}
/**
* appendHexString.
*
* @param builder a {@link StringBuilder} object.
* @param byt a byte.
*/
public static void appendHexString(StringBuilder builder, byte byt){
builder.append(String.format("%02X", byt));
}
/**
* toHexString.
*
* @param byt a byte.
* @return a {@link String} object.
*/
public static String toHexString(byte byt){
return String.format("%02X", byt);
}
/**
* appendHexString.
*
* @param builder a {@link StringBuilder} object.
* @param byt a int.
*/
public static void appendHexString(StringBuilder builder, int byt){
builder.append(String.format("%02X", (byte)byt));
}
/**
* toHexString.
*
* @param byt a int.
* @return a {@link String} object.
*/
public static String toHexString(int byt){
return String.format("%02X", (byte)byt);
}
/**
* appendHexString.
*
* @param builder a {@link StringBuilder} object.
* @param bytes an array of {@link byte} objects.
*/
public static void appendHexString(StringBuilder builder, byte[] bytes){
for (byte b : bytes) {
builder.append(String.format("%02X ", b));
}
}
/**
* toHexString.
*
* @param bytes an array of {@link byte} objects.
* @return a {@link String} object.
*/
public static String toHexString(byte[] bytes){
StringBuilder sb = new StringBuilder();
appendHexString(sb, bytes);
return sb.toString().trim();
}
/**
* appendHexString.
*
* @param builder a {@link StringBuilder} object.
* @param buffer a {@link ByteBuffer} object.
*/
public static void appendHexString(StringBuilder builder, ByteBuffer buffer){
appendHexString(builder, buffer.array());
}
/**
* toHexString.
*
* @param buffer a {@link ByteBuffer} object.
* @return a {@link String} object.
*/
public static String toHexString(ByteBuffer buffer){
StringBuilder sb = new StringBuilder();
appendHexString(sb, buffer);
return sb.toString().trim();
}
/**
* appendHexString.
*
* @param builder a {@link StringBuilder} object.
* @param bytes an array of {@link byte} objects.
* @param offset a int.
* @param length a int.
*/
public static void appendHexString(StringBuilder builder, byte[] bytes, int offset, int length){
appendHexString(builder, Arrays.copyOfRange(bytes, offset, length));
}
/**
* toHexString.
*
* @param bytes an array of {@link byte} objects.
* @param offset a int.
* @param length a int.
* @return a {@link String} object.
*/
public static String toHexString(byte[] bytes, int offset, int length){
StringBuilder sb = new StringBuilder();
appendHexString(sb, bytes, offset, length);
return sb.toString().trim();
}
/**
* appendHexString.
*
* @param builder a {@link StringBuilder} object.
* @param buffer a {@link ByteBuffer} object.
* @param offset a int.
* @param length a int.
*/
public static void appendHexString(StringBuilder builder, ByteBuffer buffer, int offset, int length){
appendHexString(builder, buffer.array(), offset, length);
}
/**
* toHexString.
*
* @param buffer a {@link ByteBuffer} object.
* @param offset a int.
* @param length a int.
* @return a {@link String} object.
*/
public static String toHexString(ByteBuffer buffer, int offset, int length){
StringBuilder sb = new StringBuilder();
appendHexString(sb, buffer, offset, offset+length);
return sb.toString().trim();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy