Implementing java.util.EnumMap

This commit is contained in:
Alexey Andreev 2017-10-10 23:57:31 +03:00 committed by Alexey Andreev
parent 19a003669b
commit 0b985b95d1
2 changed files with 498 additions and 0 deletions

View File

@ -0,0 +1,264 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
public class TEnumMap<K extends Enum<K>, V> extends AbstractMap<K, V> implements Serializable, Cloneable {
private Class<K> keyType;
private Object[] data;
private boolean[] provided;
private int size;
private Set<Entry<K, V>> entrySet;
public TEnumMap(Class<K> keyType) {
initFromKeyType(keyType);
}
public TEnumMap(TEnumMap<K, V> m) {
initFromOtherEnumMap(m);
}
public TEnumMap(Map<K, V> m) {
if (m instanceof TEnumMap) {
initFromOtherEnumMap((TEnumMap<K, V>) m);
} else {
if (m.isEmpty()) {
throw new IllegalArgumentException();
}
initFromKeyType(m.keySet().iterator().next().getDeclaringClass());
for (Entry<K, V> entry : m.entrySet()) {
int index = entry.getKey().ordinal();
provided[index] = true;
data[index] = entry.getValue();
}
size = m.size();
}
}
private void initFromKeyType(Class<K> keyType) {
this.keyType = keyType;
this.data = new Object[TGenericEnumSet.getConstants(keyType).length];
this.provided = new boolean[data.length];
}
private void initFromOtherEnumMap(TEnumMap<K, V> m) {
this.keyType = m.keyType;
this.data = Arrays.copyOf(m.data, m.data.length);
this.provided = Arrays.copyOf(m.provided, m.provided.length);
this.size = m.size;
}
@Override
public int size() {
return size;
}
@Override
public boolean containsValue(Object value) {
for (int i = 0; i < data.length; ++i) {
if (provided[i] && Objects.equals(value, data[i])) {
return true;
}
}
return false;
}
@Override
public boolean containsKey(Object key) {
if (!keyType.isInstance(key)) {
return false;
}
int index = ((Enum<?>) key).ordinal();
return provided[index];
}
@Override
public V get(Object key) {
if (!keyType.isInstance(key)) {
return null;
}
int index = ((Enum<?>) key).ordinal();
@SuppressWarnings("unchecked")
V value = (V) data[index];
return value;
}
@Override
public V put(K key, V value) {
int index = key.ordinal();
@SuppressWarnings("unchecked")
V old = (V) data[index];
if (!provided[index]) {
provided[index] = true;
size++;
}
data[index] = value;
return old;
}
@Override
public V remove(Object key) {
if (!keyType.isInstance(key)) {
return null;
}
int index = ((Enum<?>) key).ordinal();
@SuppressWarnings("unchecked")
V old = (V) data[index];
if (provided[index]) {
provided[index] = false;
data[index] = null;
size--;
}
return old;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
int index = entry.getKey().ordinal();
if (!provided[index]) {
provided[index] = true;
size++;
}
data[index] = entry.getValue();
}
}
@Override
public void clear() {
if (size > 0) {
size = 0;
Arrays.fill(provided, false);
Arrays.fill(data, null);
}
}
@Override
public Set<Entry<K, V>> entrySet() {
if (entrySet == null) {
entrySet = new AbstractSet<Entry<K, V>>() {
@Override
public Iterator<Entry<K, V>> iterator() {
return new Iterator<Entry<K, V>>() {
int index;
int removeIndex = -1;
{
find();
}
@Override
public boolean hasNext() {
return index < data.length;
}
@Override
public Entry<K, V> next() {
if (index >= data.length) {
throw new NoSuchElementException();
}
removeIndex = index;
EntryImpl result = new EntryImpl(index++);
find();
return result;
}
private void find() {
while (index < provided.length && !provided[index]) {
index++;
}
}
@Override
public void remove() {
if (removeIndex < 0) {
throw new IllegalStateException();
}
data[removeIndex] = null;
provided[removeIndex] = false;
size--;
removeIndex = -1;
}
};
}
@Override
public int size() {
return size;
}
@Override
public boolean remove(Object o) {
if (!keyType.isInstance(o)) {
return false;
}
int index = ((Enum<?>) o).ordinal();
if (provided[index]) {
provided[index] = true;
data[index] = null;
size--;
return true;
} else {
return false;
}
}
@Override
public void clear() {
TEnumMap.this.clear();
}
class EntryImpl implements Entry<K, V> {
int index;
EntryImpl(int index) {
this.index = index;
}
@Override
@SuppressWarnings("unchecked")
public K getKey() {
return (K) TGenericEnumSet.getConstants(keyType)[index];
}
@Override
@SuppressWarnings("unchecked")
public V getValue() {
return (V) data[index];
}
@Override
public V setValue(V value) {
@SuppressWarnings("unchecked")
V old = (V) data[index];
data[index] = value;
return old;
}
}
};
}
return entrySet;
}
}

View File

@ -0,0 +1,234 @@
/*
* Copyright 2017 Alexey Andreev.
*
* 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 org.teavm.classlib.java.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner;
@RunWith(TeaVMTestRunner.class)
public class EnumMapTest {
@Test
public void emptyCreated() {
EnumMap<L, String> map = new EnumMap<>(L.class);
assertEquals(0, map.size());
assertFalse(map.entrySet().iterator().hasNext());
assertNull(map.get(L.A));
assertFalse(map.containsKey(L.A));
}
@Test
public void createdFromOtherEnumMap() {
EnumMap<L, String> map = new EnumMap<>(L.class);
assertNull(map.put(L.A, "A"));
EnumMap<L, String> otherMap = new EnumMap<>(map);
map.clear();
assertEquals(1, otherMap.size());
assertEquals("A", otherMap.get(L.A));
otherMap = new EnumMap<>((Map<L, String>) map);
assertEquals(0, otherMap.size());
assertEquals(null, otherMap.get(L.A));
}
@Test
public void createdFromOtherMap() {
Map<L, String> map = new HashMap<>();
assertNull(map.put(L.A, "A"));
EnumMap<L, String> otherMap = new EnumMap<>(map);
map.clear();
assertEquals(1, otherMap.size());
assertEquals("A", otherMap.get(L.A));
try {
new EnumMap<>(map);
fail("Should throw exception when creating from empty map");
} catch (IllegalArgumentException e) {
// expected
}
}
@Test
public void entriesAdded() {
EnumMap<L, String> map = new EnumMap<>(L.class);
assertNull(map.put(L.A, "A"));
assertEquals(1, map.size());
assertEquals("A", map.get(L.A));
assertTrue(map.containsKey(L.A));
assertEquals("A", map.put(L.A, "A0"));
assertEquals(1, map.size());
assertEquals("A0", map.get(L.A));
assertTrue(map.containsKey(L.A));
assertNull(map.put(L.B, "B"));
assertEquals(2, map.size());
assertEquals("B", map.get(L.B));
assertTrue(map.containsKey(L.B));
List<String> values = new ArrayList<>();
List<L> keys = new ArrayList<>();
for (Map.Entry<L, String> entry : map.entrySet()) {
values.add(entry.getValue());
keys.add(entry.getKey());
}
assertEquals(Arrays.asList("A0", "B"), values);
assertEquals(Arrays.asList(L.A, L.B), keys);
}
@Test
public void multipleEntriesAdded() {
EnumMap<L, String> map = new EnumMap<>(L.class);
map.put(L.A, "A");
map.put(L.B, "B");
Map<L, String> otherMap = new HashMap<>();
otherMap.put(L.B, "B0");
otherMap.put(L.C, "C0");
map.putAll(otherMap);
assertEquals(3, map.size());
assertEquals("A", map.get(L.A));
assertEquals("B0", map.get(L.B));
assertEquals("C0", map.get(L.C));
}
@Test
public void entriesRemoved() {
EnumMap<L, String> map = new EnumMap<>(L.class);
map.put(L.A, "A");
map.put(L.B, "B");
assertEquals(2, map.size());
assertEquals("A", map.remove(L.A));
assertEquals(1, map.size());
assertNull(map.get(L.A));
assertNull(map.remove(L.A));
assertEquals(1, map.size());
assertNull(map.remove("Dummy"));
List<String> values = new ArrayList<>();
List<L> keys = new ArrayList<>();
for (Map.Entry<L, String> entry : map.entrySet()) {
values.add(entry.getValue());
keys.add(entry.getKey());
}
assertEquals(Arrays.asList("B"), values);
assertEquals(Arrays.asList(L.B), keys);
}
@Test
public void containsNullValue() {
EnumMap<L, String> map = new EnumMap<>(L.class);
map.put(L.A, null);
assertEquals(1, map.size());
assertNull(map.get(L.A));
assertTrue(map.containsKey(L.A));
assertNull(map.values().iterator().next());
assertEquals(L.A, map.keySet().iterator().next());
}
@Test
public void clearWorks() {
EnumMap<L, String> map = new EnumMap<>(L.class);
map.put(L.A, "A");
map.put(L.B, "B");
assertEquals(2, map.size());
map.clear();
assertEquals(0, map.size());
assertFalse(map.entrySet().iterator().hasNext());
assertFalse(map.containsKey(L.A));
assertNull(map.get(L.A));
}
@Test
public void iteratorReplacesValue() {
EnumMap<L, String> map = new EnumMap<>(L.class);
map.put(L.A, "A");
map.put(L.B, "B");
Iterator<Map.Entry<L, String>> iter = map.entrySet().iterator();
assertTrue(iter.hasNext());
Map.Entry<L, String> entry = iter.next();
assertEquals(L.A, entry.getKey());
assertEquals("A", entry.setValue("A0"));
assertTrue(iter.hasNext());
entry = iter.next();
assertEquals(L.B, entry.getKey());
assertEquals("B", entry.setValue("B0"));
assertFalse(iter.hasNext());
assertEquals("A0", map.get(L.A));
assertEquals("B0", map.get(L.B));
}
@Test
public void iteratorRemovesValue() {
EnumMap<L, String> map = new EnumMap<>(L.class);
map.put(L.A, "A");
map.put(L.B, "B");
Iterator<Map.Entry<L, String>> iter = map.entrySet().iterator();
try {
iter.remove();
fail("Remove without calling next should throw exception");
} catch (IllegalStateException e) {
// It's expected
}
assertTrue(iter.hasNext());
Map.Entry<L, String> entry = iter.next();
assertEquals(L.A, entry.getKey());
iter.remove();
try {
iter.remove();
fail("Repeated remove should throw exception");
} catch (IllegalStateException e) {
// It's expected
}
assertTrue(iter.hasNext());
iter.next();
assertFalse(iter.hasNext());
assertEquals(null, map.get(L.A));
assertEquals("B", map.get(L.B));
assertEquals(1, map.size());
}
enum L {
A, B, C
}
}