com.elephantdrummer.parser.ParserString Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of drummer Show documentation
Show all versions of drummer Show documentation
Elephant Drummer Java Job Scheduler
The newest version!
package com.elephantdrummer.parser;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import com.elephantdrummer.exception.ParserException;
import com.elephantdrummer.validator.ValidatorNumeric;
/**
* Copyright 2018 Elephant Software Klaudiusz Wojtkowiak e-mail: [email protected]
*
* Licensed 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.
*/
public interface ParserString {
public static BigDecimal toBigDecimal(String from) {
if (from==null||from.trim().isEmpty()) return null;
ValidatorNumeric.validateNumberOrError(from);
return new BigDecimal(from);
}
public static boolean toBooleanSmall(String from,boolean inCaseOfNull) {
if (from==null||from.isEmpty()) return inCaseOfNull;
String data=from.trim();
if (data==null||data.trim().length()==0) return inCaseOfNull;
if (data.equalsIgnoreCase("true")||data.equalsIgnoreCase("false")){
return data.equalsIgnoreCase("true");
}else if (data.equalsIgnoreCase("1")||data.equalsIgnoreCase("0")){
return data.equalsIgnoreCase("1");
}else if (data.equalsIgnoreCase("T")||data.equalsIgnoreCase("N")){
return data.equalsIgnoreCase("T");
}else if (data.equalsIgnoreCase("Y")||data.equalsIgnoreCase("N")){
return data.equalsIgnoreCase("Y");
}else{
throw new ParserException("Value '"+from+"' can not be translated to boolean.");
}
}
public static Boolean toBoolean(String from,String inCaseOfTrue,String inCaseOfFalse) {
if (from==null||from.trim().equals("")) return null;
if (from.equals(inCaseOfTrue)) return true;
if (from.equals(inCaseOfFalse)) return false;
throw new ParserException("Value '"+from+"' can not be translated to boolean.");
}
public static Boolean toBooleanBig(String from) {
return from==null?null:toBooleanSmall(from,false);
}
public static boolean toBooleanSmall(String from) {
return toBooleanSmall(from,false);
}
/******* EO Boolean ***/
public static Double toDouble(String from) {
if (from==null||from.trim().isEmpty()) return null;
return Double.parseDouble(ValidatorNumeric.validateNumberOrError(from));
}
public static Float toFloat(String from) {
if (from==null||from.trim().isEmpty()) return null;
return Float.parseFloat(ValidatorNumeric.validateNumberOrError(from));
}
public static Integer toInteger(String from) {
if (from==null||from.trim().isEmpty()) return null;
return Integer.parseInt(ValidatorNumeric.validateNumberOrError(from));
}
public static int toSmallIntegerWithDefault(String from,int defaultValueWhenNull) {
if (from==null||from.trim().isEmpty()) return defaultValueWhenNull;
return Integer.parseInt(ValidatorNumeric.validateNumberOrError(from));
}
public static Long toLong(String from) {
if (from==null||from.trim().isEmpty()) return null;
return Long.parseLong(ValidatorNumeric.validateNumberOrError(from));
}
public static byte[] toByteArrayFromUTF8(String from) {
if (from==null) return null;
try {
return from.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new ParserException("UnsupportedEncodingException",e);
}
}
public static byte[] toByteArray(String from,String charset) {
if (from==null) return null;
try {
return from.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new ParserException("UnsupportedEncodingException",e);
}
}
public static byte[] toBytes(String from) {
if (from==null) return null;
try {
return from.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new ParserException("conversion to byte array failed",e);
}
}
public static String toShorterString(String from, int length){
return (from==null || from.length()<=length) ? from : from.substring(0, length);
}
}