| 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.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 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 3 | public class KeySet implements Set<String> { |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
private HasKeys<?> request; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 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 | |
|
| 129 | |
|
| 130 | |
|
| 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 | |
|
| 143 | |
|
| 144 | 5 | private class KeySetIterator implements Iterator<String> { |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 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 | |
} |