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

com.payu.sdk.api.util.ParametersUtils Maven / Gradle / Ivy

The newest version!
package com.payu.sdk.api.util;

import com.payu.sdk.api.exceptions.InvalidParametersException;
import java.util.Map;

public final class ParametersUtils {

  private ParametersUtils() {
  }

  public static void validateParameters(Map parameters, String... required)
      throws InvalidParametersException {
    StringBuilder errorMessage = new StringBuilder();
    boolean isError = false;

    if (parameters == null || parameters.isEmpty()) {
      throw new InvalidParametersException("Parameters can not be null or empty.");
    } else {
      for (String r : required) {
        if (!parameters.keySet().contains(r) || parameters.get(r) == null || parameters.get(r)
            .trim()
            .isEmpty()) {

          errorMessage.append("Parameter [").append(r).append("] is required.").append("\n");
          isError = true;
        }
      }
    }

    if (isError) {
      throw new InvalidParametersException(errorMessage.toString());
    }
  }

  public static Integer getIntegerParameter(Map parameters, String paramName)
      throws InvalidParametersException {
    String parameter = getParameter(parameters, paramName);
    if (parameter != null && parameter.trim().isEmpty()) {
      parameter = null;
    }

    return (parameter != null ? getInteger(parameter, paramName) : null);
  }

  public static String getParameter(Map parameters, String paramName) {
    String parameter = parameters.get(paramName);
    if (parameter != null && parameter.trim().isEmpty()) {
      parameter = null;
    }

    return parameter;
  }

  private static Integer getInteger(String value, String name) throws InvalidParametersException {
    Integer integerValue;
    try {
      integerValue = Integer.parseInt(value);
    } catch (Exception e) {
      throw new InvalidParametersException(
          String.format("The parameter [%s] isn't a valid integer value", name), e);
    }

    return integerValue;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy