exchange.apexpro.connector.impl.InputChecker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apexpro-connector-java Show documentation
Show all versions of apexpro-connector-java Show documentation
A lightweight library to ApeX-Protocol
package exchange.apexpro.connector.impl;
import exchange.apexpro.connector.exception.ApexProApiException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class InputChecker {
private static final String regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\t";
private static final InputChecker checkerInst;
static {
checkerInst = new InputChecker();
}
static InputChecker checker() {
return checkerInst;
}
private boolean isSpecialChar(String str) {
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
InputChecker shouldNotNull(T value, String name) {
if (value == null) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + name + " should not be null");
}
return checkerInst;
}
InputChecker shouldNull(T value, String name) {
if (value != null) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + name + " should be null");
}
return checkerInst;
}
InputChecker checkSymbol(String symbol) {
if (symbol == null || "".equals(symbol)) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] Symbol is mandatory");
}
if (isSpecialChar(symbol)) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + symbol + " is invalid symbol");
}
return checkerInst;
}
InputChecker checkCurrency(String currency) {
if (currency == null || "".equals(currency)) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] Currency is mandatory");
}
if (isSpecialChar(currency)) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + currency + " is invalid currency");
}
return checkerInst;
}
InputChecker checkETF(String symbol) {
if (!"hb10".equals(symbol)) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"currently only support hb10 :-)");
}
return checkerInst;
}
private InputChecker checkRange(int size, int min, int max, String name) {
if (!(min <= size && size <= max)) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + name + " is out of bound. " + size + " is not in [" + min + "," + max + "]");
}
return checkerInst;
}
InputChecker checkSymbolList(List symbols) {
if (symbols == null || symbols.size() == 0) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR, "[Input] Symbol is mandatory");
}
for (String symbol : symbols) {
checkSymbol(symbol);
}
return checkerInst;
}
InputChecker checkRange(Integer size, int min, int max, String name) {
if (size != null) {
checkRange(size.intValue(), min, max, name);
}
return checkerInst;
}
InputChecker greaterOrEqual(Integer value, int base, String name) {
if (value != null && value < base) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + name + " should be greater than " + base);
}
return checkerInst;
}
InputChecker checkList(List list, int min, int max, String name) {
if (list != null) {
if (list.size() > max) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + name + " is out of bound, the max size is " + max);
} else if (list.size() < min) {
throw new ApexProApiException(ApexProApiException.INPUT_ERROR,
"[Input] " + name + " should contain " + min + " item(s) at least");
}
}
return checkerInst;
}
}