Files
2026-06-19 22:57:43 +02:00

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">&nbsp;package it.polimi.ingsw.gc14;
&nbsp;
&nbsp;import java.util.Collection;
&nbsp;import java.util.Map;
&nbsp;import java.util.Set;
&nbsp;import java.util.concurrent.ConcurrentHashMap;
&nbsp;
&nbsp;/**
&nbsp; * A {@link ConcurrentHashMap}-backed map with a configurable size limit and an associated action.
&nbsp; * When the number of elements reaches or exceeds the limit, the specified action is automatically triggered.
&nbsp; * This implementation is thread-safe for {@code put}; callers that need compound operations must synchronize externally.
&nbsp; *
&nbsp; * @param &lt;K&gt; the type of keys maintained by this map.
&nbsp; * @param &lt;V&gt; the type of mapped values.
&nbsp; */
&nbsp;public class LimitedMap&lt;K, V&gt; implements Map&lt;K, V&gt; {
&nbsp; /** The underlying thread-safe hash map storing all key-value pairs. */
<b class="nc">&nbsp; private final ConcurrentHashMap&lt;K, V&gt; map = new ConcurrentHashMap&lt;&gt;();</b>
&nbsp;
&nbsp; /**
&nbsp; * The maximum number of elements allowed in the map before the action is triggered.
&nbsp; */
&nbsp; private volatile int limit;
&nbsp;
&nbsp; /**
&nbsp; * The action to execute when the map size reaches or exceeds the limit.
&nbsp; */
&nbsp; private volatile Runnable action;
&nbsp;
&nbsp; /**
&nbsp; * Creates a new {@code LimitedMap} with the specified limit and action.
&nbsp; *
&nbsp; * @param limit the maximum number of elements before the action is triggered.
&nbsp; * @param action the action to execute when the limit is reached.
&nbsp; */
<b class="nc">&nbsp; public LimitedMap(int limit, Runnable action) {</b>
<b class="nc">&nbsp; this.limit = limit;</b>
<b class="nc">&nbsp; this.action = action;</b>
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Associates the specified value with the specified key in this map.
&nbsp; * If the map size reaches or exceeds the limit after the insertion, the configured action is triggered.
&nbsp; *
&nbsp; * @param key the key with which the specified value is to be associated.
&nbsp; * @param value the value to be associated with the specified key.
&nbsp; * @return the previous value associated with the key, or {@code null} if there was no mapping.
&nbsp; */
&nbsp; @Override
&nbsp; public synchronized V put(K key, V value) {
<b class="nc">&nbsp; boolean added = true;</b>
<b class="nc">&nbsp; if (map.size() == limit) {</b>
<b class="nc">&nbsp; if (!map.containsKey(key))</b>
<b class="nc">&nbsp; return null;</b>
<b class="nc">&nbsp; added = false;</b>
&nbsp; }
<b class="nc">&nbsp; V result = map.put(key, value);</b>
<b class="nc">&nbsp; if (map.size() &gt;= limit &amp;&amp; added) {</b>
<b class="nc">&nbsp; action.run();</b>
&nbsp; }
<b class="nc">&nbsp; return result;</b>
&nbsp; }
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public synchronized V remove(Object key) { return map.remove(key); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public V get(Object key) { return map.get(key); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public boolean containsKey(Object key) { return map.containsKey(key); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public boolean containsValue(Object value) { return map.containsValue(value); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public int size() { return map.size(); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public boolean isEmpty() { return map.isEmpty(); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public void putAll(Map&lt;? extends K, ? extends V&gt; m) { m.forEach(this::put); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public void clear() { map.clear(); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public Set&lt;K&gt; keySet() { return map.keySet(); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public Collection&lt;V&gt; values() { return map.values(); }</b>
&nbsp;
&nbsp; @Override
<b class="nc">&nbsp; public Set&lt;Entry&lt;K, V&gt;&gt; entrySet() { return map.entrySet(); }</b>
&nbsp;
&nbsp; /**
&nbsp; * Sets a new size limit for this map.
&nbsp; *
&nbsp; * @param num the new limit.
&nbsp; */
<b class="nc">&nbsp; public void setLimit(int num) { this.limit = num; }</b>
&nbsp;
&nbsp; /**
&nbsp; * Sets a new action to execute when the map size reaches or exceeds the limit.
&nbsp; *
&nbsp; * @param action the new action to set; must not be {@code null}.
&nbsp; */
&nbsp; public void setAction(Runnable action) {
<b class="nc">&nbsp; if (action == null) throw new IllegalArgumentException(&quot;action must not be null&quot;);</b>
<b class="nc">&nbsp; this.action = action;</b>
&nbsp; }
&nbsp;}
</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>