01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.tomcat.util.collections;
18:
19: import java.util.Enumeration;
20:
21: /** Enumerate the distinct header names.
22: Each nextElement() is O(n) ( a comparation is
23: done with all previous elements ).
24:
25: This is less frequesnt than add() -
26: we want to keep add O(1).
27: */
28: public final class MultiMapNamesEnumeration implements Enumeration {
29: int pos;
30: int size;
31: String next;
32: MultiMap headers;
33:
34: // toString and unique options are not implemented -
35: // we allways to toString and unique.
36:
37: /** Create a new multi-map enumeration.
38: * @param headers the collection to enumerate
39: * @param toString convert each name to string
40: * @param unique return only unique names
41: */
42: MultiMapNamesEnumeration(MultiMap headers, boolean toString,
43: boolean unique) {
44: this .headers = headers;
45: pos = 0;
46: size = headers.size();
47: findNext();
48: }
49:
50: private void findNext() {
51: next = null;
52: for (; pos < size; pos++) {
53: next = headers.getName(pos).toString();
54: for (int j = 0; j < pos; j++) {
55: if (headers.getName(j).equalsIgnoreCase(next)) {
56: // duplicate.
57: next = null;
58: break;
59: }
60: }
61: if (next != null) {
62: // it's not a duplicate
63: break;
64: }
65: }
66: // next time findNext is called it will try the
67: // next element
68: pos++;
69: }
70:
71: public boolean hasMoreElements() {
72: return next != null;
73: }
74:
75: public Object nextElement() {
76: String current = next;
77: findNext();
78: return current;
79: }
80: }
|