From 340a8b28f9c9b6bbe1ef0d4d23308ae43aad0390 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 19 Jul 2011 15:54:36 +0000 Subject: [PATCH] ConvertiblePair implements equals and hashCode (SPR-8459) --- .../convert/converter/GenericConverter.java | 26 ++++++++++++++++--- .../GenericConversionServiceTests.java | 26 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java index 52da8c3ca0..84c93c8b99 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,11 @@ package org.springframework.core.convert.converter; -import java.util.Set; - import org.springframework.core.convert.TypeDescriptor; import org.springframework.util.Assert; +import java.util.Set; + /** * Generic converter interface for converting between two or more types. * @@ -87,6 +87,24 @@ public interface GenericConverter { public Class getTargetType() { return this.targetType; } - } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || obj.getClass() != ConvertiblePair.class) { + return false; + } + ConvertiblePair other = (ConvertiblePair) obj; + return this.sourceType.equals(other.sourceType) && this.targetType.equals(other.targetType); + + } + + @Override + public int hashCode() { + return this.sourceType.hashCode() * 31 + this.targetType.hashCode(); + } + } } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index d941ec832e..cb06f31350 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -19,10 +19,12 @@ package org.springframework.core.convert.support; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import org.junit.Test; @@ -30,6 +32,7 @@ import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.io.DescriptiveResource; import org.springframework.core.io.Resource; import org.springframework.util.StopWatch; @@ -378,6 +381,29 @@ public class GenericConversionServiceTests { public static Map map; + @Test + public void testConvertiblePairsInSet() throws Exception { + Set set = new HashSet(); + set.add(new GenericConverter.ConvertiblePair(Number.class, String.class)); + assert set.contains(new GenericConverter.ConvertiblePair(Number.class, String.class)); + } + + @Test + public void testConvertiblePairEqualsAndHash() throws Exception { + GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); + GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class); + assertEquals(pair, pairEqual); + assertEquals(pair.hashCode(), pairEqual.hashCode()); + } + + @Test + public void testConvertiblePairDifferentEqualsAndHash() throws Exception { + GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); + GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class); + assertFalse(pair.equals(pairOpposite)); + assertFalse(pair.hashCode() == pairOpposite.hashCode()); + } + private interface MyBaseInterface {