Coverage Report - org.apache.tiles.request.collection.KeySet
 
Classes in this File Line Coverage Branch Coverage Complexity
KeySet
100%
27/27
100%
10/10
1.611
KeySet$1
N/A
N/A
1.611
KeySet$KeySetIterator
100%
5/5
N/A
1.611
 
 1  
 /*
 2  
  * $Id: KeySet.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.ArrayList;
 26  
 import java.util.Collection;
 27  
 import java.util.Enumeration;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Set;
 31  
 
 32  
 import org.apache.tiles.request.attribute.HasKeys;
 33  
 
 34  
 /**
 35  
  * Exposes keys of a {@link HasKeys} object as a set.
 36  
  *
 37  
  * @version $Rev: 1229087 $ $Date: 2012-01-09 21:35:14 +1100 (Mon, 09 Jan 2012) $
 38  
  */
 39  3
 public class KeySet implements Set<String> {
 40  
 
 41  
     /**
 42  
      * The request to read.
 43  
      */
 44  
     private HasKeys<?> request;
 45  
 
 46  
     /**
 47  
      * Constructor.
 48  
      *
 49  
      * @param request The request to read.
 50  
      */
 51  24
     public KeySet(HasKeys<?> request) {
 52  24
         this.request = request;
 53  24
     }
 54  
 
 55  
     @Override
 56  
     public boolean add(String e) {
 57  1
         throw new UnsupportedOperationException();
 58  
     }
 59  
 
 60  
     @Override
 61  
     public boolean addAll(Collection<? extends String> c) {
 62  1
         throw new UnsupportedOperationException();
 63  
     }
 64  
 
 65  
     @Override
 66  
     public void clear() {
 67  1
         throw new UnsupportedOperationException();
 68  
     }
 69  
 
 70  
     @Override
 71  
     public boolean contains(Object o) {
 72  2
         return request.getValue(key(o)) != null;
 73  
     }
 74  
 
 75  
     @SuppressWarnings("unchecked")
 76  
     @Override
 77  
     public boolean containsAll(Collection<?> c) {
 78  2
         Collection<String> realCollection = (Collection<String>) c;
 79  2
         for (String key : realCollection) {
 80  4
             if (request.getValue(key(key)) == null) {
 81  1
                 return false;
 82  
             }
 83  3
         }
 84  1
         return true;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public boolean isEmpty() {
 89  2
         return !request.getKeys().hasMoreElements();
 90  
     }
 91  
 
 92  
     @Override
 93  
     public Iterator<String> iterator() {
 94  2
         return new KeySetIterator();
 95  
     }
 96  
 
 97  
     @Override
 98  
     public boolean remove(Object o) {
 99  1
         throw new UnsupportedOperationException();
 100  
     }
 101  
 
 102  
     @Override
 103  
     public boolean removeAll(Collection<?> c) {
 104  1
         throw new UnsupportedOperationException();
 105  
     }
 106  
 
 107  
     @Override
 108  
     public boolean retainAll(Collection<?> c) {
 109  1
         throw new UnsupportedOperationException();
 110  
     }
 111  
 
 112  
     @Override
 113  
     public int size() {
 114  1
         return enumerationSize(request.getKeys());
 115  
     }
 116  
 
 117  
     @Override
 118  
     public Object[] toArray() {
 119  1
         return toList().toArray();
 120  
     }
 121  
 
 122  
     @Override
 123  
     public <T> T[] toArray(T[] a) {
 124  1
         return toList().toArray(a);
 125  
     }
 126  
 
 127  
     /**
 128  
      * Turns this set into a list.
 129  
      *
 130  
      * @return The corresponding list.
 131  
      */
 132  
     private List<String> toList() {
 133  2
         List<String> entries = new ArrayList<String>();
 134  2
         Enumeration<String> names = request.getKeys();
 135  6
         while (names.hasMoreElements()) {
 136  4
             entries.add(names.nextElement());
 137  
         }
 138  2
         return entries;
 139  
     }
 140  
 
 141  
     /**
 142  
      * Iterates elements of {@link KeySet}.
 143  
      */
 144  5
     private class KeySetIterator implements Iterator<String> {
 145  
 
 146  
         /**
 147  
          * The key names enumeration.
 148  
          */
 149  2
         private Enumeration<String> namesEnumeration = request.getKeys();
 150  
 
 151  
         @Override
 152  
         public boolean hasNext() {
 153  1
             return namesEnumeration.hasMoreElements();
 154  
         }
 155  
 
 156  
         @Override
 157  
         public String next() {
 158  1
             return namesEnumeration.nextElement();
 159  
         }
 160  
 
 161  
         @Override
 162  
         public void remove() {
 163  1
             throw new UnsupportedOperationException();
 164  
         }
 165  
     }
 166  
 }