Coverage Report - org.seasar.cubby.internal.controller.impl.FlashMap
 
Classes in this File Line Coverage Branch Coverage Complexity
FlashMap
100%
38/38
62%
5/8
0
 
 1  
 /*
 2  
  * Copyright 2004-2009 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.cubby.internal.controller.impl;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.Map;
 20  
 import java.util.Set;
 21  
 import java.util.concurrent.ConcurrentHashMap;
 22  
 
 23  
 import javax.servlet.http.HttpServletRequest;
 24  
 import javax.servlet.http.HttpSession;
 25  
 
 26  
 class FlashMap<K, V> implements Map<K, V> {
 27  
 
 28  1
         private static final String ATTRIBUTE_NAME = FlashMap.class.getName()
 29  
                         + ".MAP";
 30  
 
 31  
         private final HttpServletRequest request;
 32  
 
 33  
         private final Map<K, V> map;
 34  
 
 35  3
         FlashMap(final HttpServletRequest request) {
 36  3
                 this.request = request;
 37  3
                 this.map = buildMap(request);
 38  3
         }
 39  
 
 40  
         private Map<K, V> buildMap(final HttpServletRequest request) {
 41  3
                 final HttpSession session = request.getSession(false);
 42  3
                 if (session != null) {
 43  1
                         final Map<K, V> map = getAttribute(session, ATTRIBUTE_NAME);
 44  1
                         if (map != null) {
 45  1
                                 return map;
 46  
                         }
 47  
                 }
 48  2
                 return new ConcurrentHashMap<K, V>();
 49  
         }
 50  
 
 51  
         private void export(final HttpSession session) {
 52  8
                 session.setAttribute(ATTRIBUTE_NAME, this.map);
 53  8
         }
 54  
 
 55  
         public int size() {
 56  8
                 return map.size();
 57  
         }
 58  
 
 59  
         public boolean isEmpty() {
 60  8
                 return map.isEmpty();
 61  
         }
 62  
 
 63  
         public boolean containsKey(final Object key) {
 64  8
                 return map.containsKey(key);
 65  
         }
 66  
 
 67  
         public boolean containsValue(final Object value) {
 68  8
                 return map.containsValue(value);
 69  
         }
 70  
 
 71  
         public V get(final Object key) {
 72  6
                 return map.get(key);
 73  
         }
 74  
 
 75  
         public V put(final K key, final V value) {
 76  2
                 final V previousValue = map.put(key, value);
 77  2
                 export(request.getSession());
 78  2
                 return previousValue;
 79  
         }
 80  
 
 81  
         public V remove(final Object key) {
 82  2
                 final V removedValue = map.remove(key);
 83  2
                 final HttpSession session = request.getSession(false);
 84  2
                 if (session != null) {
 85  2
                         export(session);
 86  
                 }
 87  2
                 return removedValue;
 88  
         }
 89  
 
 90  
         public void putAll(final Map<? extends K, ? extends V> t) {
 91  2
                 map.putAll(t);
 92  2
                 export(request.getSession());
 93  2
         }
 94  
 
 95  
         public void clear() {
 96  2
                 map.clear();
 97  2
                 final HttpSession session = request.getSession(false);
 98  2
                 if (session != null) {
 99  2
                         export(session);
 100  
                 }
 101  2
         }
 102  
 
 103  
         public Set<K> keySet() {
 104  8
                 return map.keySet();
 105  
         }
 106  
 
 107  
         public Collection<V> values() {
 108  8
                 return map.values();
 109  
         }
 110  
 
 111  
         public Set<Entry<K, V>> entrySet() {
 112  8
                 return map.entrySet();
 113  
         }
 114  
 
 115  
         @SuppressWarnings("unchecked")
 116  
         private static <T> T getAttribute(final HttpSession session,
 117  
                         final String name) {
 118  1
                 return (T) session.getAttribute(name);
 119  
         }
 120  
 
 121  
 }