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

org.jvnet.jaxb2_commons.lang.JAXBHashCodeStrategy Maven / Gradle / Ivy

Go to download

This is an empty jar to override org.jvnet.jaxb2_commons:jaxb2-basics-runtime versions below v2 when you have both org.jvnet.jaxb2_commons:jaxb2-basics-runtime and org.jvnet.jaxb:jaxb2-basics-runtime artifacts in the same application. See https://github.com/highsource/jaxb-tools/issues/465

There is a newer version: 2.0.12
Show newest version
package org.jvnet.jaxb2_commons.lang;

import static org.jvnet.jaxb2_commons.locator.util.LocatorUtils.item;
import static org.jvnet.jaxb2_commons.locator.util.LocatorUtils.property;

import java.util.List;

import javax.xml.bind.JAXBElement;

import org.jvnet.jaxb2_commons.locator.ObjectLocator;

public class JAXBHashCodeStrategy extends DefaultHashCodeStrategy {

	public JAXBHashCodeStrategy() {
		super();
	}

	public JAXBHashCodeStrategy(int multiplierNonZeroOddNumber) {
		super(multiplierNonZeroOddNumber);
	}

	protected int hashCodeInternal(ObjectLocator locator, int hashCode,
			Object value) {
		if (value instanceof JAXBElement) {
			final JAXBElement element = (JAXBElement) value;
			return hashCodeInternal(locator, hashCode, element);
		} else if (value instanceof List) {
			final List list = (List) value;
			return hashCodeInternal(locator, hashCode, list);
		} else {
			return super.hashCodeInternal(locator, hashCode, value);
		}
	}

	protected int hashCodeInternal(ObjectLocator locator, int hashCode,
			final JAXBElement element) {
		int currentHashCode = hashCode;
		currentHashCode = hashCode(
				property(locator, "name", element.getName()), currentHashCode,
				element.getName());
		currentHashCode = hashCode(
				property(locator, "declaredType", element.getDeclaredType()),
				currentHashCode, element.getDeclaredType());
		currentHashCode = hashCode(
				property(locator, "scope", element.getScope()),
				currentHashCode, element.getScope());
		currentHashCode = hashCode(
				property(locator, "value", element.getValue()),
				currentHashCode, element.getValue());
		return currentHashCode;
	}

	protected int hashCodeInternal(ObjectLocator locator, int hashCode,
			final List list) {
		// Treat empty lists as nulls
		if (list.isEmpty()) {
			return super.hashCode(locator, hashCode, (Object) null);
		} else {
			int currentHashCode = hashCode;
			for (int index = 0; index < list.size(); index++) {
				final Object item = list.get(index);
				currentHashCode = hashCode(item(locator, index, item),
						currentHashCode, item);
			}
			return currentHashCode;
		}
	}

	public static JAXBHashCodeStrategy INSTANCE = new JAXBHashCodeStrategy();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy