Validation.java [src/csip/utils] Revision: Date:
/*
* $Id:$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2022, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;
/**
* Generic validation methods..
*
* @author od
*/
public class Validation {
static final double CONUS_NORTH = 49.3457868; // CONUS_NORTH lat
static final double CONUS_WEST = -124.7844079; // CONUS_WEST long
static final double CONUS_EAST = -66.9513812; // CONUS_EAST long
static final double CONUS_SOUTH = 24.7433195; // CONUS_SOUTH lat
static final int MAX_COKEY = 99999999; // Cokeys aren't longer than 8 digits
static final int MAX_MUKEY = 9999999; // Mukeys aren't longer than 7 digits
/**
* checks for the lat/lon to be in CONUS.
*
* @param lat latitude
* @param lon longitude
* @throws IllegalArgumentException if outside.
*/
public static void checkCONUS(double lat, double lon) {
if ((CONUS_SOUTH <= lat) && (lat <= CONUS_NORTH)
&& (CONUS_WEST <= lon) && (lon <= CONUS_EAST))
return;
throw new IllegalArgumentException("Invalid CONUS coordinates: " + lat + "/" + lon);
}
/**
* Check for valid Cokey.
*
* @param cokey the Cokey number as string, e.g. "12856507"
*/
public static void checkCokey(String cokey) {
if (cokey == null || cokey.isEmpty())
throw new IllegalArgumentException("No Cokey value:");
try {
checkCokey(Integer.valueOf(cokey));
} catch (NumberFormatException E) {
throw new IllegalArgumentException("Illegal Cokey value: " + cokey);
}
}
/**
* Check for valid Cokey.
*
* @param cokey the Cokey as number.
* @throws IllegalArgumentException if invalid
*/
public static void checkCokey(Number cokey) {
if (cokey == null)
throw new IllegalArgumentException("No Cokey value:");
int val = cokey.intValue();
if (val >= 1 && val <= MAX_COKEY)
return;
throw new IllegalArgumentException("Invalid Cokey: " + cokey);
}
/**
* Check for valid CMZ.
*
* @param cmz the CMZ number as string, e.g. "12' or "4.1", no CMZ prefix
* @throws IllegalArgumentException 74 PB 73 HI 72 AK 04.1 37.1 38.1 75 PR
* 15.1
*
*/
public static void checkCMZ(String cmz) {
if (cmz == null || cmz.isEmpty())
throw new IllegalArgumentException("No CMZ value:");
cmz = cmz.trim();
if (cmz.indexOf(' ') > 0) {
if (cmz.equals("72 AK") || cmz.equals("73 HI")
|| cmz.equals("74 PB") || cmz.equals("75 PR"))
return;
}
try {
checkCMZ(Double.valueOf(cmz));
} catch (NumberFormatException E) {
throw new IllegalArgumentException("Illegal CMZ value: " + cmz);
}
}
/**
* Check for valid CMZ.
*
* @param cmz the CMZ as number.
* @throws IllegalArgumentException if invalid
*/
public static void checkCMZ(Number cmz) {
if (cmz == null)
throw new IllegalArgumentException("No CMZ value:");
float val = cmz.floatValue();
if (val == 4.1f || val == 15.1f || val == 37.1f || val == 38.1f)
return;
if (val >= 1 && val <= 71 && (val == (int) val))
return;
throw new IllegalArgumentException("Invalid CMZ: " + cmz);
}
/**
* Check for valid Mukey.
*
* @param mukey the Mukey number as string, e.g. "2955448"
*/
public static void checkMukey(String mukey) {
if (mukey == null || mukey.isEmpty())
throw new IllegalArgumentException("No Mukey value:");
try {
checkCokey(Integer.valueOf(mukey));
} catch (NumberFormatException E) {
throw new IllegalArgumentException("Illegal Mukey value: " + mukey);
}
}
/**
* Check for valid Mukey.
*
* @param mukey the Mukey as number.
* @throws IllegalArgumentException if invalid
*/
public static void checkMukey(Number mukey) {
if (mukey == null)
throw new IllegalArgumentException("No Mukey value:");
int val = mukey.intValue();
if (val >= 1 && val <= MAX_MUKEY)
return;
throw new IllegalArgumentException("Invalid Mukey: " + mukey);
}
public static void checkString(String actual, String... expected) {
for (String exp : expected) {
if (actual.equals(exp))
return;
}
throw new IllegalArgumentException("expected " + Arrays.toString(expected)
+ ", but given: " + actual);
}
public static void checkStringIgnoreCase(String actual, String... expected) {
for (String exp : expected) {
if (actual.equalsIgnoreCase(exp))
return;
}
throw new IllegalArgumentException("expected " + Arrays.toString(expected)
+ ", but given: " + actual);
}
public static String digest(String s, String algorithm) {
try {
return DatatypeConverter.printHexBinary(MessageDigest.getInstance(algorithm)
.digest(s.getBytes("UTF-8"))).toLowerCase();
} catch (NoSuchAlgorithmException | UnsupportedEncodingException E) {
return null;
}
}
}