com.hp.octane.integrations.utils.SdkStringUtils Maven / Gradle / Ivy
/*
* Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P.
* 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.hp.octane.integrations.utils;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class SdkStringUtils {
public static String strip(String str, String stripChars) {
if (isEmpty(str)) {
return str;
} else {
return stripEnd(stripStart(str, stripChars), stripChars);
}
}
public static String stripStart(String str, String stripChars) {
int strLen;
if (str != null && (strLen = str.length()) != 0) {
int start = 0;
if (stripChars == null) {
while (start != strLen && Character.isWhitespace(str.charAt(start))) {
++start;
}
} else {
if (stripChars.length() == 0) {
return str;
}
while (start != strLen && stripChars.indexOf(str.charAt(start)) != -1) {
++start;
}
}
return str.substring(start);
} else {
return str;
}
}
public static String stripEnd(String str, String stripChars) {
int end;
if (str != null && (end = str.length()) != 0) {
if (stripChars == null) {
while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) {
--end;
}
} else {
if (stripChars.length() == 0) {
return str;
}
while (end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1) {
--end;
}
}
return str.substring(0, end);
} else {
return str;
}
}
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public static String join(Collection collection, String separator) {
if (collection == null) {
return null;
}
StringBuffer buffer = new StringBuffer();
for (Iterator iterator = collection.iterator(); iterator.hasNext(); buffer.append(iterator.next().toString())) {
if (buffer.length() != 0) {
buffer.append(separator);
}
}
return buffer.toString();
}
public static String join(String[] collection, String separator) {
return join(Arrays.asList(collection), separator);
}
public static boolean equals(String str1, String str2) {
return str1 == null ? str2 == null : str1.equals(str2);
}
public static boolean equalsIgnoreCase(String str1, String str2) {
return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
}
}