KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > iterator > MapIterator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  * $Header:$
17  */

18 package org.apache.beehive.netui.util.iterator;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23
24 import org.apache.beehive.netui.util.Bundle;
25
26 /**
27  * This class implements the {@link Iterator} interface for accessing the set of
28  * values stored in a {@link Map}.
29  */

30 public class MapIterator
31     implements Iterator JavaDoc {
32
33     /**
34      * An iterator for the map's values.
35      */

36     private Iterator JavaDoc _mapIterator = null;
37
38     /**
39      * Create the {@link Iterator} for the given {@link Map}
40      * @param map
41      */

42     public MapIterator(Map JavaDoc map) {
43         if(map == null)
44             return;
45
46         _mapIterator = map.values().iterator();
47     }
48
49     /**
50      * Advance to the next value in the {@link Map}.
51      *
52      * @return <code>true</code> if there is a next item; <code>false</code> otherwise
53      */

54     public boolean hasNext() {
55         if(_mapIterator == null)
56             return false;
57         else return _mapIterator.hasNext();
58     }
59
60     /**
61      * Advance to the next item in the {@link Map}
62      *
63      * @return the next item
64      * @throws NoSuchElementException if the map has no more elements
65      */

66     public Object JavaDoc next() {
67         if(_mapIterator == null)
68             throw new NoSuchElementException JavaDoc(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
69         else return _mapIterator.next();
70     }
71
72     /**
73      * Remove the current item in the iterator.
74      */

75     public void remove() {
76         if(_mapIterator == null)
77             throw new UnsupportedOperationException JavaDoc(Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported", new Object JavaDoc[]{this.getClass().getName()}));
78         else _mapIterator.remove();
79     }
80 }
81
Popular Tags