Skip to content

Commit

Permalink
Changing parameter String to CharSequence in all parseStringToXXX met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
michaelg committed Jan 19, 2016
1 parent 6fc4a3b commit 31f5272
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 40 deletions.
33 changes: 30 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,38 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mgnt</groupId>
<groupId>com.github.michaelgantman.mgnt.utils</groupId>
<artifactId>MgntUtils</artifactId>
<version>1.0-SNAPSHOT</version>

<version>1.03</version>
<name>MgntUtils</name>
<description>
Set of various Utils: stacktrace noice filter, String to/from unicode sequence converter, Silent String parsing
to Integer and other numeric types, Version comparator and Version ranges operations, Self-throttling binary
reader from Http request, File reader.
</description>
<url>https://github.com/michaelgantman/Mgnt</url>
<licenses>
<license>
<name>The MIT License (MIT)</name>
<url>http://opensource.org/licenses/mit-license.php</url>
</license>
</licenses>
<developers>
<developer>
<name>
Michael Gantman
</name>
<email>
michael_gantman@yahoo.com
</email>
</developer>
</developers>

<scm>
<connection>scm:git:https://github.com/michaelgantman/Mgnt</connection>
<developerConnection>scm:git:https://github.com/michaelgantman/Mgnt</developerConnection>
<url>https://github.com/michaelgantman/Mgnt</url>
</scm>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
Expand Down
73 changes: 36 additions & 37 deletions src/main/java/com/mgnt/utils/TextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mgnt.utils.textutils.InvalidVersionFormatException;
import com.mgnt.utils.textutils.Version;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -108,23 +107,23 @@ public static int compareVersions(Version ver1, Version ver2) {
* project that uses this utility. If {@code nullOrEmptyStringErrorMessage} or {@code numberFormatErrorMessage} parameters are null or empty
* Strings than the correlating error will not be printed.
*
* @param numStr String to be parsed
* @param num CharSequence to be parsed
* @param defaultValue value that will be returned by this method if parsing of the String failed
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is not in appropriate format
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code num} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code num} is not in appropriate format
* @return numeric value parsed from the String
*/
public static int parseStringToInt(String numStr, int defaultValue,
public static int parseStringToInt(CharSequence num, int defaultValue,
String nullOrEmptyStringErrorMessage, String numberFormatErrorMessage) {
Integer result = null;
if (numStr == null || "".equals(numStr)) {
if (num == null || "".equals(num.toString())) {
if (nullOrEmptyStringErrorMessage != null && !"".equals(nullOrEmptyStringErrorMessage)) {
logger.warn(nullOrEmptyStringErrorMessage);
}
result = defaultValue;
} else {
try {
result = Integer.parseInt(numStr);
result = Integer.parseInt(num.toString());
} catch (NumberFormatException nfe) {
if (numberFormatErrorMessage != null && !"".equals(numberFormatErrorMessage)) {
warn(numberFormatErrorMessage, nfe);
Expand All @@ -142,23 +141,23 @@ public static int parseStringToInt(String numStr, int defaultValue,
* uses this utility. If {@code nullOrEmptyStringErrorMessage} or {@code numberFormatErrorMessage} parameters are null or empty Strings than the
* correlating error will not be printed.
*
* @param numStr String to be parsed
* @param num CharSequence to be parsed
* @param defaultValue value that will be returned by this method if parsing of the String failed
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is not in appropriate format
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code num} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code num} is not in appropriate format
* @return numeric value parsed from the String
*/
public static float parseStringToFloat(String numStr, float defaultValue,
public static float parseStringToFloat(CharSequence num, float defaultValue,
String nullOrEmptyStringErrorMessage, String numberFormatErrorMessage) {
Float result = null;
if (numStr == null || "".equals(numStr)) {
if (num == null || "".equals(num.toString())) {
if (nullOrEmptyStringErrorMessage != null && !"".equals(nullOrEmptyStringErrorMessage)) {
logger.warn(nullOrEmptyStringErrorMessage);
}
result = defaultValue;
} else {
try {
result = Float.parseFloat(numStr);
result = Float.parseFloat(num.toString());
} catch (NumberFormatException nfe) {
if (numberFormatErrorMessage != null && !"".equals(numberFormatErrorMessage)) {
warn(numberFormatErrorMessage, nfe);
Expand All @@ -176,23 +175,23 @@ public static float parseStringToFloat(String numStr, float defaultValue,
* uses this utility. If {@code nullOrEmptyStringErrorMessage} or {@code numberFormatErrorMessage} parameters are null or empty Strings than the
* correlating error will not be printed.
*
* @param numStr String to be parsed
* @param num CharSequence to be parsed
* @param defaultValue value that will be returned by this method if parsing of the String failed
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is not in appropriate format
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code num} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code num} is not in appropriate format
* @return numeric value parsed from the String
*/
public static Byte parseStringToByte(String numStr, byte defaultValue,
public static Byte parseStringToByte(CharSequence num, byte defaultValue,
String nullOrEmptyStringErrorMessage, String numberFormatErrorMessage) {
Byte result = null;
if (numStr == null || "".equals(numStr)) {
if (num == null || "".equals(num.toString())) {
if (nullOrEmptyStringErrorMessage != null && !"".equals(nullOrEmptyStringErrorMessage)) {
logger.warn(nullOrEmptyStringErrorMessage);
}
result = defaultValue;
} else {
try {
result = Byte.parseByte(numStr);
result = Byte.parseByte(num.toString());
} catch (NumberFormatException nfe) {
if (numberFormatErrorMessage != null && !"".equals(numberFormatErrorMessage)) {
warn(numberFormatErrorMessage, nfe);
Expand All @@ -210,23 +209,23 @@ public static Byte parseStringToByte(String numStr, byte defaultValue,
* uses this utility. If {@code nullOrEmptyStringErrorMessage} or {@code numberFormatErrorMessage} parameters are null or empty Strings than the
* correlating error will not be printed.
*
* @param numStr String to be parsed
* @param num CharSequence to be parsed
* @param defaultValue value that will be returned by this method if parsing of the String failed
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is not in appropriate format
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code num} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code num} is not in appropriate format
* @return numeric value parsed from the String
*/
public static double parseStringToDouble(String numStr, double defaultValue,
public static double parseStringToDouble(CharSequence num, double defaultValue,
String nullOrEmptyStringErrorMessage, String numberFormatErrorMessage) {
Double result = null;
if (numStr == null || "".equals(numStr)) {
if (num == null || "".equals(num.toString())) {
if (nullOrEmptyStringErrorMessage != null && !"".equals(nullOrEmptyStringErrorMessage)) {
logger.warn(nullOrEmptyStringErrorMessage);
}
result = defaultValue;
} else {
try {
result = Double.parseDouble(numStr);
result = Double.parseDouble(num.toString());
} catch (NumberFormatException nfe) {
if (numberFormatErrorMessage != null && !"".equals(numberFormatErrorMessage)) {
warn(numberFormatErrorMessage, nfe);
Expand All @@ -244,23 +243,23 @@ public static double parseStringToDouble(String numStr, double defaultValue,
* uses this utility. If {@code nullOrEmptyStringErrorMessage} or {@code numberFormatErrorMessage} parameters are null or empty Strings than the
* correlating error will not be printed.
*
* @param numStr String to be parsed
* @param num CharSequence to be parsed
* @param defaultValue value that will be returned by this method if parsing of the String failed
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is not in appropriate format
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code num} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code num} is not in appropriate format
* @return numeric value parsed from the String
*/
public static long parseStringToLong(String numStr, long defaultValue,
public static long parseStringToLong(CharSequence num, long defaultValue,
String nullOrEmptyStringErrorMessage, String numberFormatErrorMessage) {
Long result = null;
if (numStr == null || "".equals(numStr)) {
if (num == null || "".equals(num.toString())) {
if (nullOrEmptyStringErrorMessage != null && !"".equals(nullOrEmptyStringErrorMessage)) {
logger.warn(nullOrEmptyStringErrorMessage);
}
result = defaultValue;
} else {
try {
result = Long.parseLong(numStr);
result = Long.parseLong(num.toString());
} catch (NumberFormatException nfe) {
if (numberFormatErrorMessage != null && !"".equals(numberFormatErrorMessage)) {
warn(numberFormatErrorMessage, nfe);
Expand All @@ -278,23 +277,23 @@ public static long parseStringToLong(String numStr, long defaultValue,
* uses this utility. If {@code nullOrEmptyStringErrorMessage} or {@code numberFormatErrorMessage} parameters are null or empty Strings than the
* correlating error will not be printed.
*
* @param numStr String to be parsed
* @param num CharSequence to be parsed
* @param defaultValue value that will be returned by this method if parsing of the String failed
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code numStr} is not in appropriate format
* @param nullOrEmptyStringErrorMessage String that holds an error message that will printed into log if parameter {@code num} is null or blank
* @param numberFormatErrorMessage String that holds an error message that will printed into log if parameter {@code num} is not in appropriate format
* @return numeric value parsed from the String
*/
public static Short parseStringToShort(String numStr, short defaultValue,
public static Short parseStringToShort(CharSequence num, short defaultValue,
String nullOrEmptyStringErrorMessage, String numberFormatErrorMessage) {
Short result = null;
if (numStr == null || "".equals(numStr)) {
if (num == null || "".equals(num.toString())) {
if (nullOrEmptyStringErrorMessage != null && !"".equals(nullOrEmptyStringErrorMessage)) {
logger.warn(nullOrEmptyStringErrorMessage);
}
result = defaultValue;
} else {
try {
result = Short.parseShort(numStr);
result = Short.parseShort(num.toString());
} catch (NumberFormatException nfe) {
if (numberFormatErrorMessage != null && !"".equals(numberFormatErrorMessage)) {
warn(numberFormatErrorMessage, nfe);
Expand Down

0 comments on commit 31f5272

Please sign in to comment.