Java Common validation Methods helper classes

Source: Internet
Author: User


Import java.text.ParseException;
Import java.util.Collection;
Import Java.util.Map;

/**
* Common validation Method helper classes, providing validation of strings, collections, arrays, numeric values *
*
*/
public class Validatehelper {
/**
* Determine if a string is not an empty string
*
* @param s to judge the string
* @return Returns TRUE if not NULL, otherwise false
*/
public static Boolean isnotempty (String s) {
Return ((s! = null) && s.length () > 0);
}

/**
* Determine if a string is an empty string
*
* @param s to judge the string
* @return Returns TRUE if NULL, otherwise false
*/
public static Boolean isEmpty (String s) {
Return ((s = = null) | | (s.length () = = 0));
}

/**
* Determine if a collection of collection types is not an empty collection
*
* @param c to judge the set
* @return Returns TRUE if not NULL, otherwise false
*/
public static Boolean Isnotempty (Collection c) {
Return ((c! = null) && (c.size () > 0));
}

/**
* Determines whether a collection of collection types is an empty collection
*
* @param c to judge the set
* @return Returns TRUE if NULL, otherwise false
*/
public static Boolean IsEmpty (Collection c) {
Return ((c = = null) | | (c.size () = = 0));
}

/**
* Determine if a collection of map types is not an empty collection
*
* @param c to judge the set
* @return Returns TRUE if not NULL, otherwise false
*/
public static Boolean Isnotempty (Map m) {
Return ((M! = null) && (m.size () > 0));
}

/**
* Determine if a collection of map types is an empty collection
*
* @param c to judge the set
* @return Returns TRUE if NULL, otherwise false
*/
public static Boolean IsEmpty (Map m) {
Return ((M = = null) | | (m.size () = = 0));
}

/**
* Determine if an array of type int is not an empty array
*
* @param c to determine the array
* @return Returns TRUE if not NULL, otherwise false
*/
public static Boolean Isnotempty (int[] i) {
Return ((i! = null) && (i.length > 0));
}

/**
* Determine if an array of type int is an empty array
*
* @param c to determine the array
* @return Returns TRUE if NULL, otherwise false
*/
public static Boolean IsEmpty (int[] i) {
return ((i = = null) | | (I.length = = 0));
}

/**
* Determine if an array of type string is not an empty array
*
* @param c to determine the array
* @return Returns TRUE if not NULL, otherwise false
*/
public static Boolean Isnotempty (string[] s) {
Return ((s! = null) && (s.length > 0));
}

/**
* Determines whether an array of type string is an empty array
*
* @param c to determine the array
* @return Returns TRUE if NULL, otherwise false
*/
public static Boolean IsEmpty (string[] s) {
Return ((s = = null) | | (s.length = = 0));
}

/**
* Verifies whether a string is a double type
*
* @param s to validate the string
* @return Returns True if the type is double, otherwise false
*/
public static Boolean isdouble (String s) {
if (s = = NULL | | s.equals (""))
return false;
String num = "0123456789.";
if (S.indexof ('. ') >= 0)
if (S.indexof ('. '), S.indexof ('. ') + 1) > 0)
return false;
for (int i = 0; i < s.length (); i++) {
if (Num.indexof (S.charat (i)) < 0) {
return false;
} else {
try {
Double.parsedouble (s);
} catch (NumberFormatException e) {
return false;
}
}
}
return true;
}

/**
* Verify whether a string is a float type
*
* @param s to validate the string
* @return Returns True if it is a float type, otherwise false
*/
public static Boolean isfloat (String s) {
if (s = = NULL | | s.equals (""))
return false;
String num = "0123456789.";
if (S.indexof ('. ') >= 0)
if (S.indexof ('. '), S.indexof ('. ') + 1) > 0)
return false;
for (int i = 0; i < s.length (); i++) {
if (Num.indexof (S.charat (i)) < 0) {
return false;
} else {
try {
Float.parsefloat (s);
} catch (NumberFormatException e) {
return false;
}
}
}
return true;
}

/**
* Verifies whether a string is shaped
*
* @param s to validate the string
* @return Returns True if it is an integer, otherwise false
*/
public static Boolean Isinteger (String s) {
if (s = = NULL | | s.length () = = 0) {
return false;
} else {
String str = "0123456789";
String num = "0123456789";
if (Num.indexof (S.charat (0)) < 0)
return false;
for (int i = 1; i < s.length (); i++) {
if (Str.indexof (S.charat (i)) < 0) {
return false;
} else {
try {
Integer.parseint (s);
} catch (NumberFormatException e) {
return false;
}
}
}
}
return true;
}

/**
* Verify whether a string is a. And a set of numbers
*
* @param s number string to pass in
* @return Returns True if it is a long type number, otherwise false
*/
public static Boolean Islongnumber (String s) {
if (s = = NULL | | s.equals (""))
return false;
String num = "0123456789.";
if (S.indexof ('. ') >= 0)
if (S.indexof ('. '), S.indexof ('. ') + 1) > 0)
return false;
for (int i = 0; i < s.length (); i++) {
if (Num.indexof (S.charat (i)) < 0)
return false;
}
return true;
}

/**
* Verify whether a string is a numeric component
*
* @param s to validate the string
* @return Returns True if the string is a number, otherwise false
*/
public static Boolean isnumber (String s) {
if (s = = NULL | | s.equals (""))
return false;
String num = "0123456789";
for (int i = 0; i < s.length (); i++) {
if (Num.indexof (S.charat (i)) < 0)
return false;
}
return true;
}

/**
* Verify that a string is a valid date, date format: YYYY-MM-DD
*
* @param date of the string to validate
* @return Returns True if the string is a valid date, otherwise false
*/
public static Boolean isDate (String date) {
Java.text.SimpleDateFormat df = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
try {
Df.setlenient (FALSE);
Df.parse (date);
return true;
} catch (ParseException e) {
return false;
}
}

/**
* Verify that a string is a valid datetime, DateTime format: YYYY-MM-DD HH:mm:ss
*
* @param date and time of the string to validate
* @return Returns True if the string is a valid datetime, otherwise false
*/
public static Boolean Istimestamp (String time) {
Java.text.SimpleDateFormat df = new Java.text.SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
try {
Df.setlenient (FALSE);
Df.parse (time);
return true;
} catch (ParseException e) {
return false;
}
}

/**
* Calculates string values based on the start and end lengths specified by the byte array
*
* @param bytes byte array
* @param begin Index
* @param End Index
* @return the converted string result
*/
public static String getString (byte[] bytes, int begin, int end) throws RuntimeException {
byte[] newbytes = new Byte[end-begin];
for (int i = begin, j = 0; i < end; i++, J + +) {
byte C = bytes[i];
NEWBYTES[J] = c;
}
Return (new String (newbytes));
}

/**
* Calculates the byte length of a string based on the start and end lengths specified by the byte array
*
* @param bytes byte array
* @param begin Index
* @param End Index
* @return The converted string length
*/
public static int getlength (byte[] bytes, int begin, int end) {
byte[] newbytes = new Byte[end-begin];
try {
for (int i = begin, j = 0; i < end; i++, J + +) {
byte B = bytes[i];
NEWBYTES[J] = b;
}
} catch (RuntimeException ex) {
Ex.printstacktrace ();
}
return (newbytes.length);
}

}

Java Common validation Methods helper classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.