BAEL-6600: override put instead of additional method

This commit is contained in:
mcasari 2023-11-13 23:39:30 +01:00
parent e0c7ddb52e
commit 5ecbb1b7c8
3 changed files with 52 additions and 47 deletions

View File

@ -7,19 +7,21 @@ public class HashMapWithMaxSizeLimit<K, V> extends HashMap<K, V> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private int maxSize = -1; private int maxSize = -1;
public HashMapWithMaxSizeLimit(int maxSize) { public HashMapWithMaxSizeLimit(int maxSize) {
super(); super();
this.maxSize = maxSize; this.maxSize = maxSize;
} }
@Override
public V putWithLimit(K key, V value) throws Exception { public V put(K key, V value) {
if (this.maxSize != - 1 && this.size() >= this.maxSize && !this.containsKey(key)) { V res = null;
throw new Exception("Max size exceeded!"); if (this.maxSize == -1 || this.size() < this.maxSize) {
res = super.put(key, value);
} else if (this.maxSize != -1) {
throw new RuntimeException("Max size exceeded!");
} }
return this.put(key, value); return res;
} }
} }

View File

@ -1,8 +1,9 @@
package com.baeldung.map; package com.baeldung.map;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.LinkedHashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -11,33 +12,27 @@ import org.junit.jupiter.api.Test;
class LimitMaxSizeHashMapByCustomHashMapUnitTest { class LimitMaxSizeHashMapByCustomHashMapUnitTest {
private final int MAX_SIZE = 4; private final int MAX_SIZE = 4;
private LinkedHashMap<Integer, String> linkedHashMap; private HashMapWithMaxSizeLimit<Integer, String> hashMapWithMaxSizeLimit;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
linkedHashMap = new LinkedHashMap<Integer, String>() { hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE);
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 @Test
void givenLinkedHashMapObject_whenAddingNewEntry_thenEldestEntryIsRemoved() { void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() {
linkedHashMap.put(5, "Five"); Exception exception = assertThrows(RuntimeException.class, () -> {
String[] expectedArrayAfterFive = { "Two", "Three", "Four", "Five" }; hashMapWithMaxSizeLimit.put(1, "One");
assertArrayEquals(expectedArrayAfterFive, linkedHashMap.values() hashMapWithMaxSizeLimit.put(2, "Two");
.toArray()); hashMapWithMaxSizeLimit.put(3, "Three");
linkedHashMap.put(6, "Six"); hashMapWithMaxSizeLimit.put(4, "Four");
String[] expectedArrayAfterSix = { "Three", "Four", "Five", "Six" }; hashMapWithMaxSizeLimit.put(5, "Five");
assertArrayEquals(expectedArrayAfterSix, linkedHashMap.values() });
.toArray());
String messageThrownWhenSizeExceedsLimit = "Max size exceeded!";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit));
} }
} }

View File

@ -1,7 +1,9 @@
package com.baeldung.map; package com.baeldung.map;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -9,27 +11,33 @@ import org.junit.jupiter.api.Test;
class LimitMaxSizeHashMapByLinkedHashMapUnitTest { class LimitMaxSizeHashMapByLinkedHashMapUnitTest {
private final int MAX_SIZE = 4; private final int MAX_SIZE = 4;
private HashMapWithMaxSizeLimit<Integer, String> hashMapWithMaxSizeLimit; private LinkedHashMap<Integer, String> linkedHashMap;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE); 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 @Test
void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() { void givenLinkedHashMapObject_whenAddingNewEntry_thenEldestEntryIsRemoved() {
Exception exception = assertThrows(Exception.class, () -> { linkedHashMap.put(5, "Five");
hashMapWithMaxSizeLimit.putWithLimit(1, "One"); String[] expectedArrayAfterFive = { "Two", "Three", "Four", "Five" };
hashMapWithMaxSizeLimit.putWithLimit(2, "Two"); assertArrayEquals(expectedArrayAfterFive, linkedHashMap.values()
hashMapWithMaxSizeLimit.putWithLimit(3, "Three"); .toArray());
hashMapWithMaxSizeLimit.putWithLimit(4, "Four"); linkedHashMap.put(6, "Six");
hashMapWithMaxSizeLimit.putWithLimit(5, "Five"); String[] expectedArrayAfterSix = { "Three", "Four", "Five", "Six" };
}); assertArrayEquals(expectedArrayAfterSix, linkedHashMap.values()
.toArray());
String messageThrownWhenSizeExceedsLimit = "Max size exceeded!";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit));
} }
} }