All Downloads are FREE. Search and download functionalities are using the official Maven repository.

exchange.apexpro.connector.impl.InputChecker Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy