Skip to content

Commit

Permalink
Create SlicedMapImpl#collectiveSliceKeys on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
bashor committed May 21, 2015
1 parent 492404b commit d8b1856
Showing 1 changed file with 8 additions and 1 deletion.
Expand Up @@ -26,6 +26,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

public class SlicedMapImpl implements MutableSlicedMap {
Expand All @@ -35,7 +36,7 @@ public static SlicedMapImpl create() {
}

private final Map<Object, UserDataHolderImpl> map = new THashMap<Object, UserDataHolderImpl>(0);
private final Multimap<WritableSlice<?, ?>, Object> collectiveSliceKeys = ArrayListMultimap.create();
private Multimap<WritableSlice<?, ?>, Object> collectiveSliceKeys = null;

@Override
public <K, V> void put(WritableSlice<K, V> slice, K key, V value) {
Expand Down Expand Up @@ -63,6 +64,10 @@ public <K, V> void put(WritableSlice<K, V> slice, K key, V value) {
}

if (slice.isCollective()) {
if (collectiveSliceKeys == null) {
collectiveSliceKeys = ArrayListMultimap.create();
}

collectiveSliceKeys.put(slice, key);
}

Expand All @@ -88,6 +93,8 @@ public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
@SuppressWarnings("unchecked")
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
assert slice.isCollective() : "Keys are not collected for slice " + slice;

if (collectiveSliceKeys == null) return Collections.emptyList();
return (Collection<K>) collectiveSliceKeys.get(slice);
}

Expand Down

0 comments on commit d8b1856

Please sign in to comment.