001package ezvcard.util; 002 003/* 004Copyright (c) 2012-2026, Michael Angstadt 005All rights reserved. 006 007Redistribution and use in source and binary forms, with or without 008modification, are permitted provided that the following conditions are met: 009 0101. Redistributions of source code must retain the above copyright notice, this 011list of conditions and the following disclaimer. 0122. Redistributions in binary form must reproduce the above copyright notice, 013this list of conditions and the following disclaimer in the documentation 014and/or other materials provided with the distribution. 015 016THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 017ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 018WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 019DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 020ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 021(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 022LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 023ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 024(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 025SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 026 027The views and conclusions contained in the software and documentation are those 028of the authors and should not be interpreted as representing official policies, 029either expressed or implied, of the FreeBSD Project. 030*/ 031 032/** 033 * Iterates over the characters in a String. Provides additional functionality 034 * over {@link String#chars}. 035 * @author Michael Angstadt 036 */ 037public class CharIterator { 038 private final String s; 039 private int i; 040 041 /** 042 * @param s the string to iterate over 043 */ 044 public CharIterator(String s) { 045 this(s, 0); 046 } 047 048 /** 049 * @param s the string to iterate over 050 * @param startIndex the index to start at 051 */ 052 public CharIterator(String s, int startIndex) { 053 this.s = s; 054 i = startIndex - 1; 055 } 056 057 /** 058 * Determines if there are more characters to iterate over. 059 * @return true if there are more characters, false if not 060 */ 061 public boolean hasNext() { 062 return i + 1 < s.length(); 063 } 064 065 /** 066 * Advances to the next character. 067 * @return the next character 068 */ 069 public char next() { 070 return s.charAt(++i); 071 } 072 073 /** 074 * Gets the previous character. 075 * @return the previous character or 0 if the iterator is at the beginning 076 * of the string 077 */ 078 public char prev() { 079 return (i <= 0) ? 0 : s.charAt(i - 1); 080 } 081 082 /** 083 * Gets the index of the current character in the string. 084 * @return the index 085 */ 086 public int index() { 087 return i; 088 } 089}