Skip to content

Commit

Permalink
Fix MapEntry implementation that was not compatible with java Map.Entry
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-costigliola committed Jan 31, 2016
1 parent de90083 commit 3118bf0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
28 changes: 12 additions & 16 deletions src/main/java/org/assertj/core/data/MapEntry.java
Expand Up @@ -12,19 +12,19 @@
*/
package org.assertj.core.data;

import static org.assertj.core.util.Objects.*;
import static org.assertj.core.presentation.DefaultToString.toStringOf;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;

import java.util.Map;
import java.util.Objects;

/**
* Understands an entry in a <code>{@link Map}</code>.
*
* @author Yvonne Wang
*/
public class MapEntry<K, V> implements Map.Entry<K, V>{
public class MapEntry<K, V> implements Map.Entry<K, V> {

public final K key;
public final V value;

Expand All @@ -35,7 +35,7 @@ public class MapEntry<K, V> implements Map.Entry<K, V>{
* @param value the value of the entry to create.
* @return the created {@code MapEntry}.
*/
public static <K,V> MapEntry<K, V> entry(K key, V value) {
public static <K, V> MapEntry<K, V> entry(K key, V value) {
return new MapEntry<>(key, value);
}

Expand All @@ -45,20 +45,16 @@ private MapEntry(K key, V value) {
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof MapEntry)) return false;
@SuppressWarnings("rawtypes")
MapEntry other = (MapEntry) obj;
return areEqual(key, other.key) && areEqual(value, other.value);
public boolean equals(Object object) {
if (!(object instanceof Map.Entry)) return false;
Map.Entry<?, ?> that = (Map.Entry<?, ?>) object;
return Objects.equals(this.getKey(), that.getKey())
&& Objects.equals(this.getValue(), that.getValue());
}

@Override
public int hashCode() {
int result = 1;
result = HASH_CODE_PRIME * result + hashCodeFor(key);
result = HASH_CODE_PRIME * result + hashCodeFor(value);
return result;
return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
}

@Override
Expand All @@ -75,7 +71,7 @@ public K getKey() {
public V getValue() {
return value;
}

/**
* Always throws <tt>UnsupportedOperationException</tt>,
* as this class represents an <i>immutable</i> map entry.
Expand All @@ -86,6 +82,6 @@ public V getValue() {
*/
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
throw new UnsupportedOperationException();
}
}
Expand Up @@ -12,13 +12,18 @@
*/
package org.assertj.core.data;

import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
import static org.assertj.core.test.EqualsHashCodeContractAssert.*;
import static org.assertj.core.test.EqualsHashCodeContractAssert.assertEqualsIsReflexive;
import static org.assertj.core.test.EqualsHashCodeContractAssert.assertEqualsIsSymmetric;
import static org.assertj.core.test.EqualsHashCodeContractAssert.assertEqualsIsTransitive;
import static org.assertj.core.test.EqualsHashCodeContractAssert.assertMaintainsEqualsAndHashCodeContract;

import java.util.Map.Entry;

import org.assertj.core.data.MapEntry;
import org.junit.*;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* Tests for {@link MapEntry#equals(Object)} and {@link MapEntry#hashCode()}.
Expand All @@ -27,10 +32,12 @@
*/
public class MapEntry_equals_hashCode_Test {
private static MapEntry<String, String> entry;
private static Entry<String, String> javaEntry;

@BeforeClass
public static void setUpOnce() {
entry = entry("key", "value");
javaEntry = singletonMap("key", "value").entrySet().iterator().next();
}

@Test
Expand Down Expand Up @@ -67,4 +74,20 @@ public void should_not_be_equal_to_null() {
public void should_not_be_equal_to_MapEntry_with_different_value() {
assertThat(entry.equals(entry("key0", "value0"))).isFalse();
}

@Test
public void should_have_symmetric_equals_with_java_MapEntry() {
assertEqualsIsSymmetric(javaEntry, entry);
}

@Test
public void should_maintain_equals_and_hashCode_contract_with_java_MapEntry() {
assertMaintainsEqualsAndHashCodeContract(javaEntry, entry);
}

@Test
public void should_have_transitive_equals_with_java_MapEntry() {
Entry<String, String> javaEntry2 = singletonMap("key", "value").entrySet().iterator().next();
assertEqualsIsTransitive(entry, javaEntry, javaEntry2);
}
}

1 comment on commit 3118bf0

@joel-costigliola
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I cast the other object to a Map.Entry only getters are accessible.

Whatever goes in 2.x will be 3.x, as 3.x is 2.x + java 8 specific features.
Oh and 3.3 is already out ;-) so 2.4/3.4 it will be.

Please sign in to comment.