All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.openpojo.business.identity.impl.DefaultHashCodeGenerator Maven / Gradle / Ivy

Go to download

This project was born out of a need to validate all POJOs (Plain Old Java Object) are behaving correctly. This project has two main aspects to it: * Make Testing as easy as possible. * Simplifying identity management (hashCode / equals) using annotation.

There is a newer version: 0.7.3
Show newest version
/*
 * Copyright (c) 2010-2013 Osman Shoukry
 *
 *    This program 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 any
 *    later version.
 *
 *    This program 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 Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see .
 */

package com.openpojo.business.identity.impl;

import com.openpojo.business.annotation.BusinessKey;
import com.openpojo.business.exception.BusinessException;
import com.openpojo.business.identity.HashCodeGenerator;
import com.openpojo.business.utils.BusinessIdentityUtils;
import com.openpojo.business.utils.BusinessPojoHelper;
import com.openpojo.reflection.PojoField;

/**
 * This class is the default implementation for hash code generation.
 * The algorithm is simple and conforms to the java spec for hash code generation.
 * 
* Please note that as per spec, two equal hash codes don't mean that objects are equal, * while two un-equal hash codes DO mean that the objects are not equal. *
* * @author oshoukry */ public class DefaultHashCodeGenerator implements HashCodeGenerator { private DefaultHashCodeGenerator() { } public static HashCodeGenerator getInstance() { return Instance.INSTANCE; } public int doGenerate(final Object object) { if (object == null) { throw BusinessException.getInstance("null parameter passed object=[null]"); } final int prime = 31; int result = 1; for (PojoField pojoField : BusinessPojoHelper.getBusinessKeyFields(object.getClass())) { BusinessKey businessKey = pojoField.getAnnotation(BusinessKey.class); result = prime * result + BusinessIdentityUtils.getHashCode(pojoField, object, businessKey.caseSensitive()); } return result; } private static class Instance { static final HashCodeGenerator INSTANCE = new DefaultHashCodeGenerator(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy