blob: 0ed99ecfdaffe8ad05a016fe2dce89c8fe6ab035 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.common.hash;
import org.eclipse.osbp.runtime.common.annotations.FindIdHelper;
import com.google.common.hash.Hashing;
// TODO: Auto-generated Javadoc
/**
* Common HashUtil.
*/
public class HashUtil {
/**
* Creates a hash code for an object with the class name and objects
* hashCode.
* <p>
* <i>org.eclipse.osbp.dtos.ItemDTO(id=17):</i>
* <code>hash("org.eclipse.osbp.dtos.ItemDto({object.hashCode()})")</code>
*
* @param clazz
* the clazz
* @param object
* the object
* @return the string
*/
public static String createObjectWithIdHash(Class<?> clazz, Object object) {
return createObjectWithIdHash(clazz.getCanonicalName(), object);
}
/**
* Creates a hash code for an object with the class name and objects
* hashCode.
* <p>
* <i>org.eclipse.osbp.dtos.ItemDTO(id=17):</i>
* <code>hash("org.eclipse.osbp.dtos.ItemDto({object.hashCode()})")</code>
*
* @param objectName
* the object name
* @param object
* the object
* @return the string
*/
public static String createObjectWithIdHash(String objectName, Object object) {
Object id = FindIdHelper.getIdValue(object);
if (id instanceof Number) {
if (((Number) id).longValue() == 0) {
id = object.hashCode();
}
} else if (id == null) {
id = object.hashCode();
}
return Hashing.sha1().hashBytes(String.format("%s(%s)", objectName, String.valueOf(id)).getBytes()).toString();
}
}