Skip to content

Commit

Permalink
perf[cow]: CopyOnWriteHashMap equals and hashcode
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 2, 2024
1 parent 7ae1971 commit 0c0dc62
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion orm/src/test/java/com/zfoo/orm/accessor/MapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void insert(long id) {
bagMap.put("bag3", bagItem3);
bagMap.put("bag4", null);

var map = new HashMap<String, Map<String, String>>();
var map = new CopyOnWriteHashMap<String, Map<String, String>>();
map.put("a", Map.of("b", "b"));
entity.setBaseMap(map);

Expand Down
6 changes: 3 additions & 3 deletions orm/src/test/java/com/zfoo/orm/entity/bag/MapEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MapEntity implements IEntity<Long> {
private CopyOnWriteHashMap<Long, CopyOnWriteHashMap<Integer,Integer>> copyOnWriteHashMap = new CopyOnWriteHashMap<>();
private Map<String, BagItem> bagMap = new HashMap<>();

private Map<String, Map<String, String>> baseMap = new HashMap<>();
private CopyOnWriteHashMap<String, Map<String, String>> baseMap = new CopyOnWriteHashMap<>();

private Map<Long, String> longStringMap = new HashMap<>();
private Map<Integer, String> intStringMap = new HashMap<>();
Expand Down Expand Up @@ -101,11 +101,11 @@ public void setBagMap(Map<String, BagItem> bagMap) {
this.bagMap = bagMap;
}

public Map<String, Map<String, String>> getBaseMap() {
public CopyOnWriteHashMap<String, Map<String, String>> getBaseMap() {
return baseMap;
}

public void setBaseMap(Map<String, Map<String, String>> baseMap) {
public void setBaseMap(CopyOnWriteHashMap<String, Map<String, String>> baseMap) {
this.baseMap = baseMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,12 @@ public List<E> subList(int fromIndex, int toIndex) {

@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}

if (this != o) {
return false;
lock.lock();
try {
return list.equals(o);
} finally {
lock.unlock();
}

return list.equals(o);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,14 @@ public Collection<V> values() {
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}

@Override
public boolean equals(Object o) {
return map.equals(o);
}

@Override
public int hashCode() {
return map.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,14 @@ public Collection<V> values() {
public Set<Entry<Long, V>> entrySet() {
return map.entrySet();
}

@Override
public boolean equals(Object o) {
return map.equals(o);
}

@Override
public int hashCode() {
return map.hashCode();
}
}

0 comments on commit 0c0dc62

Please sign in to comment.