
com.usefulmilk.support.domain.ValueObject Maven / Gradle / Ivy
/*
* Copyright 2016 UsefulMilk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.usefulmilk.support.domain;
/**
* According to Eric Evans, Value Object are objects that matter only as the
* combination of their attributes. Two value objects with the same values
* for all their attributes are considered equal.
* Implementations of {@link ValueObject} must implements {@link ValueObject#hashCode()}
* and {@link ValueObject#equals(Object)} in order to compare all attributes.
* If two different instances have all attributes equals, so these instances
* have to return the same identification when {@link ValueObject#getId()} is called.
*/
public abstract class ValueObject extends DomainObject {
/**
* It will generate a hashCode considering all attributes of this object.
* This implementation have to ensure that a set of attributes will always
* generate the same hashCode. To achieve it this implementation must not call
* the default implementation of {@link Object#hashCode()}, once it can produce
* different values in different process execution. For example,
* {@link Enum#hashCode()} can produces different hashCode each time the
* main process is restarted because its implementation don't override the
* default implementation of {@link Object#hashCode()}.
* @return The hashCode of this object.
*/
@Override
public abstract int hashCode();
/**
* Compare the parameter object with this object.
* If all attributes of this object are equals, regardless
* {@link ValueObject#getId()}, this class must return true otherwise
* it has to return false. The implementation of this method have to
* keep in mind that {@link ValueObject#getId()} must not be compared to
* determine if these objects are equals. The {@link ValueObject#getId()}
* has to return the same value only if all other attributes are equals.
* @param obj The object to be compared with this object.
* @return Return true if the objects are equals.
*/
@Override
public abstract boolean equals(Object obj);
}