下面列出了org.apache.commons.lang3.math.NumberUtils#createLong ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Inject
public ArchivaRuntimeInfo( @Named( value = "archivaRuntimeProperties" ) Properties archivaRuntimeProperties )
{
this.version = (String) archivaRuntimeProperties.get( "archiva.version" );
this.buildNumber = (String) archivaRuntimeProperties.get( "archiva.buildNumber" );
String archivaTimeStamp = (String) archivaRuntimeProperties.get( "archiva.timestamp" );
if ( NumberUtils.isNumber( archivaTimeStamp ) )
{
this.timestamp = NumberUtils.createLong( archivaTimeStamp );
}
else
{
this.timestamp = new Date().getTime();
}
this.devMode = Boolean.getBoolean( "archiva.devMode" );
}
/**
* Given a long integer string, it checks if it's a valid long integer (based on apaches NumberUtils.createLong)
*
* @param longStr the long integer string to check
* @return true if it's valid, otherwise false
*/
public static boolean isValidLong(@Nullable final String longStr) {
if (StringUtils.isBlank(longStr)) {
return false;
}
final String stripedLong = StringUtils.strip(longStr);
try {
NumberUtils.createLong(stripedLong);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* Given a long integer string if it's a valid long (see isValidLong) it converts it into a long integer otherwise it throws an exception
*
* @param longStr the long integer to convert
* @return the long integer value of the longStr
* @throws IllegalArgumentException if the passed long integer string is not a valid long value
*/
public static long toLong(@Nullable final String longStr) {
if (!isValidLong(longStr)) {
throw new IllegalArgumentException(longStr + ExceptionValues.EXCEPTION_DELIMITER + ExceptionValues.INVALID_LONG_VALUE);
}
final String stripedLong = StringUtils.strip(longStr);
return NumberUtils.createLong(stripedLong);
}