<commit_before>package org.jvnet.jaxb2_commons.plugin.simplehashcode.generator;

import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.lang3.Validate;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;

public class ListHashCodeCodeGenerator extends BlockHashCodeCodeGenerator {

	public ListHashCodeCodeGenerator(TypedHashCodeCodeGeneratorFactory factory,
			JCodeModel codeModel) {
		super(factory, codeModel);
	}

	@Override
	protected void generate(JBlock block, JVar currentHashCode, JType exposedType,
			Collection<JType> possibleTypes,
			JVar value) {
		Validate.isInstanceOf(JClass.class, exposedType);
		final JClass listType = (JClass) exposedType;

		final JClass elementType;
		if (listType.getTypeParameters().size() == 1) {
			elementType = listType.getTypeParameters().get(0);
		} else {
			elementType = getCodeModel().ref(Object.class);
		}

		final JVar iterator = block.decl(JMod.FINAL,
				getCodeModel().ref(Iterator.class).narrow(elementType), value.name() + "Iterator",
				value.invoke("iterator"));

		final JVar listHashCode = block.decl(JMod.NONE,
				getCodeModel().INT, 
				value.name() + "ListHashCode",
				 JExpr.lit(1));
		
		final JBlock elementBlock = block._while(iterator.invoke("hasNext"))
				.body();
		final JVar elementValue = elementBlock.decl(JMod.FINAL, elementType,
				value.name() + "Element", iterator.invoke("next"));

		final boolean isAlwaysSet = elementType.isPrimitive();
		final JExpression hasSetValue = isAlwaysSet ? JExpr.TRUE : elementValue
				.ne(JExpr._null());
		getFactory().getCodeGenerator(elementType).generate(elementBlock,
				listHashCode, elementType, possibleTypes, elementValue,
				hasSetValue, isAlwaysSet);
		block.assign(currentHashCode,
				currentHashCode.mul(JExpr.lit(getFactory().getMultiplier()))
						.plus(listHashCode));
	}
}<commit_msg>Issue #11. If the list does not contain arrays or JAXBElements, delegate to List.hashCode().<commit_after>package org.jvnet.jaxb2_commons.plugin.simplehashcode.generator;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.xml.bind.JAXBElement;

import org.apache.commons.lang3.Validate;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;

public class ListHashCodeCodeGenerator extends BlockHashCodeCodeGenerator {

	public ListHashCodeCodeGenerator(TypedHashCodeCodeGeneratorFactory factory,
			JCodeModel codeModel) {
		super(factory, codeModel);
	}

	@Override
	protected void generate(JBlock block, JVar currentHashCode,
			JType exposedType, Collection<JType> possibleTypes, JVar value) {
		Validate.isInstanceOf(JClass.class, exposedType);

		final JClass jaxbElementClass = getCodeModel().ref(JAXBElement.class);
		final Set<JType> arrays = new HashSet<JType>();
		final Collection<JClass> jaxbElements = new HashSet<JClass>();
		final Set<JType> otherTypes = new HashSet<JType>();
		for (final JType possibleType : possibleTypes) {
			if (possibleType.isArray()) {
				arrays.add(possibleType);
			} else if (possibleType instanceof JClass
					&& jaxbElementClass
							.isAssignableFrom(((JClass) possibleType).erasure())) {
				jaxbElements.add((JClass) possibleType);
			} else {
				otherTypes.add(possibleType);
			}
		}
		// If list items are not arrays or JAXBElements, just delegate to the
		// hashCode of the list
		if (arrays.isEmpty() && jaxbElements.isEmpty()) {
			block.assign(
					currentHashCode,
					currentHashCode
							.mul(JExpr.lit(getFactory().getMultiplier())).plus(
									value.invoke("hashCode")));

		} else {

			final JClass listType = (JClass) exposedType;

			final JClass elementType;
			if (listType.getTypeParameters().size() == 1) {
				elementType = listType.getTypeParameters().get(0);
			} else {
				elementType = getCodeModel().ref(Object.class);
			}

			final JVar iterator = block.decl(JMod.FINAL,
					getCodeModel().ref(Iterator.class).narrow(elementType),
					value.name() + "Iterator", value.invoke("iterator"));

			final JVar valueHashCode = block.decl(JMod.NONE, getCodeModel().INT,
					value.name() + "HashCode", JExpr.lit(1));

			final JBlock elementBlock = block
					._while(iterator.invoke("hasNext")).body();
			final JVar elementValue = elementBlock.decl(JMod.FINAL,
					elementType, value.name() + "Element",
					iterator.invoke("next"));

			final boolean isAlwaysSet = elementType.isPrimitive();
			final JExpression hasSetValue = isAlwaysSet ? JExpr.TRUE
					: elementValue.ne(JExpr._null());
			getFactory().getCodeGenerator(elementType).generate(elementBlock,
					valueHashCode, elementType, possibleTypes, elementValue,
					hasSetValue, isAlwaysSet);
			block.assign(
					currentHashCode,
					currentHashCode
							.mul(JExpr.lit(getFactory().getMultiplier())).plus(
									valueHashCode));
		}
	}
}
{'fraction_non_alphanumeric': 0.1203580270424681, 'fraction_numerical': 0.0022852789944772423, 'mean_word_length': 4.721132897603486, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7156319', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1641, 'n_words': 318}