From ac8a31b82fc25936c0108b68a745f11a1bc111c3 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 4 Apr 2012 09:59:16 +0200 Subject: [PATCH] SmartFMap tweaks --- .../util/src/com/intellij/util/SmartFMap.java | 68 +++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/platform/util/src/com/intellij/util/SmartFMap.java b/platform/util/src/com/intellij/util/SmartFMap.java index b77fdd2df5ab..6736cf9a79e3 100644 --- a/platform/util/src/com/intellij/util/SmartFMap.java +++ b/platform/util/src/com/intellij/util/SmartFMap.java @@ -27,9 +27,9 @@ import java.util.*; * @author peter */ @SuppressWarnings("unchecked") -public class SmartFMap extends AbstractMap { +public class SmartFMap implements Map { private static final SmartFMap EMPTY = new SmartFMap(ArrayUtil.EMPTY_OBJECT_ARRAY); - private static final int ARRAY_THRESHOLD = 5; + private static final int ARRAY_THRESHOLD = 8; private final Object myMap; private SmartFMap(Object map) { @@ -78,6 +78,16 @@ public class SmartFMap extends AbstractMap { } THashMap newMap = new THashMap((Map)myMap); newMap.remove(key); + if (newMap.size() <= ARRAY_THRESHOLD) { + Object[] newArray = new Object[newMap.size() * 2]; + int i = 0; + for (K k : newMap.keySet()) { + newArray[i++] = k; + newArray[i++] = newMap.get(k); + } + return new SmartFMap(newArray); + } + return new SmartFMap(newMap); } @@ -130,6 +140,11 @@ public class SmartFMap extends AbstractMap { return false; } + @Override + public boolean containsValue(Object value) { + return false; + } + @Override @Nullable public V get(Object key) { @@ -166,6 +181,24 @@ public class SmartFMap extends AbstractMap { throw new UnsupportedOperationException(); } + @Override + public Set keySet() { + LinkedHashSet result = new LinkedHashSet(); + for (Entry entry : entrySet()) { + result.add(entry.getKey()); + } + return Collections.unmodifiableSet(result); + } + + @Override + public Collection values() { + ArrayList result = new ArrayList(); + for (Entry entry : entrySet()) { + result.add(entry.getValue()); + } + return Collections.unmodifiableCollection(result); + } + @Override @Deprecated public V remove(Object key) { @@ -180,20 +213,47 @@ public class SmartFMap extends AbstractMap { return ((Object[])myMap).length >> 1; } + @Override + public boolean isEmpty() { + return size() == 0; + } + @Override public Set> entrySet() { LinkedHashSet> set = new LinkedHashSet>(); if (myMap instanceof Map) { for (Entry entry : ((Map)myMap).entrySet()) { - set.add(new SimpleImmutableEntry(entry)); + set.add(new AbstractMap.SimpleImmutableEntry(entry)); } } else { Object[] array = (Object[])myMap; for (int i = 0; i < array.length; i += 2) { - set.add(new SimpleImmutableEntry((K)array[i], (V)array[i + 1])); + set.add(new AbstractMap.SimpleImmutableEntry((K)array[i], (V)array[i + 1])); } } return Collections.unmodifiableSet(set); } + // copied from AbstractMap + public String toString() { + Iterator> i = entrySet().iterator(); + if (! i.hasNext()) + return "{}"; + + StringBuilder sb = new StringBuilder(); + sb.append('{'); + while (true) { + Entry e = i.next(); + K key = e.getKey(); + V value = e.getValue(); + sb.append(key == this ? "(this Map)" : key); + sb.append('='); + sb.append(value == this ? "(this Map)" : value); + if (!i.hasNext()) { + return sb.append('}').toString(); + } + sb.append(", "); + } + } + }