com.anaptecs.spring.base.ParentClass Maven / Gradle / Ivy
/*
* anaptecs GmbH, Ricarda-Huch-Str. 71, 72760 Reutlingen, Germany
*
* Copyright 2004 - 2019. All rights reserved.
*/
package com.anaptecs.spring.base;
import java.util.Objects;
import com.anaptecs.annotations.MyNotNullProperty;
import com.anaptecs.jeaf.validation.api.spring.SpringValidationExecutor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType", visible = true)
@JsonSubTypes({ @JsonSubTypes.Type(value = ChildA.class, name = "ChildA"),
@JsonSubTypes.Type(value = ChildAA.class, name = "ChildAA"),
@JsonSubTypes.Type(value = ChildB.class, name = "ChildB"),
@JsonSubTypes.Type(value = ChildBB.class, name = "ChildBB") })
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
setterVisibility = JsonAutoDetect.Visibility.NONE)
public class ParentClass {
/**
* Constant for the name of attribute "parentAttribute".
*/
public static final String PARENTATTRIBUTE = "parentAttribute";
private String parentAttribute;
/**
* 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 ParentClass( ) {
}
/**
* Initialize object using the passed builder.
*
* @param pBuilder Builder that should be used to initialize this object. The parameter must not be null.
*/
protected ParentClass( Builder pBuilder ) {
// Read attribute values from builder.
parentAttribute = pBuilder.parentAttribute;
}
/**
* Method returns a new builder.
*
* @return {@link Builder} New builder that can be used to create new ParentClass objects.
*/
public static Builder builder( ) {
return new Builder();
}
/**
* Convenience method to create new instance of class ParentClass.
*
*
* @param pParentAttribute Value to which {@link #parentAttribute} should be set.
*
* @return {@link com.anaptecs.spring.base.ParentClass}
*/
public static ParentClass of( String pParentAttribute ) {
ParentClass.Builder lBuilder = ParentClass.builder();
lBuilder.setParentAttribute(pParentAttribute);
return lBuilder.build();
}
/**
* Class implements builder to create a new instance of class ParentClass
.
*/
public static class Builder {
private String parentAttribute;
/**
* Use {@link ParentClass#builder()} instead of private constructor to create new builder.
*/
protected Builder( ) {
}
/**
* Use {@link ParentClass#builder(ParentClass)} instead of private constructor to create new builder.
*/
protected Builder( ParentClass pObject ) {
if (pObject != null) {
// Read attribute values from passed object.
this.setParentAttribute(pObject.parentAttribute);
}
}
/**
* Method returns a new builder.
*
* @return {@link Builder} New builder that can be used to create new ParentClass objects.
*/
public static Builder newBuilder( ) {
return new Builder();
}
/**
* Method creates a new builder and initialize it with the data from the passed object.
*
* @param pObject Object that should be used to initialize the builder. The parameter may be null.
* @return {@link Builder} New builder that can be used to create new ParentClass objects. The method never returns
* null.
*/
public static Builder newBuilder( ParentClass pObject ) {
return new Builder(pObject);
}
/**
* Method sets attribute {@link #parentAttribute}.
*
* @param pParentAttribute Value to which {@link #parentAttribute} should be set.
* @return {@link Builder} Instance of this builder to support chaining setters. Method never returns null.
*/
public Builder setParentAttribute( @MyNotNullProperty String pParentAttribute ) {
// Assign value to attribute
parentAttribute = pParentAttribute;
return this;
}
/**
* Method creates a new instance of class ParentClass. The object will be initialized with the values of the
* builder.
*
* @return ParentClass Created object. The method never returns null.
*/
public ParentClass build( ) {
ParentClass lObject = new ParentClass(this);
SpringValidationExecutor.getValidationExecutor().validateObject(lObject);
return lObject;
}
}
/**
* Method returns attribute {@link #parentAttribute}.
*
* @return {@link String} Value to which {@link #parentAttribute} is set.
*/
@MyNotNullProperty
public String getParentAttribute( ) {
return parentAttribute;
}
/**
* Method sets attribute {@link #parentAttribute}.
*
* @param pParentAttribute Value to which {@link #parentAttribute} should be set.
*/
public void setParentAttribute( @MyNotNullProperty String pParentAttribute ) {
// Assign value to attribute
parentAttribute = pParentAttribute;
}
@Override
public int hashCode( ) {
final int lPrime = 31;
int lResult = 1;
lResult = lPrime * lResult + Objects.hashCode(parentAttribute);
return lResult;
}
@Override
public boolean equals( Object pObject ) {
boolean lEquals;
if (this == pObject) {
lEquals = true;
}
else if (pObject == null) {
lEquals = false;
}
else if (this.getClass() != pObject.getClass()) {
lEquals = false;
}
else {
ParentClass lOther = (ParentClass) pObject;
lEquals = Objects.equals(parentAttribute, lOther.parentAttribute);
}
return lEquals;
}
/**
* 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("parentAttribute: ");
lBuilder.append(parentAttribute);
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();
}
/**
* Method creates a new builder and initializes it with the data of this object.
*
* @return {@link Builder} New builder that can be used to create new ParentClass objects. The method never returns
* null.
*/
public Builder toBuilder( ) {
return new Builder(this);
}
}