import java.text.*;

public class Iterators
{
	public static void testForward1()
	{
		// This is an example of an internal iterator
		ProcessingStringCharacterIterator iterator=new PrintingStringCharacterIterator("Hello, world!");
		
		iterator.process();		
	}
	
	public static void testForward2()
	{
		// This is an example of an internal iterator
		ProcessingStringCharacterIterator iterator=new PrintingStringCharacterIterator("Hello, world!");
		
		iterator.process(new PrintProcessable());
		System.out.println();			
	}
	
	public static void testForward3()
	{
		StringCharacterIterator iterator=new StringCharacterIterator("Hello, world!");	

		// This is an example of an external iterator
		for (char c=iterator.first();c!=CharacterIterator.DONE;c=iterator.next())
			System.out.print(c);
		System.out.println();
	}
	
	public static void testForward4()
	{
		PrintingStringCharacterIterator iterator=new PrintingStringCharacterIterator("Hello, world!");

		// This is an example of an external iterator
		for (char c=iterator.first();c!=CharacterIterator.DONE;c=iterator.next())
			System.out.print(c);
		System.out.println();
	}
	
	public static void testForward()
	{
		testForward1();
		testForward2();
		testForward3();
		testForward4();
	}
	
	public static void testReverse1()
	{
		StringCharacterIterator iterator=new StringCharacterIterator("Hello, world!");	

		// This is an example of an external iterator
		for (char c=iterator.last();c!=CharacterIterator.DONE;c=iterator.previous())
			System.out.print(c);
		System.out.println();
	}
	
	public static void testReverse2()
	{
		PrintingStringCharacterIterator iterator=new PrintingStringCharacterIterator("Hello, world!");

		// This is an example of an external iterator
		for (char c=iterator.last();c!=CharacterIterator.DONE;c=iterator.previous())
			System.out.print(c);
		System.out.println();
	}
	
	public static void testReverse()
	{
		testReverse1();
		testReverse2();
	}
	
	public static boolean testPalindrome1()
	{
		String text="poordanisinadroop";

		// It seems impossible to use two internal iterators at a time
		return new PrintingStringCharacterIterator(text).isPalindrome();
	}
	
	public static boolean testPalindrome2()
	{
		String text="drawalevelaward";
		StringCharacterIterator forwardIterator=new StringCharacterIterator(text);
		StringCharacterIterator reverseIterator=new StringCharacterIterator(text);
		char forwardC;
		char reverseC;
		
		forwardIterator.first();
		reverseIterator.last();
		while ((forwardC=forwardIterator.current())!=CharacterIterator.DONE &&
				(reverseC=reverseIterator.current())!=CharacterIterator.DONE)
		{
			if (forwardC!=reverseC)
				return false;
			forwardIterator.next();
			reverseIterator.previous();
		}
		return true;
	}
	
	public static boolean testPalindrome3()
	{
		DoubleStringCharacterIterator iterator=new DoubleStringCharacterIterator("madamimadam");
		while (iterator.current()!=CharacterIterator.DONE)
		{
			DoubleStringCharacterIterator.CharacterPair pair=iterator.currentPair();
			
			if (pair.left!=pair.right)
				return false;
			iterator.next();
		}
		
		return true;
	}
	
	public static void testPalindrome()
	{
		System.out.println(testPalindrome1());
		System.out.println(testPalindrome2());
		System.out.println(testPalindrome3());
	}
	
	public static void main(String[] args)
	{
		testForward();
		testReverse();
		testPalindrome();
	}
}
