Add the ability to get keys and values from KeyFMap

This commit is contained in:
Zalim Bashorov
2015-04-29 12:40:15 +03:00
parent 9bb470daac
commit f06a33f802
7 changed files with 129 additions and 0 deletions
@@ -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;
}
}
@@ -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 "<empty>";
@@ -36,6 +36,9 @@ public interface KeyFMap {
@Nullable
<V> V get(@NotNull Key<V> key);
@NotNull
Key[] getKeys();
String toString();
boolean isEmpty();
@@ -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<Object> implements KeyFMap {
private MapBackedFMap(@NotNull MapBackedFMap oldMap, final int exclude) {
super(oldMap.size());
@@ -86,6 +88,12 @@ class MapBackedFMap extends TIntObjectHashMap<Object> implements KeyFMap {
return (V)get(key.hashCode());
}
@NotNull
@Override
public Key[] getKeys() {
return getKeysByIndices(keys());
}
@Override
public String toString() {
final StringBuilder s = new StringBuilder();
@@ -46,6 +46,12 @@ public class OneElementFMap<V> implements KeyFMap {
return myKey == key ? (V)myValue : null;
}
@NotNull
@Override
public Key[] getKeys() {
return new Key[] { myKey };
}
@Override
public String toString() {
return "<" + myKey + " -> " + myValue+">";
@@ -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 + ")";
@@ -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);
}
}