This project has retired. For details please refer to its Attic page.
KeySet xref
View Javadoc

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  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      public KeySet(HasKeys<?> request) {
52          this.request = request;
53      }
54  
55      @Override
56      public boolean add(String e) {
57          throw new UnsupportedOperationException();
58      }
59  
60      @Override
61      public boolean addAll(Collection<? extends String> c) {
62          throw new UnsupportedOperationException();
63      }
64  
65      @Override
66      public void clear() {
67          throw new UnsupportedOperationException();
68      }
69  
70      @Override
71      public boolean contains(Object o) {
72          return request.getValue(key(o)) != null;
73      }
74  
75      @SuppressWarnings("unchecked")
76      @Override
77      public boolean containsAll(Collection<?> c) {
78          Collection<String> realCollection = (Collection<String>) c;
79          for (String key : realCollection) {
80              if (request.getValue(key(key)) == null) {
81                  return false;
82              }
83          }
84          return true;
85      }
86  
87      @Override
88      public boolean isEmpty() {
89          return !request.getKeys().hasMoreElements();
90      }
91  
92      @Override
93      public Iterator<String> iterator() {
94          return new KeySetIterator();
95      }
96  
97      @Override
98      public boolean remove(Object o) {
99          throw new UnsupportedOperationException();
100     }
101 
102     @Override
103     public boolean removeAll(Collection<?> c) {
104         throw new UnsupportedOperationException();
105     }
106 
107     @Override
108     public boolean retainAll(Collection<?> c) {
109         throw new UnsupportedOperationException();
110     }
111 
112     @Override
113     public int size() {
114         return enumerationSize(request.getKeys());
115     }
116 
117     @Override
118     public Object[] toArray() {
119         return toList().toArray();
120     }
121 
122     @Override
123     public <T> T[] toArray(T[] a) {
124         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         List<String> entries = new ArrayList<String>();
134         Enumeration<String> names = request.getKeys();
135         while (names.hasMoreElements()) {
136             entries.add(names.nextElement());
137         }
138         return entries;
139     }
140 
141     /**
142      * Iterates elements of {@link KeySet}.
143      */
144     private class KeySetIterator implements Iterator<String> {
145 
146         /**
147          * The key names enumeration.
148          */
149         private Enumeration<String> namesEnumeration = request.getKeys();
150 
151         @Override
152         public boolean hasNext() {
153             return namesEnumeration.hasMoreElements();
154         }
155 
156         @Override
157         public String next() {
158             return namesEnumeration.nextElement();
159         }
160 
161         @Override
162         public void remove() {
163             throw new UnsupportedOperationException();
164         }
165     }
166 }