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

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          super(request);
51          this.request = request;
52      }
53  
54      @Override
55      public boolean remove(Object o) {
56          String skey = key(o);
57          Object previous = request.getValue(skey);
58          if (previous != null) {
59              request.removeValue(skey);
60              return true;
61          }
62          return false;
63      }
64  
65      @SuppressWarnings("unchecked")
66      @Override
67      public boolean removeAll(Collection<?> c) {
68          Collection<String> realCollection = (Collection<String>) c;
69          boolean retValue = false;
70          for (String entry : realCollection) {
71              retValue |= remove(entry);
72          }
73          return retValue;
74      }
75  
76      @SuppressWarnings("unchecked")
77      @Override
78      public boolean retainAll(Collection<?> c) {
79          Collection<String> realCollection = (Collection<String>) c;
80          boolean retValue = false;
81          Set<String> keysToRemove = new LinkedHashSet<String>();
82          for (Enumeration<String> keys = request.getKeys(); keys.hasMoreElements();) {
83              String key = keys.nextElement();
84              if (!realCollection.contains(key)) {
85                  retValue = true;
86                  keysToRemove.add(key);
87              }
88          }
89          for (String key : keysToRemove) {
90              request.removeValue(key);
91          }
92          return retValue;
93      }
94  
95  }