public interface Validatable
Validatable interface should be implemented by classes that require
validation of their state before being written through a method writer.
Implementing this interface indicates that the object is capable of self-validation, which is essential in contexts like serialization or communication where the integrity and correctness of an object's state are crucial.
Example usage:
public class MyData implements Validatable {
private String name;
private Integer age;
// getters and setters
@Override
public void validate() throws InvalidMarshallableException {
if (name == null || name.isEmpty()) {
throw new InvalidMarshallableException("Name cannot be null or empty");
}
if (age == null || age < 0) {
throw new InvalidMarshallableException("Age cannot be null or negative");
}
}
}
| Modifier and Type | Method and Description |
|---|---|
void |
validate()
Validates the state of the object.
|
void validate()
throws InvalidMarshallableException
This method should be called prior to writing the object via a method writer.
Implementations should check the state of the object and throw an
InvalidMarshallableException if the object is in an invalid state.
For example, this could involve checking for null values in required fields, validating that numerical values are within acceptable ranges, etc.
InvalidMarshallableException - if the object is in an invalid state,
such as having null values in required fields or values
out of acceptable range.RuntimeException - if an unexpected error occurs during validation.Copyright © 2024. All rights reserved.