]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/util/UResourceBundleIterator.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / util / UResourceBundleIterator.java
1 /*\r
2 ******************************************************************************\r
3 * Copyright (C) 2004-2009, International Business Machines Corporation and   *\r
4 * others. All Rights Reserved.                                               *\r
5 ******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.util;\r
9 \r
10 import java.util.NoSuchElementException;\r
11 \r
12 /**\r
13  * <p>Class for enabling iteration over UResourceBundle objects.\r
14  * Example of use:<br>\r
15  * <pre>\r
16  * ICUResourceBundleIterator iterator = resB.getIterator();\r
17  * ICUResourceBundle temp;\r
18  * while (iterator.hasNext()) {\r
19  *    temp = iterartor.next();  \r
20  *    int type = temp.getType();\r
21  *    switch(type){\r
22  *      case UResourceBundle.STRING:\r
23  *          str = temp.getString();\r
24  *          break;\r
25  *      case UResourceBundle.INT:\r
26  *          integer = temp.getInt();\r
27  *          break;\r
28  *     .....\r
29  *    }\r
30  *   // do something interesting with data collected\r
31  * }\r
32  * </pre>\r
33  * @author ram\r
34  * @stable ICU 3.8\r
35  */\r
36 public class UResourceBundleIterator{\r
37     private UResourceBundle bundle;\r
38     private int index = 0;\r
39     private int size = 0;\r
40     /**\r
41      * Construct a resource bundle iterator for the\r
42      * given resource bundle\r
43      * \r
44      * @param bndl The resource bundle to iterate over\r
45      * @stable ICU 3.8\r
46      */\r
47     public UResourceBundleIterator(UResourceBundle bndl){\r
48         bundle = bndl;   \r
49         size = bundle.getSize();\r
50     }\r
51 \r
52     /**\r
53      * Returns the next element of this iterator if this iterator object has at least one more element to provide\r
54      * @return the UResourceBundle object\r
55      * @throws NoSuchElementException If there does not exist such an element.\r
56      * @stable ICU 3.8\r
57      */\r
58     public UResourceBundle next()throws NoSuchElementException{\r
59         if(index<size){\r
60             return bundle.get(index++);\r
61         }\r
62         throw new NoSuchElementException();\r
63     }\r
64     /**\r
65      * Returns the next String of this iterator if this iterator object has at least one more element to provide\r
66      * @return the UResourceBundle object\r
67      * @throws NoSuchElementException If there does not exist such an element.\r
68      * @throws UResourceTypeMismatchException If resource has a type mismatch.\r
69      * @stable ICU 3.8\r
70      */\r
71     public String nextString()throws NoSuchElementException, UResourceTypeMismatchException{\r
72         if(index<size){\r
73             return bundle.getString(index++);\r
74         }  \r
75         throw new NoSuchElementException();\r
76     }\r
77     \r
78     /**\r
79      * Resets the internal context of a resource so that iteration starts from the first element.\r
80      * @stable ICU 3.8\r
81      */\r
82     public void reset(){\r
83         //reset the internal context   \r
84         index = 0;\r
85     }\r
86     \r
87     /**\r
88      * Checks whether the given resource has another element to iterate over.\r
89      * @return TRUE if there are more elements, FALSE if there is no more elements\r
90      * @stable ICU 3.8\r
91      */\r
92     public boolean hasNext(){\r
93         return index < size;   \r
94     }\r
95 }