Coverage Report - org.apache.shiro.util.MapContext
 
Classes in this File Line Coverage Branch Coverage Complexity
MapContext
60%
20/33
62%
5/8
1.312
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shiro.util;
 20  
 
 21  
 import java.io.Serializable;
 22  
 import java.util.*;
 23  
 
 24  
 /**
 25  
  * A {@code MapContext} provides a common base for context-based data storage in a {@link Map}.  Type-safe attribute
 26  
  * retrieval is provided for subclasses with the {@link #getTypedValue(String, Class)} method.
 27  
  *
 28  
  * @see org.apache.shiro.subject.SubjectContext SubjectContext
 29  
  * @see org.apache.shiro.session.mgt.SessionContext SessionContext
 30  
  * @since 1.0
 31  
  */
 32  0
 public class MapContext implements Map<String, Object>, Serializable {
 33  
 
 34  
     private static final long serialVersionUID = 5373399119017820322L;
 35  
 
 36  
     private final Map<String, Object> backingMap;
 37  
 
 38  114
     public MapContext() {
 39  114
         this.backingMap = new HashMap<String, Object>();
 40  114
     }
 41  
 
 42  
     public MapContext(Map<String, Object> map) {
 43  46
         this();
 44  46
         if (!CollectionUtils.isEmpty(map)) {
 45  46
             this.backingMap.putAll(map);
 46  
         }
 47  46
     }
 48  
 
 49  
     /**
 50  
      * Performs a {@link #get get} operation but additionally ensures that the value returned is of the specified
 51  
      * {@code type}.  If there is no value, {@code null} is returned.
 52  
      *
 53  
      * @param key  the attribute key to look up a value
 54  
      * @param type the expected type of the value
 55  
      * @param <E>  the expected type of the value
 56  
      * @return the typed value or {@code null} if the attribute does not exist.
 57  
      */
 58  
     @SuppressWarnings({"unchecked"})
 59  
     protected <E> E getTypedValue(String key, Class<E> type) {
 60  1061
         E found = null;
 61  1061
         Object o = backingMap.get(key);
 62  1061
         if (o != null) {
 63  181
             if (!type.isAssignableFrom(o.getClass())) {
 64  0
                 String msg = "Invalid object found in SubjectContext Map under key [" + key + "].  Expected type " +
 65  
                         "was [" + type.getName() + "], but the object under that key is of type " +
 66  
                         "[" + o.getClass().getName() + "].";
 67  0
                 throw new IllegalArgumentException(msg);
 68  
             }
 69  181
             found = (E) o;
 70  
         }
 71  1061
         return found;
 72  
     }
 73  
 
 74  
     /**
 75  
      * Places a value in this context map under the given key only if the given {@code value} argument is not null.
 76  
      *
 77  
      * @param key   the attribute key under which the non-null value will be stored
 78  
      * @param value the non-null value to store.  If {@code null}, this method does nothing and returns immediately.
 79  
      */
 80  
     protected void nullSafePut(String key, Object value) {
 81  84
         if (value != null) {
 82  84
             put(key, value);
 83  
         }
 84  84
     }
 85  
 
 86  
     public int size() {
 87  46
         return backingMap.size();
 88  
     }
 89  
 
 90  
     public boolean isEmpty() {
 91  46
         return backingMap.isEmpty();
 92  
     }
 93  
 
 94  
     public boolean containsKey(Object o) {
 95  0
         return backingMap.containsKey(o);
 96  
     }
 97  
 
 98  
     public boolean containsValue(Object o) {
 99  0
         return backingMap.containsValue(o);
 100  
     }
 101  
 
 102  
     public Object get(Object o) {
 103  0
         return backingMap.get(o);
 104  
     }
 105  
 
 106  
     public Object put(String s, Object o) {
 107  102
         return backingMap.put(s, o);
 108  
     }
 109  
 
 110  
     public Object remove(Object o) {
 111  0
         return backingMap.remove(o);
 112  
     }
 113  
 
 114  
     public void putAll(Map<? extends String, ?> map) {
 115  0
         backingMap.putAll(map);
 116  0
     }
 117  
 
 118  
     public void clear() {
 119  0
         backingMap.clear();
 120  0
     }
 121  
 
 122  
     public Set<String> keySet() {
 123  0
         return Collections.unmodifiableSet(backingMap.keySet());
 124  
     }
 125  
 
 126  
     public Collection<Object> values() {
 127  0
         return Collections.unmodifiableCollection(backingMap.values());
 128  
     }
 129  
 
 130  
     public Set<Entry<String, Object>> entrySet() {
 131  46
         return Collections.unmodifiableSet(backingMap.entrySet());
 132  
     }
 133  
 }