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