blob: ab4261108424a089c193a9dff713ecf2bfb4c6af [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>MapContext.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Test Coverage</a> &gt; <a href="../index.html" class="el_bundle">shiro-core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.util</a> &gt; <span class="el_source">MapContext.java</span></div><h1>MapContext.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.util;
import java.io.Serializable;
import java.util.*;
/**
* A {@code MapContext} provides a common base for context-based data storage in a {@link Map}. Type-safe attribute
* retrieval is provided for subclasses with the {@link #getTypedValue(String, Class)} method.
*
* @see org.apache.shiro.subject.SubjectContext SubjectContext
* @see org.apache.shiro.session.mgt.SessionContext SessionContext
* @since 1.0
*/
public class MapContext implements Map&lt;String, Object&gt;, Serializable {
private static final long serialVersionUID = 5373399119017820322L;
private final Map&lt;String, Object&gt; backingMap;
<span class="fc" id="L38"> public MapContext() {</span>
<span class="fc" id="L39"> this.backingMap = new HashMap&lt;String, Object&gt;();</span>
<span class="fc" id="L40"> }</span>
public MapContext(Map&lt;String, Object&gt; map) {
<span class="fc" id="L43"> this();</span>
<span class="pc bpc" id="L44" title="1 of 2 branches missed."> if (!CollectionUtils.isEmpty(map)) {</span>
<span class="fc" id="L45"> this.backingMap.putAll(map);</span>
}
<span class="fc" id="L47"> }</span>
/**
* Performs a {@link #get get} operation but additionally ensures that the value returned is of the specified
* {@code type}. If there is no value, {@code null} is returned.
*
* @param key the attribute key to look up a value
* @param type the expected type of the value
* @param &lt;E&gt; the expected type of the value
* @return the typed value or {@code null} if the attribute does not exist.
*/
@SuppressWarnings({&quot;unchecked&quot;})
protected &lt;E&gt; E getTypedValue(String key, Class&lt;E&gt; type) {
<span class="fc" id="L60"> E found = null;</span>
<span class="fc" id="L61"> Object o = backingMap.get(key);</span>
<span class="fc bfc" id="L62" title="All 2 branches covered."> if (o != null) {</span>
<span class="pc bpc" id="L63" title="1 of 2 branches missed."> if (!type.isAssignableFrom(o.getClass())) {</span>
<span class="nc" id="L64"> String msg = &quot;Invalid object found in SubjectContext Map under key [&quot; + key + &quot;]. Expected type &quot; +</span>
<span class="nc" id="L65"> &quot;was [&quot; + type.getName() + &quot;], but the object under that key is of type &quot; +</span>
<span class="nc" id="L66"> &quot;[&quot; + o.getClass().getName() + &quot;].&quot;;</span>
<span class="nc" id="L67"> throw new IllegalArgumentException(msg);</span>
}
<span class="fc" id="L69"> found = (E) o;</span>
}
<span class="fc" id="L71"> return found;</span>
}
/**
* Places a value in this context map under the given key only if the given {@code value} argument is not null.
*
* @param key the attribute key under which the non-null value will be stored
* @param value the non-null value to store. If {@code null}, this method does nothing and returns immediately.
*/
protected void nullSafePut(String key, Object value) {
<span class="pc bpc" id="L81" title="1 of 2 branches missed."> if (value != null) {</span>
<span class="fc" id="L82"> put(key, value);</span>
}
<span class="fc" id="L84"> }</span>
public int size() {
<span class="fc" id="L87"> return backingMap.size();</span>
}
public boolean isEmpty() {
<span class="fc" id="L91"> return backingMap.isEmpty();</span>
}
public boolean containsKey(Object o) {
<span class="nc" id="L95"> return backingMap.containsKey(o);</span>
}
public boolean containsValue(Object o) {
<span class="nc" id="L99"> return backingMap.containsValue(o);</span>
}
public Object get(Object o) {
<span class="nc" id="L103"> return backingMap.get(o);</span>
}
public Object put(String s, Object o) {
<span class="fc" id="L107"> return backingMap.put(s, o);</span>
}
public Object remove(Object o) {
<span class="nc" id="L111"> return backingMap.remove(o);</span>
}
public void putAll(Map&lt;? extends String, ?&gt; map) {
<span class="nc" id="L115"> backingMap.putAll(map);</span>
<span class="nc" id="L116"> }</span>
public void clear() {
<span class="nc" id="L119"> backingMap.clear();</span>
<span class="nc" id="L120"> }</span>
public Set&lt;String&gt; keySet() {
<span class="nc" id="L123"> return Collections.unmodifiableSet(backingMap.keySet());</span>
}
public Collection&lt;Object&gt; values() {
<span class="nc" id="L127"> return Collections.unmodifiableCollection(backingMap.values());</span>
}
public Set&lt;Entry&lt;String, Object&gt;&gt; entrySet() {
<span class="fc" id="L131"> return Collections.unmodifiableSet(backingMap.entrySet());</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html>