230 lines
6.8 KiB
HTML
230 lines
6.8 KiB
HTML
|
|
|
|
|
|
<!DOCTYPE html>
|
|
<html id="htmlId">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
|
<title>Coverage Report > LimitedMap</title>
|
|
<style type="text/css">
|
|
@import "../../css/coverage.css";
|
|
@import "../../css/idea.min.css";
|
|
</style>
|
|
<script type="text/javascript" src="../../js/highlight.min.js"></script>
|
|
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="content">
|
|
<div class="breadCrumbs">
|
|
Current scope: <a href="../../index.html">all classes</a>
|
|
<span class="separator">|</span>
|
|
<a href="../index.html">it.polimi.ingsw.gc14</a>
|
|
</div>
|
|
|
|
<h1>Coverage Summary for Class: LimitedMap (it.polimi.ingsw.gc14)</h1>
|
|
|
|
<table class="coverageStats">
|
|
<tr>
|
|
<th class="name">Class</th>
|
|
<th class="coverageStat
|
|
">
|
|
Class, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Method, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Branch, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Line, %
|
|
</th>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">LimitedMap</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/1)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/15)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/10)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/27)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<br/>
|
|
<br/>
|
|
|
|
|
|
<pre>
|
|
<code class="sourceCode" id="sourceCode"> package it.polimi.ingsw.gc14;
|
|
|
|
import java.util.Collection;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
/**
|
|
* A {@link ConcurrentHashMap}-backed map with a configurable size limit and an associated action.
|
|
* When the number of elements reaches or exceeds the limit, the specified action is automatically triggered.
|
|
* This implementation is thread-safe for {@code put}; callers that need compound operations must synchronize externally.
|
|
*
|
|
* @param <K> the type of keys maintained by this map.
|
|
* @param <V> the type of mapped values.
|
|
*/
|
|
public class LimitedMap<K, V> implements Map<K, V> {
|
|
/** The underlying thread-safe hash map storing all key-value pairs. */
|
|
<b class="nc"> private final ConcurrentHashMap<K, V> map = new ConcurrentHashMap<>();</b>
|
|
|
|
/**
|
|
* The maximum number of elements allowed in the map before the action is triggered.
|
|
*/
|
|
private volatile int limit;
|
|
|
|
/**
|
|
* The action to execute when the map size reaches or exceeds the limit.
|
|
*/
|
|
private volatile Runnable action;
|
|
|
|
/**
|
|
* Creates a new {@code LimitedMap} with the specified limit and action.
|
|
*
|
|
* @param limit the maximum number of elements before the action is triggered.
|
|
* @param action the action to execute when the limit is reached.
|
|
*/
|
|
<b class="nc"> public LimitedMap(int limit, Runnable action) {</b>
|
|
<b class="nc"> this.limit = limit;</b>
|
|
<b class="nc"> this.action = action;</b>
|
|
}
|
|
|
|
/**
|
|
* Associates the specified value with the specified key in this map.
|
|
* If the map size reaches or exceeds the limit after the insertion, the configured action is triggered.
|
|
*
|
|
* @param key the key with which the specified value is to be associated.
|
|
* @param value the value to be associated with the specified key.
|
|
* @return the previous value associated with the key, or {@code null} if there was no mapping.
|
|
*/
|
|
@Override
|
|
public synchronized V put(K key, V value) {
|
|
<b class="nc"> boolean added = true;</b>
|
|
<b class="nc"> if (map.size() == limit) {</b>
|
|
<b class="nc"> if (!map.containsKey(key))</b>
|
|
<b class="nc"> return null;</b>
|
|
<b class="nc"> added = false;</b>
|
|
}
|
|
<b class="nc"> V result = map.put(key, value);</b>
|
|
<b class="nc"> if (map.size() >= limit && added) {</b>
|
|
<b class="nc"> action.run();</b>
|
|
}
|
|
<b class="nc"> return result;</b>
|
|
}
|
|
|
|
@Override
|
|
<b class="nc"> public synchronized V remove(Object key) { return map.remove(key); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public V get(Object key) { return map.get(key); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public boolean containsKey(Object key) { return map.containsKey(key); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public boolean containsValue(Object value) { return map.containsValue(value); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public int size() { return map.size(); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public boolean isEmpty() { return map.isEmpty(); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public void putAll(Map<? extends K, ? extends V> m) { m.forEach(this::put); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public void clear() { map.clear(); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public Set<K> keySet() { return map.keySet(); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public Collection<V> values() { return map.values(); }</b>
|
|
|
|
@Override
|
|
<b class="nc"> public Set<Entry<K, V>> entrySet() { return map.entrySet(); }</b>
|
|
|
|
/**
|
|
* Sets a new size limit for this map.
|
|
*
|
|
* @param num the new limit.
|
|
*/
|
|
<b class="nc"> public void setLimit(int num) { this.limit = num; }</b>
|
|
|
|
/**
|
|
* Sets a new action to execute when the map size reaches or exceeds the limit.
|
|
*
|
|
* @param action the new action to set; must not be {@code null}.
|
|
*/
|
|
public void setAction(Runnable action) {
|
|
<b class="nc"> if (action == null) throw new IllegalArgumentException("action must not be null");</b>
|
|
<b class="nc"> this.action = action;</b>
|
|
}
|
|
}
|
|
</code>
|
|
</pre>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
(function() {
|
|
var msie = false, msie9 = false;
|
|
/*@cc_on
|
|
msie = true;
|
|
@if (@_jscript_version >= 9)
|
|
msie9 = true;
|
|
@end
|
|
@*/
|
|
|
|
if (!msie || msie && msie9) {
|
|
hljs.highlightAll()
|
|
hljs.initLineNumbersOnLoad();
|
|
}
|
|
})();
|
|
</script>
|
|
|
|
<div class="footer">
|
|
|
|
<div style="float:right;">generated on 2026-06-19 22:53</div>
|
|
</div>
|
|
</body>
|
|
</html>
|