Coverage Report - org.seasar.teeda.core.context.AbstractExternalContextMap
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractExternalContextMap
100%
44/44
82%
18/22
1.49
AbstractExternalContextMap$AbstractExternalContextIterator
43%
10/23
0%
0/10
1.49
AbstractExternalContextMap$AbstractExternalContextSet
20%
1/5
0%
0/2
1.49
AbstractExternalContextMap$EntryIterator
62%
5/8
N/A
1.49
AbstractExternalContextMap$EntrySet
44%
4/9
0%
0/4
1.49
AbstractExternalContextMap$ImmutableEntry
29%
4/14
0%
0/20
1.49
AbstractExternalContextMap$KeyIterator
67%
4/6
N/A
1.49
AbstractExternalContextMap$KeySet
44%
4/9
0%
0/4
1.49
AbstractExternalContextMap$ValuesCollection
100%
8/8
100%
2/2
1.49
AbstractExternalContextMap$ValuesIterator
56%
5/9
N/A
1.49
 
 1  
 /*
 2  
  * Copyright 2004-2011 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.teeda.core.context;
 17  
 
 18  
 import java.util.AbstractCollection;
 19  
 import java.util.AbstractMap;
 20  
 import java.util.AbstractSet;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collection;
 23  
 import java.util.Enumeration;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Set;
 28  
 
 29  
 import org.seasar.framework.util.EnumerationIterator;
 30  
 
 31  
 /**
 32  
  * @author shot
 33  
  */
 34  
 public abstract class AbstractExternalContextMap extends AbstractMap {
 35  
 
 36  
     //TODO REMOVE this after org.seasar.teeda.core.context.portlet.* remove.
 37  
     private Set entrySet_;
 38  
 
 39  
     private Set keySet_;
 40  
 
 41  
     private Collection values_;
 42  
 
 43  66
     public AbstractExternalContextMap() {
 44  66
     }
 45  
 
 46  
     public void clear() {
 47  
         //avoid ConcurrentModificationException
 48  1
         List list = new ArrayList();
 49  1
         for (Enumeration e = getAttributeNames(); e.hasMoreElements();) {
 50  2
             String key = (String) e.nextElement();
 51  2
             list.add(key);
 52  
         }
 53  1
         clearReally(list);
 54  1
     }
 55  
 
 56  
     private void clearReally(List keys) {
 57  1
         for (Iterator itr = keys.iterator(); itr.hasNext();) {
 58  2
             String key = (String) itr.next();
 59  2
             removeAttribute(key);
 60  
         }
 61  1
     }
 62  
 
 63  
     public boolean containsKey(Object key) {
 64  40
         return (getAttribute(key.toString()) != null);
 65  
     }
 66  
 
 67  
     public boolean containsValue(Object value) {
 68  2
         if (value != null) {
 69  2
             for (Enumeration e = getAttributeNames(); e.hasMoreElements();) {
 70  5
                 String key = (String) e.nextElement();
 71  5
                 Object attributeValue = getAttribute(key);
 72  5
                 if (value.equals(attributeValue)) {
 73  1
                     return true;
 74  
                 }
 75  
             }
 76  
         }
 77  1
         return false;
 78  
     }
 79  
 
 80  
     public Set entrySet() {
 81  1
         if (entrySet_ == null) {
 82  1
             entrySet_ = new EntrySet(this);
 83  
         }
 84  1
         return entrySet_;
 85  
     }
 86  
 
 87  
     public Object get(Object key) {
 88  64
         return getAttribute(key.toString());
 89  
     }
 90  
 
 91  
     public Object put(Object key, Object value) {
 92  41
         String keyStr = key.toString();
 93  41
         Object o = getAttribute(keyStr);
 94  41
         setAttribute(keyStr, value);
 95  41
         return o;
 96  
     }
 97  
 
 98  
     public void putAll(Map map) {
 99  1
         for (Iterator itr = map.entrySet().iterator(); itr.hasNext();) {
 100  2
             Map.Entry entry = (Map.Entry) itr.next();
 101  2
             String key = (String) entry.getKey();
 102  2
             setAttribute(key, entry.getValue());
 103  
         }
 104  1
     }
 105  
 
 106  
     public boolean isEmpty() {
 107  6
         return !getAttributeNames().hasMoreElements();
 108  
     }
 109  
 
 110  
     public Set keySet() {
 111  1
         if (keySet_ == null) {
 112  1
             keySet_ = new KeySet(this);
 113  
         }
 114  1
         return keySet_;
 115  
     }
 116  
 
 117  
     public Object remove(Object key) {
 118  1
         String keyStr = key.toString();
 119  1
         Object o = getAttribute(keyStr);
 120  1
         removeAttribute(keyStr);
 121  1
         return o;
 122  
     }
 123  
 
 124  
     public Collection values() {
 125  1
         if (values_ == null) {
 126  1
             values_ = new ValuesCollection(this);
 127  
         }
 128  1
         return values_;
 129  
     }
 130  
 
 131  
     protected abstract Object getAttribute(String key);
 132  
 
 133  
     protected abstract void setAttribute(String key, Object value);
 134  
 
 135  
     protected abstract Enumeration getAttributeNames();
 136  
 
 137  
     protected abstract void removeAttribute(String key);
 138  
 
 139  2
     abstract class AbstractExternalContextSet extends AbstractSet {
 140  
 
 141  
         /* (non-Javadoc)
 142  
          * @see java.util.AbstractCollection#size()
 143  
          */
 144  
         public int size() {
 145  0
             int size = 0;
 146  0
             for (Iterator itr = iterator(); itr.hasNext(); size++) {
 147  0
                 itr.next();
 148  
             }
 149  0
             return size;
 150  
         }
 151  
 
 152  
     }
 153  
 
 154  
     class EntrySet extends AbstractExternalContextSet {
 155  
 
 156  
         private AbstractExternalContextMap contextMap_;
 157  
 
 158  1
         public EntrySet(AbstractExternalContextMap contextMap) {
 159  1
             contextMap_ = contextMap;
 160  1
         }
 161  
 
 162  
         /* (non-Javadoc)
 163  
          * @see java.util.AbstractCollection#iterator()
 164  
          */
 165  
         public Iterator iterator() {
 166  1
             return new EntryIterator(contextMap_);
 167  
         }
 168  
 
 169  
         /* (non-Javadoc)
 170  
          * @see java.util.AbstractCollection#remove(java.lang.Object)
 171  
          */
 172  
         public boolean remove(Object o) {
 173  0
             if (!(o instanceof Map.Entry)) {
 174  0
                 return false;
 175  
             }
 176  0
             Map.Entry entry = (Map.Entry) o;
 177  0
             Object returnObj = contextMap_.remove(entry.getKey());
 178  0
             return (returnObj != null);
 179  
         }
 180  
     }
 181  
 
 182  
     class KeySet extends AbstractExternalContextSet {
 183  
 
 184  
         private AbstractExternalContextMap contextMap_;
 185  
 
 186  1
         public KeySet(AbstractExternalContextMap contextMap) {
 187  1
             contextMap_ = contextMap;
 188  1
         }
 189  
 
 190  
         /* (non-Javadoc)
 191  
          * @see java.util.AbstractCollection#iterator()
 192  
          */
 193  
         public Iterator iterator() {
 194  1
             return new KeyIterator(contextMap_);
 195  
         }
 196  
 
 197  
         /* (non-Javadoc)
 198  
          * @see java.util.AbstractCollection#remove(java.lang.Object)
 199  
          */
 200  
         public boolean remove(Object o) {
 201  0
             if (!(o instanceof String)) {
 202  0
                 return false;
 203  
             }
 204  0
             String s = (String) o;
 205  0
             Object returnObj = contextMap_.remove(s);
 206  0
             return (returnObj != null);
 207  
         }
 208  
     }
 209  
 
 210  
     class ValuesCollection extends AbstractCollection {
 211  
 
 212  
         private AbstractExternalContextMap contextMap_;
 213  
 
 214  1
         public ValuesCollection(AbstractExternalContextMap contextMap) {
 215  1
             contextMap_ = contextMap;
 216  1
         }
 217  
 
 218  
         public int size() {
 219  1
             int size = 0;
 220  4
             for (Iterator itr = iterator(); itr.hasNext(); size++) {
 221  3
                 itr.next();
 222  
             }
 223  1
             return size;
 224  
         }
 225  
 
 226  
         public Iterator iterator() {
 227  4
             return new ValuesIterator(contextMap_);
 228  
         }
 229  
 
 230  
     }
 231  
 
 232  
     abstract class AbstractExternalContextIterator extends EnumerationIterator {
 233  
 
 234  
         private final AbstractExternalContextMap contextMap_;
 235  
 
 236  
         private String currentKey_;
 237  
 
 238  6
         private boolean removeCalled_ = false;
 239  
 
 240  
         public AbstractExternalContextIterator(
 241  6
                 final AbstractExternalContextMap contextMap) {
 242  6
             super(contextMap.getAttributeNames());
 243  6
             contextMap_ = contextMap;
 244  6
         }
 245  
 
 246  
         public Object next() {
 247  13
             currentKey_ = (String) super.next();
 248  
             try {
 249  13
                 return doNext();
 250  
             } finally {
 251  13
                 removeCalled_ = false;
 252  
             }
 253  
         }
 254  
 
 255  
         public void remove() {
 256  0
             if (currentKey_ != null && !removeCalled_) {
 257  0
                 doRemove();
 258  0
                 removeCalled_ = true;
 259  
             } else {
 260  0
                 throw new IllegalStateException();
 261  
             }
 262  0
         }
 263  
 
 264  
         protected String getCurrentKey() {
 265  13
             return currentKey_;
 266  
         }
 267  
 
 268  
         protected Object getValueFromMap(String key) {
 269  11
             return contextMap_.get(key);
 270  
         }
 271  
 
 272  
         protected void removeKeyFromMap(String key) {
 273  0
             contextMap_.remove(key);
 274  0
         }
 275  
 
 276  
         protected void removeValueFromMap(Object value) {
 277  0
             if (containsValue(value)) {
 278  0
                 for (Iterator itr = entrySet().iterator(); itr.hasNext();) {
 279  0
                     Map.Entry e = (Map.Entry) itr.next();
 280  0
                     if (value.equals(e.getValue())) {
 281  0
                         contextMap_.remove(e.getKey());
 282  
                     }
 283  
                 }
 284  
             }
 285  
 
 286  0
         }
 287  
 
 288  
         protected abstract Object doNext();
 289  
 
 290  
         protected abstract void doRemove();
 291  
     }
 292  
 
 293  
     class EntryIterator extends AbstractExternalContextIterator {
 294  
 
 295  1
         public EntryIterator(AbstractExternalContextMap contextMap) {
 296  1
             super(contextMap);
 297  1
         }
 298  
 
 299  
         protected Object doNext() {
 300  2
             String key = getCurrentKey();
 301  2
             return new ImmutableEntry(key, getValueFromMap(key));
 302  
         }
 303  
 
 304  
         protected void doRemove() {
 305  0
             String key = getCurrentKey();
 306  0
             removeKeyFromMap(key);
 307  0
         }
 308  
 
 309  
     }
 310  
 
 311  
     class KeyIterator extends AbstractExternalContextIterator {
 312  
 
 313  1
         public KeyIterator(AbstractExternalContextMap contextMap) {
 314  1
             super(contextMap);
 315  1
         }
 316  
 
 317  
         protected Object doNext() {
 318  2
             return getCurrentKey();
 319  
         }
 320  
 
 321  
         protected void doRemove() {
 322  0
             removeKeyFromMap(getCurrentKey());
 323  0
         }
 324  
     }
 325  
 
 326  
     class ValuesIterator extends AbstractExternalContextIterator {
 327  
 
 328  4
         public ValuesIterator(AbstractExternalContextMap contextMap) {
 329  4
             super(contextMap);
 330  4
         }
 331  
 
 332  
         protected Object doNext() {
 333  9
             String key = getCurrentKey();
 334  9
             return getValueFromMap(key);
 335  
         }
 336  
 
 337  
         protected void doRemove() {
 338  0
             String key = getCurrentKey();
 339  0
             Object value = getValueFromMap(key);
 340  0
             removeValueFromMap(value);
 341  0
         }
 342  
 
 343  
     }
 344  
 
 345  
     protected static class ImmutableEntry implements Map.Entry {
 346  
 
 347  
         private final Object key_;
 348  
 
 349  
         private final Object value_;
 350  
 
 351  2
         public ImmutableEntry(Object key, Object value) {
 352  2
             key_ = key;
 353  2
             value_ = value;
 354  2
         }
 355  
 
 356  
         public Object getKey() {
 357  0
             return key_;
 358  
         }
 359  
 
 360  
         public Object getValue() {
 361  0
             return value_;
 362  
         }
 363  
 
 364  
         public Object setValue(Object arg0) {
 365  0
             throw new UnsupportedOperationException("Immutable entry.");
 366  
         }
 367  
 
 368  
         public boolean equals(Object obj) {
 369  0
             if (obj == null || !(obj instanceof ImmutableEntry)) {
 370  0
                 return false;
 371  
             }
 372  0
             ImmutableEntry entry = (ImmutableEntry) obj;
 373  0
             Object key = entry.getKey();
 374  0
             Object value = entry.getValue();
 375  
 
 376  0
             return (key == key_ || (key != null && key.equals(key_)))
 377  
                     && (value == value_ || (value != null && value
 378  
                             .equals(value_)));
 379  
         }
 380  
 
 381  
         public int hashCode() {
 382  0
             return ((key_ != null) ? key_.hashCode() : 0)
 383  
                     ^ ((value_ != null) ? value_.hashCode() : 0);
 384  
         }
 385  
     }
 386  
 
 387  
 }