com.lotaris.jee.validation.AbstractPatchTransferObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jee-validation Show documentation
Show all versions of jee-validation Show documentation
This library offers components that facilitate validation of arbitrary objects and how they are serialized towards a client.
It focuses on the wiring of the proposed patterns and does not contain any concrete validator implementations.
package com.lotaris.jee.validation;
import java.util.HashSet;
import java.util.Set;
/**
* {@link IPatchObject} implementation. Setters in subclasses should call the
* {@link #markPropertyAsSet(java.lang.String, java.lang.Object)} method.
*
*
* public class PersonTO extends AbstractPatchTransferObject {
*
* public static final String FIRST_NAME = "firstName";
* public static final String MIDDLE_NAME = "middleName";
* public static final String LAST_NAME = "lastName";
*
* @JsonProperty(FIRST_NAME)
* private String firstName;
* @JsonProperty(MIDDLE_NAME)
* private String middleName;
* @JsonProperty(LAST_NAME)
* private String lastName;
*
* public void setFirstName(String firstName) {
* this.firstName = markPropertyAsSet(FIRST_NAME, firstName);
* }
*
* public void setMiddleName(String middleName) {
* this.middleName = markPropertyAsSet(MIDDLE_NAME, middleName);
* }
*
* public void setFirstName(String lastName) {
* this.lastName = markPropertyAsSet(LAST_NAME, lastName);
* }
* }
*
* PersonTO person = new PersonTO();
* person.setFirstName("Bob");
* person.setMiddleName(null);
*
* assertTrue(person.isPropertySet(PersonTO.FIRST_NAME)); // first name was set
* assertTrue(person.isPropertySet(PersonTO.MIDDLE_NAME)); // middle name was set to null
* assertFalse(person.isPropertySet(PersonTO.LAST_NAME)); // last name was not set
*
*
* @author Simon Oulevay
*/
public abstract class AbstractPatchTransferObject implements IPatchObject {
private Set setProperties;
public AbstractPatchTransferObject() {
setProperties = new HashSet<>();
}
/**
* Marks the specified property as set and returns the value given as the second argument. This
* is meant to be used as a one-liner.
*
*
* public void setFirstName(String firstName) {
* this.firstName = markPropertyAsSet(FIRST_NAME, firstName);
* }
*
*
* @param the type of value
* @param property the property to mark as set
* @param value the value to return
* @return the value
*/
public T markPropertyAsSet(String property, T value) {
setProperties.add(property);
return value;
}
@Override
public boolean isPropertySet(String property) {
return setProperties.contains(property);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy