BAEL-6600 - limit max size by LinkedHashMap and custom HashMap

This commit is contained in:
mcasari 2023-11-06 20:38:18 +01:00
parent 4d7e5eb5c9
commit e0c7ddb52e
3 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.baeldung.map;
import java.util.HashMap;
public class HashMapWithMaxSizeLimit<K, V> extends HashMap<K, V> {
private static final long serialVersionUID = 1L;
private int maxSize = -1;
public HashMapWithMaxSizeLimit(int maxSize) {
super();
this.maxSize = maxSize;
}
public V putWithLimit(K key, V value) throws Exception {
if (this.maxSize != - 1 && this.size() >= this.maxSize && !this.containsKey(key)) {
throw new Exception("Max size exceeded!");
}
return this.put(key, value);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.map;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class LimitMaxSizeHashMapByCustomHashMapUnitTest {
private final int MAX_SIZE = 4;
private LinkedHashMap<Integer, String> linkedHashMap;
@BeforeEach
void setUp() {
linkedHashMap = new LinkedHashMap<Integer, String>() {
private static final long serialVersionUID = 1L;
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
return size() > MAX_SIZE;
}
};
linkedHashMap.put(1, "One");
linkedHashMap.put(2, "Two");
linkedHashMap.put(3, "Three");
linkedHashMap.put(4, "Four");
}
@Test
void givenLinkedHashMapObject_whenAddingNewEntry_thenEldestEntryIsRemoved() {
linkedHashMap.put(5, "Five");
String[] expectedArrayAfterFive = { "Two", "Three", "Four", "Five" };
assertArrayEquals(expectedArrayAfterFive, linkedHashMap.values()
.toArray());
linkedHashMap.put(6, "Six");
String[] expectedArrayAfterSix = { "Three", "Four", "Five", "Six" };
assertArrayEquals(expectedArrayAfterSix, linkedHashMap.values()
.toArray());
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.map;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class LimitMaxSizeHashMapByLinkedHashMapUnitTest {
private final int MAX_SIZE = 4;
private HashMapWithMaxSizeLimit<Integer, String> hashMapWithMaxSizeLimit;
@BeforeEach
void setUp() {
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE);
}
@Test
void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() {
Exception exception = assertThrows(Exception.class, () -> {
hashMapWithMaxSizeLimit.putWithLimit(1, "One");
hashMapWithMaxSizeLimit.putWithLimit(2, "Two");
hashMapWithMaxSizeLimit.putWithLimit(3, "Three");
hashMapWithMaxSizeLimit.putWithLimit(4, "Four");
hashMapWithMaxSizeLimit.putWithLimit(5, "Five");
});
String messageThrownWhenSizeExceedsLimit = "Max size exceeded!";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit));
}
}