Skip to content

Commit

Permalink
Fix remove and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Aug 26, 2017
1 parent 77fe113 commit 52193c2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
Expand Up @@ -12,25 +12,22 @@
import java.util.stream.Collectors;

/**
* A facade for another java.util.Map that overrides the equals and hashcode calculation of the added nodes
* by using another visitor for those methods.
* A map that overrides the equals and hashcode calculation of the added nodes
* by using another equals and hashcode visitor for those methods.
*/
public class VisitorMap<N extends Node, V> implements Map<N, V> {
// Cheat generics by removing them
private final Map<EqualsHashcodeOverridingFacade, V> innerMap;
private final Map<EqualsHashcodeOverridingFacade, V> innerMap = new HashMap<>();
private final GenericVisitor<Integer, Void> hashcodeVisitor;
private final GenericVisitor<Boolean, Visitable> equalsVisitor;

/**
* Wrap a map and use different visitors for equals and hashcode.
* Pass the visitors to use for equals and hashcode.
*/
public VisitorMap(GenericVisitor<Integer, Void> hashcodeVisitor, GenericVisitor<Boolean, Visitable> equalsVisitor) {
this.innerMap = new HashMap<>();
this.hashcodeVisitor = hashcodeVisitor;
this.equalsVisitor = equalsVisitor;
}



@Override
public int size() {
return innerMap.size();
Expand All @@ -53,18 +50,18 @@ public boolean containsValue(Object value) {

@Override
public V get(Object key) {
return (V) innerMap.get(new EqualsHashcodeOverridingFacade((N) key));
return innerMap.get(new EqualsHashcodeOverridingFacade((N) key));
}

@Override
public V put(N key, V value) {
return (V) innerMap.put(new EqualsHashcodeOverridingFacade(key), value);
return innerMap.put(new EqualsHashcodeOverridingFacade(key), value);
}

private class EqualsHashcodeOverridingFacade implements Visitable {
private final N overridden;

public EqualsHashcodeOverridingFacade(N overridden) {
EqualsHashcodeOverridingFacade(N overridden) {
this.overridden = overridden;
}

Expand Down Expand Up @@ -94,12 +91,12 @@ public boolean equals(final Object obj) {

@Override
public V remove(Object key) {
return (V) innerMap.remove(key);
return innerMap.remove(new EqualsHashcodeOverridingFacade((N) key));
}

@Override
public void putAll(Map<? extends N, ? extends V> m) {
m.forEach((key, value) -> this.put(key,value));
m.forEach(this::put);
}

@Override
Expand All @@ -109,7 +106,7 @@ public void clear() {

@Override
public Set<N> keySet() {
return ((Map<EqualsHashcodeOverridingFacade, V>) innerMap).keySet().stream()
return innerMap.keySet().stream()
.map(k -> k.overridden)
.collect(Collectors.toSet());
}
Expand All @@ -121,8 +118,8 @@ public Collection<V> values() {

@Override
public Set<Entry<N, V>> entrySet() {
return ((Map<EqualsHashcodeOverridingFacade, V>) innerMap).entrySet().stream()
.map(e -> new HashMap.SimpleEntry<N, V>(e.getKey().overridden, e.getValue()))
return innerMap.entrySet().stream()
.map(e -> new HashMap.SimpleEntry<>(e.getKey().overridden, e.getValue()))
.collect(Collectors.toSet());
}
}
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class VisitorMapTest {
Expand All @@ -18,52 +19,62 @@ public void normalEqualsDoesDeepCompare() {
CompilationUnit x1 = JavaParser.parse("class X{}");
CompilationUnit x2 = JavaParser.parse("class X{}");

Map<CompilationUnit, Integer> normalMap = new HashMap<>();
normalMap.put(x1, 1);
normalMap.put(x2, 2);
assertEquals(1, normalMap.size());
Map<CompilationUnit, Integer> map = new HashMap<>();
map.put(x1, 1);
map.put(x2, 2);
assertEquals(1, map.size());
}

@Test
public void objectIdentityEqualsDoesShallowCompare() {
CompilationUnit x1 = JavaParser.parse("class X{}");
CompilationUnit x2 = JavaParser.parse("class X{}");

Map<CompilationUnit, Integer> normalMap = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
normalMap.put(x1, 1);
normalMap.put(x2, 2);
assertEquals(2, normalMap.size());
Map<CompilationUnit, Integer> map = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
map.put(x1, 1);
map.put(x2, 2);
assertEquals(2, map.size());
}

@Test
public void visitorMapGet(){
CompilationUnit x1 = JavaParser.parse("class X{}");

Map<CompilationUnit, Integer> normalMap = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
normalMap.put(x1, 1);
assertEquals(1, (int)normalMap.get(x1));
Map<CompilationUnit, Integer> map = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
map.put(x1, 1);
assertEquals(1, (int)map.get(x1));
}

@Test
public void visitorMapContainsKey(){
CompilationUnit x1 = JavaParser.parse("class X{}");

Map<CompilationUnit, Integer> normalMap = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
normalMap.put(x1, 1);
assertTrue(normalMap.containsKey(x1));
Map<CompilationUnit, Integer> map = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
map.put(x1, 1);
assertTrue(map.containsKey(x1));
}

@Test
public void visitorMapPutAll(){
CompilationUnit x1 = JavaParser.parse("class X{}");
CompilationUnit x2 = JavaParser.parse("class Y{}");
Map<CompilationUnit, Integer> normalMap = new HashMap<>();
normalMap.put(x1, 1);
normalMap.put(x2, 2);
Map<CompilationUnit, Integer> map = new HashMap<>();
map.put(x1, 1);
map.put(x2, 2);
Map<CompilationUnit, Integer> visitorMap = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
visitorMap.putAll(normalMap);
visitorMap.putAll(map);
assertEquals(2, visitorMap.size());
}


@Test
public void remove(){
CompilationUnit x1 = JavaParser.parse("class X{}");
VisitorMap<CompilationUnit, Integer> map = new VisitorMap<>(new ObjectIdentityHashCodeVisitor(), new ObjectIdentityEqualsVisitor());
map.put(x1, 1);
assertTrue(map.containsKey(x1));

map.remove(x1);

assertFalse(map.containsKey(x1));
}
}

0 comments on commit 52193c2

Please sign in to comment.