diff --git a/platform/util/src/com/intellij/util/keyFMap/ArrayBackedFMap.java b/platform/util/src/com/intellij/util/keyFMap/ArrayBackedFMap.java index c924b7093871..5c243216bc86 100644 --- a/platform/util/src/com/intellij/util/keyFMap/ArrayBackedFMap.java +++ b/platform/util/src/com/intellij/util/keyFMap/ArrayBackedFMap.java @@ -140,7 +140,25 @@ public class ArrayBackedFMap implements KeyFMap { return keys; } + @NotNull + @Override + public Key[] getKeys() { + return getKeysByIndices(keys); + } + + @NotNull public Object[] getValues() { return values; } + + @NotNull + static Key[] getKeysByIndices(int[] indexes) { + Key[] result = new Key[indexes.length]; + + for (int i =0; i < indexes.length; i++) { + result[i] = Key.getKeyByIndex(indexes[i]); + } + + return result; + } } diff --git a/platform/util/src/com/intellij/util/keyFMap/EmptyFMap.java b/platform/util/src/com/intellij/util/keyFMap/EmptyFMap.java index a51068c18abd..b29c5cdc240f 100644 --- a/platform/util/src/com/intellij/util/keyFMap/EmptyFMap.java +++ b/platform/util/src/com/intellij/util/keyFMap/EmptyFMap.java @@ -19,6 +19,8 @@ import com.intellij.openapi.util.Key; import org.jetbrains.annotations.NotNull; class EmptyFMap implements KeyFMap { + private static final Key[] EMPTY_KEYS_ARRAY = {}; + EmptyFMap() { } @@ -39,6 +41,12 @@ class EmptyFMap implements KeyFMap { return null; } + @NotNull + @Override + public Key[] getKeys() { + return EMPTY_KEYS_ARRAY; + } + @Override public String toString() { return ""; diff --git a/platform/util/src/com/intellij/util/keyFMap/KeyFMap.java b/platform/util/src/com/intellij/util/keyFMap/KeyFMap.java index 8c32384c1992..18d5b1069899 100644 --- a/platform/util/src/com/intellij/util/keyFMap/KeyFMap.java +++ b/platform/util/src/com/intellij/util/keyFMap/KeyFMap.java @@ -36,6 +36,9 @@ public interface KeyFMap { @Nullable V get(@NotNull Key key); + @NotNull + Key[] getKeys(); + String toString(); boolean isEmpty(); diff --git a/platform/util/src/com/intellij/util/keyFMap/MapBackedFMap.java b/platform/util/src/com/intellij/util/keyFMap/MapBackedFMap.java index 4f9ba3e400bd..e2b4555ec928 100644 --- a/platform/util/src/com/intellij/util/keyFMap/MapBackedFMap.java +++ b/platform/util/src/com/intellij/util/keyFMap/MapBackedFMap.java @@ -21,6 +21,8 @@ import gnu.trove.TIntObjectHashMap; import gnu.trove.TIntObjectProcedure; import org.jetbrains.annotations.NotNull; +import static com.intellij.util.keyFMap.ArrayBackedFMap.getKeysByIndices; + class MapBackedFMap extends TIntObjectHashMap implements KeyFMap { private MapBackedFMap(@NotNull MapBackedFMap oldMap, final int exclude) { super(oldMap.size()); @@ -86,6 +88,12 @@ class MapBackedFMap extends TIntObjectHashMap implements KeyFMap { return (V)get(key.hashCode()); } + @NotNull + @Override + public Key[] getKeys() { + return getKeysByIndices(keys()); + } + @Override public String toString() { final StringBuilder s = new StringBuilder(); diff --git a/platform/util/src/com/intellij/util/keyFMap/OneElementFMap.java b/platform/util/src/com/intellij/util/keyFMap/OneElementFMap.java index 8bd2f343cb92..a38c5f0ed97d 100644 --- a/platform/util/src/com/intellij/util/keyFMap/OneElementFMap.java +++ b/platform/util/src/com/intellij/util/keyFMap/OneElementFMap.java @@ -46,6 +46,12 @@ public class OneElementFMap implements KeyFMap { return myKey == key ? (V)myValue : null; } + @NotNull + @Override + public Key[] getKeys() { + return new Key[] { myKey }; + } + @Override public String toString() { return "<" + myKey + " -> " + myValue+">"; diff --git a/platform/util/src/com/intellij/util/keyFMap/PairElementsFMap.java b/platform/util/src/com/intellij/util/keyFMap/PairElementsFMap.java index b6e5a21bd58e..ec1da5b4a59f 100644 --- a/platform/util/src/com/intellij/util/keyFMap/PairElementsFMap.java +++ b/platform/util/src/com/intellij/util/keyFMap/PairElementsFMap.java @@ -54,6 +54,12 @@ public class PairElementsFMap implements KeyFMap { return key == key1 ? (V)value1 : key == key2 ? (V)value2 : null; } + @NotNull + @Override + public Key[] getKeys() { + return new Key[] { key1, key2 }; + } + @Override public String toString() { return "Pair: (" + key1 + " -> " + value1 + "; " + key2 + " -> " + value2 + ")"; diff --git a/platform/util/testSrc/com/intellij/util/keyFMap/KeyFMapTest.java b/platform/util/testSrc/com/intellij/util/keyFMap/KeyFMapTest.java new file mode 100644 index 000000000000..44533f90c947 --- /dev/null +++ b/platform/util/testSrc/com/intellij/util/keyFMap/KeyFMapTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.keyFMap; + +import com.intellij.openapi.util.Key; +import com.intellij.util.ArrayUtil; +import junit.framework.TestCase; + +public class KeyFMapTest extends TestCase { + private final static Key[] KEYS = { + Key.create("K0"), + Key.create("K1"), + Key.create("K2"), + Key.create("K3"), + Key.create("K4"), + Key.create("K5"), + Key.create("K6"), + Key.create("K7"), + Key.create("K8"), + Key.create("K9") + }; + + private final static Object[] VALUES = { "V0", "V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9" }; + + private static KeyFMap createKeyFMap(Key[] keys, Object[] values, int size) { + KeyFMap fmap = KeyFMap.EMPTY_MAP; + + for (int i = 0; i < size; i++) { + fmap = fmap.plus(keys[i], values[i]); + } + + return fmap; + } + + private static void doTestGetKeys(int size) { + KeyFMap map = createKeyFMap(KEYS, VALUES, size); + + Key[] keys = map.getKeys(); + + assertEquals(size, keys.length); + + for (int i = 0; i < size; i++) { + int index = ArrayUtil.indexOf(keys, KEYS[i], 0, size); + assertTrue("Key" + " not found: " + KEYS[i], index >= 0); + } + } + + public void testGetKeysOnEmptyFMap() { + doTestGetKeys(0); + } + + public void testGetKeysOnOneElementFMap() { + doTestGetKeys(1); + } + + public void testGetKeysOnTwoElementFMap() { + doTestGetKeys(2); + } + + public void testGetKeysOnArrayBackedFMap() { + doTestGetKeys(5); + } + + public void testGetKeysOnMapBackedFMap() { + doTestGetKeys(10); + } +}