Cleaning Data in Java
Dennis Lee
Software Engineer
Salesperson | Country | Product | Date | Amount | Boxes Shipped |
---|---|---|---|---|---|
James Rudeforth | UK | Mint Chip Choco | 4-Jan-22 | $5320 | 100 |
Van Tuxwell | India | 85% Dark Bars | 1-Aug-22 | $7896 | 94 |
Gigi Bohling | US | Peanut Butter Cubes | 7-Jul-22 | $4501 | 91 |
import jakarta.validation.constraints.NotNull; // Import rules for empty fields import jakarta.validation.constraints.Size; // Import rules for string length public class ChocolateSale { @NotNull(message = "Salesperson cannot be empty") private String salesperson;
@NotNull(message = "Country cannot be empty") // Multiple rules can be stacked @Size(min = 2, max = 50) // Enforces country name length private String country;
@NotNull(message = "Product cannot be empty") private String product; }
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
// We will show message outputs later public class ChocolateSale { @Min(value = 0, message = "Sales amount must be positive") private Double amount;
@Min(value = 1, message = "Must ship at least 1 box") @Max(value = 1000, message = "Cannot ship more than 1000 boxes") private Integer boxesShipped; }
// Stores unique validation errors (no duplicates)
import java.util.Set;
// Holds details about a single validation error (field, message, etc.)
import jakarta.validation.ConstraintViolation;
// Entry point for creating validators
import jakarta.validation.Validation;
// Checks data against rules
import jakarta.validation.Validator;
// Creates configured validators
import jakarta.validation.ValidatorFactory;
// Thrown when validation fails
import jakarta.validation.ConstraintViolationException;
class SalesValidator { // Create tools for checking our data private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
// Get a validator to check sales records private static final Validator validator = factory.getValidator();
public static Set<ConstraintViolation<ChocolateSale>> validateSale(ChocolateSale sale) { // Check sale record and return any problems found return validator.validate(sale); } }
public class Main { public static void main(String[] args) { // Create sale with some invalid data (null country, negative amount) ChocolateSale sale = new ChocolateSale("James Rudeforth", null, "Mint Chip Choco", LocalDate.parse("2022-01-04"), -5320.0, 1500);
// Check sale for validation violations Set<ConstraintViolation<ChocolateSale>> violations = SalesValidator.validateSale(sale); // Print each validation error message violations.forEach(violation -> System.out.println(violation.getMessage()));
// If any violations found, throw exception if (!violations.isEmpty()) throw new ConstraintViolationException(violations); } }
Country cannot be empty Sales amount must be positive Cannot ship more than 1000 boxes
Exception in thread "main" jakarta.validation.ConstraintViolationException
@NotNull
, @Size
, @Min
, and @Max
catch errors earlyCleaning Data in Java