I've scenario to validate given input param is empty or not, I've list of fields with datatype string and date - productId,productName,productType,productRating,productSellDate and productReturnDate
I want to check these input params are null
or empty
or blank
for each field and if any one of the field is empty
or blank
or null
- it should throw NullPointerException
with field name.
Please find my code below - since I'm new to Java please apologize for the coding standard. I don't know how to get the field name which has empty
or null
when I throw NullPointerException
And I'm calling validate
and validateDate
for each string, is there any option to validate all these param in one go?
Please suggest if there is any better way of writing the below piece of code. Appreciated your help in advance! Thanks.
import java.util.Date;
public class Test {
public static void main(String[] args) {
String productId = "";
String productName = "Apple";
String productType = "Electronics";
String productRating = "Good";
Date productSellDate = new Date();
Date productReturnDate = new Date();
System.out.println(validate(productId));
System.out.println(validate(productName));
System.out.println(validate(productType));
System.out.println(validate(productRating));
System.out.println(validateDate(productSellDate));
System.out.println(validateDate(productReturnDate));
}
private static String validate(String s) {
if (s.isBlank() || s.isEmpty()) {
throw new NullPointerException("input param is empty"); // how to get the string field name which is empty or null or blank
} else {
return "valid";
}
}
private static String validateDate(Date d) {
if (d == null) {
throw new NullPointerException("sell or return date is empty"); // how to get the date field name which is empty or null or blank
} else {
return "date is valid";
}
}
}
CodePudding user response:
You can write it like this to take any number of input and throw if any one fo them are null or blank or empty
private static String validate(String... strings) {
for (String s : strings) {
if (s == null || s.isBlank() || s.isEmpty()) {
throw new NullPointerException("input param is empty");
}
}
return "valid";
}
and you can call it with any number of inputs
validate("1", "2", null, "");
CodePudding user response:
You may consider it just a minor improvement over the solution that you have got. It certainly isn’t a one-shot validation of all the variables. My idea is to do validation and assignment of each in the same line. For example:
String productId = validate("", "productId");
String productName = validate("Apple", "productName");
Result:
Exception in thread "main" java.lang.NullPointerException: productId
The message mentions that the productId
is blank/empty.
This obviously requires validate()
to return the validated object in the case where it doesn’t throw.
private static String validate(String s, String message) {
if (s == null || s.isBlank()) {
throw new NullPointerException(message);
} else {
return s;
}
}
We don’t need isEmpty()
as an explicit condition since isBlank()
also returns true for the completely empty string. Whether you want to check for null
, you know best yourself.
For non-String variables simply use the built-in Objects.requireNonNull()
:
LocalDate productSellDate = Objects.requireNonNull(
LocalDate.now(ZoneId.systemDefault()), "productSellDate");
LocalDate productReturnDate = Objects.requireNonNull(
LocalDate.now(ZoneId.systemDefault()), "productReturnDate");
LocalDate
is a class from java.time, the modern Java date and time API that I recommend for all of your date work. A LocalDate
is a date without time of day. If you need the time too, find some other class from the same API.