| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public class RemovableKeySet extends KeySet { |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
private HasRemovableKeys<?> request; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 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 | |
} |