Java tutorial
/* * commons-ddd is a set of Java classes for use in Domain Driven Design. * Copyright (C) 2013 Christian Kalkhoff <softmetz@fsfe.org> * * This file is part of commons-ddd. * * commons-ddd is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * commons-ddd is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with commons-ddd. If not, see <http://www.gnu.org/licenses/>. */ package de.softmetz.commons.ddd.shared.vo; import java.lang.reflect.ParameterizedType; import javax.persistence.MappedSuperclass; import javax.persistence.Transient; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; /** * Abstract base class for value objects that have more than one attribute. * * See {@link AbstractSingleValueObject} for a base class that is optimized for a single attribute. * * @param <VO> Type of the concrete implementation class * @author softmetz */ @MappedSuperclass public abstract class AbstractCompoundValueObject<VO extends AbstractCompoundValueObject> extends AbstractValueObject<VO> { @Transient private final Class<VO> _CONCRETE_CLASS; /** * Constructor for this value object. * * Provided for convenience to create known values. * */ protected AbstractCompoundValueObject() { _CONCRETE_CLASS = (Class<VO>) ((ParameterizedType) getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; } @Override public boolean sameValueAs(VO other) { if (other == null) { return false; } return EqualsBuilder.reflectionEquals(this, other, false, _CONCRETE_CLASS); } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (!getClass().isAssignableFrom(obj.getClass())) { return false; } return sameValueAs((VO) obj); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(7, 31, (VO) this, false, _CONCRETE_CLASS); } @Override public String toString() { return ToStringBuilder.reflectionToString((VO) this, ToStringStyle.DEFAULT_STYLE, false, _CONCRETE_CLASS); } }