StringUtils.java revision 1481
588N/A/*
588N/A * CDDL HEADER START
588N/A *
588N/A * The contents of this file are subject to the terms of the
588N/A * Common Development and Distribution License (the "License").
588N/A * You may not use this file except in compliance with the License.
588N/A *
588N/A * See LICENSE.txt included in this distribution for the specific
588N/A * language governing permissions and limitations under the License.
588N/A *
588N/A * When distributing Covered Code, include this CDDL HEADER in each
588N/A * file and include the License file at LICENSE.txt.
588N/A * If applicable, add the following below this CDDL HEADER, with the
588N/A * fields enclosed by brackets "[]" replaced with your own identifying
588N/A * information: Portions Copyright [yyyy] [name of copyright owner]
588N/A *
588N/A * CDDL HEADER END
588N/A */
588N/A
588N/Apackage org.opensolaris.opengrok.util;
588N/A
1185N/Aimport java.util.regex.Pattern;
1185N/A
588N/A/**
1185N/A * Various String utility methods.
1190N/A *
588N/A * @author austvik
588N/A */
588N/Apublic final class StringUtils {
588N/A
588N/A private StringUtils() {
588N/A // Only static utility methods
588N/A }
1190N/A
588N/A /**
588N/A * Returns true if the string is empty or only includes whitespace characters.
1190N/A *
588N/A * @param str the string to be checked
937N/A * @return true if string is empty or only contains whitespace charadcters
588N/A */
588N/A public static boolean isOnlyWhitespace(String str) {
588N/A for (int i = 0; i < str.length(); i++) {
588N/A if (!Character.isWhitespace(str.charAt(i))) {
588N/A return false;
588N/A }
588N/A }
588N/A return true;
588N/A }
937N/A
1481N/A /**
1481N/A * Check whether two strings are equal. In contrast to the string's equal
1481N/A * method this one allows {@code null} arguments.
1481N/A * @param s1 string to compare with s2
1481N/A * @param s2 string to compare with s1
1481N/A * @return {@code true} if either both arguments ar {@code null} or both are
1481N/A * {@code non-null} and s1.equals(s2).
1481N/A */
1481N/A public static boolean isSame(String s1, String s2) {
1481N/A if (s1 == null && s2 == null) {
1481N/A return true;
1481N/A }
1481N/A if (s1 == null || s2 == null) {
1481N/A return false;
1481N/A }
1481N/A return s1.equals(s2);
1481N/A }
1481N/A
1190N/A static final Pattern javaClassPattern =
1185N/A Pattern.compile("([a-z][A-Za-z]*\\.)+[A-Z][A-Za-z0-9]*");
937N/A /**
937N/A * Returns true if the string is possibly a full java class name
937N/A *
937N/A * @param s the string to be checked
937N/A * @return true if string could be a java class name
937N/A */
937N/A public static boolean isPossiblyJavaClass(String s) {
937N/A // Only match a small subset of possible class names to prevent false
937N/A // positives:
937N/A // - class must be qualified with a package name
937N/A // - only letters in package name, starting with lower case
937N/A // - class name must be in CamelCase, starting with upper case
1185N/A return javaClassPattern.matcher(s).matches();
937N/A }
1190N/A
588N/A}