com.anaptecs.jeaf.junit.rest.generics.AbstractResponse Maven / Gradle / Ivy
The newest version!
/*
* anaptecs GmbH, Ricarda-Huch-Str. 71, 72760 Reutlingen, Germany
*
* Copyright 2004 - 2021. All rights reserved.
*/
package com.anaptecs.jeaf.junit.rest.generics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import javax.validation.Valid;
import com.anaptecs.jeaf.core.api.ServiceObject;
import com.anaptecs.jeaf.xfun.api.checks.Check;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.Nulls;
@Valid
@JsonIgnoreProperties(value = "objectType")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType", visible = true)
@JsonSubTypes({ @JsonSubTypes.Type(value = GenericPageableResponse.class, name = "GenericPageableResponse"),
@JsonSubTypes.Type(value = GenericSingleValuedReponse.class, name = "GenericSingleValuedReponse") })
public abstract class AbstractResponse implements ServiceObject {
/**
* Default serial version uid.
*/
private static final long serialVersionUID = 1L;
@JsonSetter(nulls = Nulls.SKIP)
private List errors;
@JsonSetter(nulls = Nulls.SKIP)
private List warnings;
/**
* Default constructor is only intended to be used for deserialization by tools like Jackson for JSON. For "normal"
* object creation builder should be used instead.
*/
protected AbstractResponse( ) {
errors = new ArrayList();
warnings = new ArrayList();
}
/**
* Initialize object using the passed builder.
*
* @param pBuilder Builder that should be used to initialize this object. The parameter must not be null.
*/
protected AbstractResponse( Builder pBuilder ) {
// Ensure that builder is not null.
Check.checkInvalidParameterNull(pBuilder, "pBuilder");
// Read attribute values from builder.
if (pBuilder.errors != null) {
errors = pBuilder.errors;
}
else {
errors = new ArrayList();
}
if (pBuilder.warnings != null) {
warnings = pBuilder.warnings;
}
else {
warnings = new ArrayList();
}
}
/**
* Class implements builder to create a new instance of class AbstractResponse
.
*/
public static abstract class Builder {
private List errors;
private List warnings;
/**
* Use {@link AbstractResponse#builder()} instead of private constructor to create new builder.
*/
protected Builder( ) {
}
/**
* Use {@link AbstractResponse#builder(AbstractResponse)} instead of private constructor to create new builder.
*/
protected Builder( AbstractResponse pObject ) {
if (pObject != null) {
// Read attribute values from passed object.
this.setErrors(pObject.errors);
this.setWarnings(pObject.warnings);
}
}
/**
* Method sets association {@link #errors}.
*
* @param pErrors Collection to which {@link #errors} should be set.
* @return {@link Builder} Instance of this builder to support chaining setters. Method never returns null.
*/
public Builder setErrors( List pErrors ) {
// To ensure immutability we have to copy the content of the passed collection.
if (pErrors != null) {
errors = new ArrayList(pErrors);
}
else {
errors = null;
}
return this;
}
/**
* Method adds the passed objects to association {@link #errors}.
*
* @param pErrors Array of objects that should be added to {@link #errors}. The parameter may be null.
* @return {@link Builder} Instance of this builder to support chaining. Method never returns null.
*/
public Builder addToErrors( Message... pErrors ) {
if (pErrors != null) {
if (errors == null) {
errors = new ArrayList();
}
errors.addAll(Arrays.asList(pErrors));
}
return this;
}
/**
* Method sets association {@link #warnings}.
*
* @param pWarnings Collection to which {@link #warnings} should be set.
* @return {@link Builder} Instance of this builder to support chaining setters. Method never returns null.
*/
public Builder setWarnings( List pWarnings ) {
// To ensure immutability we have to copy the content of the passed collection.
if (pWarnings != null) {
warnings = new ArrayList(pWarnings);
}
else {
warnings = null;
}
return this;
}
/**
* Method adds the passed objects to association {@link #warnings}.
*
* @param pWarnings Array of objects that should be added to {@link #warnings}. The parameter may be null.
* @return {@link Builder} Instance of this builder to support chaining. Method never returns null.
*/
public Builder addToWarnings( Message... pWarnings ) {
if (pWarnings != null) {
if (warnings == null) {
warnings = new ArrayList();
}
warnings.addAll(Arrays.asList(pWarnings));
}
return this;
}
}
/**
* Method returns association {@link #errors}.
*
* @return {@link List} Value to which {@link #errors} is set. The method never returns null and the returned
* collection is modifiable.
*/
public List getErrors( ) {
// Return all Message objects directly without any protection against modification.
return errors;
}
/**
* Method adds the passed object to {@link #errors}.
*
* @param pErrors Object that should be added to {@link #errors}. The parameter must not be null.
*/
public void addToErrors( Message pErrors ) {
// Check parameter "pErrors" for invalid value null.
Check.checkInvalidParameterNull(pErrors, "pErrors");
// Add passed object to collection of associated Message objects.
errors.add(pErrors);
}
/**
* Method adds all passed objects to {@link #errors}.
*
* @param pErrors Collection with all objects that should be added to {@link #errors}. The parameter must not be null.
*/
public void addToErrors( Collection pErrors ) {
// Check parameter "pErrors" for invalid value null.
Check.checkInvalidParameterNull(pErrors, "pErrors");
// Add all passed objects.
for (Message lNextObject : pErrors) {
this.addToErrors(lNextObject);
}
}
/**
* Method removes the passed object from {@link #errors}.
*
* @param pErrors Object that should be removed from {@link #errors}. The parameter must not be null.
*/
public void removeFromErrors( Message pErrors ) {
// Check parameter for invalid value null.
Check.checkInvalidParameterNull(pErrors, "pErrors");
// Remove passed object from collection of associated Message objects.
errors.remove(pErrors);
}
/**
* Method removes all objects from {@link #errors}.
*/
public void clearErrors( ) {
// Remove all objects from association "errors".
errors.clear();
}
/**
* Method returns association {@link #warnings}.
*
* @return {@link List} Value to which {@link #warnings} is set. The method never returns null and the
* returned collection is modifiable.
*/
public List getWarnings( ) {
// Return all Message objects directly without any protection against modification.
return warnings;
}
/**
* Method adds the passed object to {@link #warnings}.
*
* @param pWarnings Object that should be added to {@link #warnings}. The parameter must not be null.
*/
public void addToWarnings( Message pWarnings ) {
// Check parameter "pWarnings" for invalid value null.
Check.checkInvalidParameterNull(pWarnings, "pWarnings");
// Add passed object to collection of associated Message objects.
warnings.add(pWarnings);
}
/**
* Method adds all passed objects to {@link #warnings}.
*
* @param pWarnings Collection with all objects that should be added to {@link #warnings}. The parameter must not be
* null.
*/
public void addToWarnings( Collection pWarnings ) {
// Check parameter "pWarnings" for invalid value null.
Check.checkInvalidParameterNull(pWarnings, "pWarnings");
// Add all passed objects.
for (Message lNextObject : pWarnings) {
this.addToWarnings(lNextObject);
}
}
/**
* Method removes the passed object from {@link #warnings}.
*
* @param pWarnings Object that should be removed from {@link #warnings}. The parameter must not be null.
*/
public void removeFromWarnings( Message pWarnings ) {
// Check parameter for invalid value null.
Check.checkInvalidParameterNull(pWarnings, "pWarnings");
// Remove passed object from collection of associated Message objects.
warnings.remove(pWarnings);
}
/**
* Method removes all objects from {@link #warnings}.
*/
public void clearWarnings( ) {
// Remove all objects from association "warnings".
warnings.clear();
}
/**
* Method returns a StringBuilder that can be used to create a String representation of this object. The returned
* StringBuilder also takes care about attributes of super classes.
*
* @return {@link StringBuilder} StringBuilder representing this object. The method never returns null.
*/
public StringBuilder toStringBuilder( String pIndent ) {
StringBuilder lBuilder = new StringBuilder();
lBuilder.append(pIndent);
lBuilder.append(this.getClass().getName());
lBuilder.append(System.lineSeparator());
lBuilder.append(pIndent);
lBuilder.append("errors: ");
if (errors != null) {
lBuilder.append(errors.size());
lBuilder.append(" element(s)");
}
else {
lBuilder.append(" null");
}
lBuilder.append(System.lineSeparator());
if (errors != null) {
for (Message lNext : errors) {
lBuilder.append(lNext.toStringBuilder(pIndent + " "));
lBuilder.append(System.lineSeparator());
}
}
lBuilder.append(pIndent);
lBuilder.append("warnings: ");
if (warnings != null) {
lBuilder.append(warnings.size());
lBuilder.append(" element(s)");
}
else {
lBuilder.append(" null");
}
lBuilder.append(System.lineSeparator());
if (warnings != null) {
for (Message lNext : warnings) {
lBuilder.append(lNext.toStringBuilder(pIndent + " "));
lBuilder.append(System.lineSeparator());
}
}
return lBuilder;
}
/**
* Method creates a new String with the values of all attributes of this class. All references to other objects will
* be ignored.
*
* @return {@link String} String representation of this object. The method never returns null.
*/
@Override
public String toString( ) {
return this.toStringBuilder("").toString();
}
}