KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > collections > MultiMapNamesEnumeration


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.util.collections;
19
20 import java.util.Enumeration JavaDoc;
21
22 /** Enumerate the distinct header names.
23     Each nextElement() is O(n) ( a comparation is
24     done with all previous elements ).
25
26     This is less frequesnt than add() -
27     we want to keep add O(1).
28 */

29 public final class MultiMapNamesEnumeration implements Enumeration JavaDoc {
30     int pos;
31     int size;
32     String JavaDoc next;
33     MultiMap headers;
34
35     // toString and unique options are not implemented -
36
// we allways to toString and unique.
37

38     /** Create a new multi-map enumeration.
39      * @param headers the collection to enumerate
40      * @param toString convert each name to string
41      * @param unique return only unique names
42      */

43     MultiMapNamesEnumeration(MultiMap headers, boolean toString,
44                  boolean unique) {
45     this.headers=headers;
46     pos=0;
47     size = headers.size();
48     findNext();
49     }
50
51     private void findNext() {
52     next=null;
53     for( ; pos< size; pos++ ) {
54         next=headers.getName( pos ).toString();
55         for( int j=0; j<pos ; j++ ) {
56         if( headers.getName( j ).equalsIgnoreCase( next )) {
57             // duplicate.
58
next=null;
59             break;
60         }
61         }
62         if( next!=null ) {
63         // it's not a duplicate
64
break;
65         }
66     }
67     // next time findNext is called it will try the
68
// next element
69
pos++;
70     }
71     
72     public boolean hasMoreElements() {
73     return next!=null;
74     }
75
76     public Object JavaDoc nextElement() {
77     String JavaDoc current=next;
78     findNext();
79     return current;
80     }
81 }
82
Popular Tags