/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 org.openmicroscopy.shoola.util; /** * Helper class used to avoid problematic dependencies. * After code from commons-lang. * This is a tmp class. * * @author Jean-Marie Burel      * <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a> * @since 5.1 */ public class CommonsLangUtils { /** The line separator.*/ public static final String LINE_SEPARATOR = System.getProperty("line.separator"); /** * Returns the character a delimiter. * * @param ch the character to check * @param delimiters the delimiters * @return true if it is a delimiter */ private static boolean isDelimiter(final char ch, final char[] delimiters) { if (delimiters == null) { return Character.isWhitespace(ch); } for (final char delimiter : delimiters) { if (ch == delimiter) { return true; } } return false; } /** * Capitalizes the specified string. * * @param str The string to handle. * @return See above. */ public static String capitalize(final String str) { return capitalize(str, null); } /** * Capitalizes the specified string. * * @param str The string to handle. * @param delimiters The delimiters if any. * @return See above. */ public static String capitalize(final String str, final char... delimiters) { final int delimLen = delimiters == null ? -1 : delimiters.length; if (isEmpty(str) || delimLen == 0) { return str; } final char[] buffer = str.toCharArray(); boolean capitalizeNext = true; for (int i = 0; i < buffer.length; i++) { final char ch = buffer[i]; if (isDelimiter(ch, delimiters)) { capitalizeNext = true; } else if (capitalizeNext) { buffer[i] = Character.toTitleCase(ch); capitalizeNext = false; } } return new String(buffer); } /** * Wraps the specified text. * * @param str The string to handle. * @param wrapLength The limit * @param newLineStr The new line character. * @param wrapLongWords Pass <code>true</code> to wrap long word, * <code>false</code> otherwise. * @return See above. */ public static String wrap(final String str, int wrapLength, String newLineStr, final boolean wrapLongWords) { if (str == null) { return null; } if (newLineStr == null) { newLineStr = LINE_SEPARATOR; } if (wrapLength < 1) { wrapLength = 1; } final int inputLineLength = str.length(); int offset = 0; final StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32); while (offset < inputLineLength) { if (str.charAt(offset) == ' ') { offset++; continue; } // only last line without leading spaces is left if (inputLineLength - offset <= wrapLength) { break; } int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset); if (spaceToWrapAt >= offset) { // normal case wrappedLine.append(str.substring(offset, spaceToWrapAt)); wrappedLine.append(newLineStr); offset = spaceToWrapAt + 1; } else { // really long word or URL if (wrapLongWords) { // wrap really long word one line at a time wrappedLine.append(str.substring(offset, wrapLength + offset)); wrappedLine.append(newLineStr); offset += wrapLength; } else { // do not wrap really long word, just extend beyond limit spaceToWrapAt = str.indexOf(' ', wrapLength + offset); if (spaceToWrapAt >= 0) { wrappedLine.append(str.substring(offset, spaceToWrapAt)); wrappedLine.append(newLineStr); offset = spaceToWrapAt + 1; } else { wrappedLine.append(str.substring(offset)); offset = inputLineLength; } } } } // Whatever is left in line is short enough to just pass through wrappedLine.append(str.substring(offset)); return wrappedLine.toString(); } /** * Returns <code>true</code> if a CharSequence is whitespace, * empty ("") or null, <code>false</code> otherwise. * * @param cs the sequence to check. * @return See above. */ public static boolean isBlank(final CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) return true; for (int i = 0; i < strLen; i++) { if (Character.isWhitespace(cs.charAt(i)) == false) { return false; } } return true; } /** * Returns <code>true</code> if a CharSequence is not empty. * * @param cs The sequence to handle. * @return See above. */ public static boolean isNotBlank(final CharSequence cs) { return !isBlank(cs); } /** * Returns <code>true</code> if a CharSequence is not empty, * <code>false</code> otherwise. * * @param cs the sequence to check. * @return See above. */ public static boolean isNotEmpty(final CharSequence cs) { return !isEmpty(cs); } /** * Returns <code>true</code> if a CharSequence is empty ("") or null, * <code>false</code> otherwise. * * @param cs the sequence to check. * @return See above. */ public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; } /** * Removes the white space if any from the specified string. * * @param str The string to handle. * @return See above. */ public static String deleteWhitespace(final String str) { if (isEmpty(str)) { return str; } final int sz = str.length(); final char[] chs = new char[sz]; int count = 0; for (int i = 0; i < sz; i++) { if (!Character.isWhitespace(str.charAt(i))) { chs[count++] = str.charAt(i); } } if (count == sz) { return str; } return new String(chs, 0, count); } /** * Returns <code>true</code> if the passed string is a number, * <code>false</code> otherwise. * * @param str The string to handle. * @return See above. */ public static boolean isNumber(final String str) { if (isEmpty(str)) { return false; } final char[] chars = str.toCharArray(); int sz = chars.length; boolean hasExp = false; boolean hasDecPoint = false; boolean allowSigns = false; boolean foundDigit = false; // deal with any possible sign up front final int start = (chars[0] == '-') ? 1 : 0; if (sz > start + 1 && chars[start] == '0') { // leading 0 if ( (chars[start + 1] == 'x') || (chars[start + 1] == 'X') ) { // leading 0x/0X int i = start + 2; if (i == sz) { return false; // str == "0x" } // checking hex (it can't be anything else) for (; i < chars.length; i++) { if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) { return false; } } return true; } else if (Character.isDigit(chars[start + 1])) { // leading 0, but not hex, must be octal int i = start + 1; for (; i < chars.length; i++) { if (chars[i] < '0' || chars[i] > '7') { return false; } } return true; } } sz--; // don't want to loop to the last char, check it afterwords // for type qualifiers int i = start; // loop to the next to last char or to the last char if we need another digit to // make a valid number (e.g. chars[0..5] = "1234E") while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { if (chars[i] >= '0' && chars[i] <= '9') { foundDigit = true; allowSigns = false; } else if (chars[i] == '.') { if (hasDecPoint || hasExp) { // two decimal points or dec in exponent return false; } hasDecPoint = true; } else if (chars[i] == 'e' || chars[i] == 'E') { // we've already taken care of hex. if (hasExp) { // two E's return false; } if (!foundDigit) { return false; } hasExp = true; allowSigns = true; } else if (chars[i] == '+' || chars[i] == '-') { if (!allowSigns) { return false; } allowSigns = false; foundDigit = false; // we need a digit after the E } else { return false; } i++; } if (i < chars.length) { if (chars[i] >= '0' && chars[i] <= '9') { // no type qualifier, OK return true; } if (chars[i] == 'e' || chars[i] == 'E') { // can't have an E at the last byte return false; } if (chars[i] == '.') { if (hasDecPoint || hasExp) { // two decimal points or dec in exponent return false; } // single trailing decimal point after non-exponent is ok return foundDigit; } if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) { return foundDigit; } if (chars[i] == 'l' || chars[i] == 'L') { // not allowing L with an exponent or decimal point return foundDigit && !hasExp && !hasDecPoint; } // last character is illegal return false; } // allowSigns is true iff the val ends in 'E' // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass return !allowSigns && foundDigit; } }