Coverage Report - org.apache.tiles.request.collection.RemovableKeySet
 
Classes in this File Line Coverage Branch Coverage Complexity
RemovableKeySet
100%
28/28
100%
10/10
2.5
 
 1  
 /*
 2  
  * $Id: RemovableKeySet.java 1229087 2012-01-09 10:35:14Z mck $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 package org.apache.tiles.request.collection;
 22  
 
 23  
 import static org.apache.tiles.request.collection.CollectionUtil.*;
 24  
 
 25  
 import java.util.Collection;
 26  
 import java.util.Enumeration;
 27  
 import java.util.LinkedHashSet;
 28  
 import java.util.Set;
 29  
 
 30  
 import org.apache.tiles.request.attribute.HasRemovableKeys;
 31  
 
 32  
 /**
 33  
  * Wraps {@link HasRemovableKeys} keys as a set.
 34  
  *
 35  
  * @version $Rev: 1229087 $ $Date: 2012-01-09 21:35:14 +1100 (Mon, 09 Jan 2012) $
 36  
  */
 37  
 public class RemovableKeySet extends KeySet {
 38  
 
 39  
     /**
 40  
      * The request.
 41  
      */
 42  
     private HasRemovableKeys<?> request;
 43  
 
 44  
     /**
 45  
      * Constructor.
 46  
      *
 47  
      * @param request The request.
 48  
      */
 49  
     public RemovableKeySet(HasRemovableKeys<?> request) {
 50  5
         super(request);
 51  5
         this.request = request;
 52  5
     }
 53  
 
 54  
     @Override
 55  
     public boolean remove(Object o) {
 56  4
         String skey = key(o);
 57  4
         Object previous = request.getValue(skey);
 58  4
         if (previous != null) {
 59  3
             request.removeValue(skey);
 60  3
             return true;
 61  
         }
 62  1
         return false;
 63  
     }
 64  
 
 65  
     @SuppressWarnings("unchecked")
 66  
     @Override
 67  
     public boolean removeAll(Collection<?> c) {
 68  1
         Collection<String> realCollection = (Collection<String>) c;
 69  1
         boolean retValue = false;
 70  1
         for (String entry : realCollection) {
 71  2
             retValue |= remove(entry);
 72  2
         }
 73  1
         return retValue;
 74  
     }
 75  
 
 76  
     @SuppressWarnings("unchecked")
 77  
     @Override
 78  
     public boolean retainAll(Collection<?> c) {
 79  1
         Collection<String> realCollection = (Collection<String>) c;
 80  1
         boolean retValue = false;
 81  1
         Set<String> keysToRemove = new LinkedHashSet<String>();
 82  1
         for (Enumeration<String> keys = request.getKeys(); keys.hasMoreElements();) {
 83  3
             String key = keys.nextElement();
 84  3
             if (!realCollection.contains(key)) {
 85  1
                 retValue = true;
 86  1
                 keysToRemove.add(key);
 87  
             }
 88  3
         }
 89  1
         for (String key : keysToRemove) {
 90  1
             request.removeValue(key);
 91  1
         }
 92  1
         return retValue;
 93  
     }
 94  
 
 95  
 }