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

@ -8,18 +8,20 @@ public class HashMapWithMaxSizeLimit<K, V> extends HashMap<K, V> {
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!");
@Override
public V put(K key, V value) {
V res = null;
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;
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 org.junit.jupiter.api.BeforeEach;
@ -11,33 +12,27 @@ import org.junit.jupiter.api.Test;
class LimitMaxSizeHashMapByCustomHashMapUnitTest {
private final int MAX_SIZE = 4;
private LinkedHashMap<Integer, String> linkedHashMap;
private HashMapWithMaxSizeLimit<Integer, String> hashMapWithMaxSizeLimit;
@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");
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE);
}
@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());
void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() {
Exception exception = assertThrows(RuntimeException.class, () -> {
hashMapWithMaxSizeLimit.put(1, "One");
hashMapWithMaxSizeLimit.put(2, "Two");
hashMapWithMaxSizeLimit.put(3, "Three");
hashMapWithMaxSizeLimit.put(4, "Four");
hashMapWithMaxSizeLimit.put(5, "Five");
});
String messageThrownWhenSizeExceedsLimit = "Max size exceeded!";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit));
}
}

View File

@ -1,7 +1,9 @@
package com.baeldung.map;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
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;
@ -9,27 +11,33 @@ import org.junit.jupiter.api.Test;
class LimitMaxSizeHashMapByLinkedHashMapUnitTest {
private final int MAX_SIZE = 4;
private HashMapWithMaxSizeLimit<Integer, String> hashMapWithMaxSizeLimit;
private LinkedHashMap<Integer, String> linkedHashMap;
@BeforeEach
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
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));
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());
}
}