BAEL-6600: fix flaws in the logic of put method

This commit is contained in:
mcasari 2023-11-14 23:22:44 +01:00
parent 2f9407e765
commit 8a31837a3e
1 changed files with 5 additions and 8 deletions

View File

@ -11,17 +11,14 @@ public class HashMapWithMaxSizeLimit<K, V> extends HashMap<K, V> {
public HashMapWithMaxSizeLimit(int maxSize) { public HashMapWithMaxSizeLimit(int maxSize) {
super(); super();
this.maxSize = maxSize; this.maxSize = maxSize;
} }
@Override @Override
public V put(K key, V value) { public V put(K key, V value) {
V res = null; if (this.maxSize == -1 || this.containsKey(key) || this.size() < this.maxSize) {
if (this.maxSize == -1 || this.size() < this.maxSize) { return super.put(key, value);
res = super.put(key, value); }
} else if (this.maxSize != -1) { throw new RuntimeException("Max size exceeded!");
throw new RuntimeException("Max size exceeded!");
}
return res;
} }
} }