ConvertiblePair implements equals and hashCode (SPR-8459)

This commit is contained in:
Juergen Hoeller
2011-07-19 15:54:36 +00:00
parent 83c5753ae1
commit 340a8b28f9
2 changed files with 48 additions and 4 deletions

View File

@@ -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();
}
}
}

View File

@@ -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<String, Integer> map;
@Test
public void testConvertiblePairsInSet() throws Exception {
Set<GenericConverter.ConvertiblePair> set = new HashSet<GenericConverter.ConvertiblePair>();
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 {