package org.ycl.commons.text; import org.apache.commons.lang.StringUtils; /** * 1. leftBit *
Convert a String
to an int
, returning
* zero
if the conversion fails.
If the string is null
, zero
is returned.
* NumberUtils.toInt(null) = 0 * NumberUtils.toInt("") = 0 * NumberUtils.toInt("1") = 1 ** * @param str the string to convert, may be null * @return the int represented by the string, or
zero
if
* conversion fails
* @since 2.1
*/
public static int toInt(String str) {
return org.apache.commons.lang.math.NumberUtils.toInt(str);
}
/**
* Returns the minimum value in an array.
* * @param array an array, must not be null or empty * @return the minimum value in the array * @throws IllegalArgumentException ifarray
is null
* @throws IllegalArgumentException if array
is empty
*/
public static long min(long[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns min
long min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
/**
* Checks whether the String
contains only
* digit characters.
Null
and empty String will return
* false
.
String
to check
* @return true
if str contains only unicode numeric
*/
public static boolean isDigits(String str) {
if (StringUtils.isEmpty(str)) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* Checks whether the String a valid Java number.
* *Valid numbers include hexadecimal marked with the 0x
* qualifier, scientific notation and numbers marked with a type
* qualifier (e.g. 123L).
Null
and empty String will return
* false
.
String
to check
* @return true
if the string is a correctly formatted number
*/
public static boolean isNumber(String str) {
if (StringUtils.isEmpty(str)) {
return false;
}
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
int start = (chars[0] == '-') ? 1 : 0;
if (sz > start + 1) {
if (chars[start] == '0' && chars[start + 1] == 'x') {
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;
}
}
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
return foundDigit && !hasExp;
}
// 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;
}
public static void main(String args[]){
System.out.println(leftBit(1));
System.out.println(leftBit(2));
System.out.println(leftBit(3));
System.out.println(leftBit(4));
System.out.println(addAuth(6,3));
System.out.println(validAuth(6,1));
System.out.println(validAuth(6,2));
System.out.println(validAuth(14,3));
System.out.println(removeAuth(6,3));
System.out.println(org.apache.commons.lang.math.NumberUtils.isNumber(""));
System.out.println(org.apache.commons.lang.math.NumberUtils.isNumber(" "));
System.out.println(org.apache.commons.lang.math.NumberUtils.isNumber(null));
}
}