#107 Iterator example JavaDoc
This commit is contained in:
		| @@ -1,41 +1,45 @@ | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Iterator (ItemIterator) adds abstraction layer on top of a collection | ||||
|  * (TreasureChest). This way the collection can change its internal | ||||
|  * implementation without affecting its clients. | ||||
|  *  | ||||
|  */ | ||||
| public class App { | ||||
|  | ||||
| 	public static void main(String[] args) { | ||||
| 		TreasureChest chest = new TreasureChest(); | ||||
|  | ||||
| 		ItemIterator ringIterator = chest.Iterator(ItemType.RING); | ||||
| 		while (ringIterator.hasNext()) { | ||||
| 			System.out.println(ringIterator.next()); | ||||
| 		} | ||||
|  | ||||
| 		System.out.println("----------"); | ||||
|  | ||||
| 		ItemIterator potionIterator = chest.Iterator(ItemType.POTION); | ||||
| 		while (potionIterator.hasNext()) { | ||||
| 			System.out.println(potionIterator.next()); | ||||
| 		} | ||||
|  | ||||
| 		System.out.println("----------"); | ||||
|  | ||||
| 		ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON); | ||||
| 		while (weaponIterator.hasNext()) { | ||||
| 			System.out.println(weaponIterator.next()); | ||||
| 		} | ||||
|  | ||||
| 		System.out.println("----------"); | ||||
|  | ||||
| 		ItemIterator it = chest.Iterator(ItemType.ANY); | ||||
| 		while (it.hasNext()) { | ||||
| 			System.out.println(it.next()); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Iterator ({@link ItemIterator}) adds abstraction layer on top of a collection | ||||
|  * ({@link TreasureChest}). This way the collection can change its internal | ||||
|  * implementation without affecting its clients. | ||||
|  *  | ||||
|  */ | ||||
| public class App { | ||||
|  | ||||
| 	/** | ||||
| 	 * Program entry point | ||||
| 	 * @param args command line args | ||||
| 	 */ | ||||
| 	public static void main(String[] args) { | ||||
| 		TreasureChest chest = new TreasureChest(); | ||||
|  | ||||
| 		ItemIterator ringIterator = chest.Iterator(ItemType.RING); | ||||
| 		while (ringIterator.hasNext()) { | ||||
| 			System.out.println(ringIterator.next()); | ||||
| 		} | ||||
|  | ||||
| 		System.out.println("----------"); | ||||
|  | ||||
| 		ItemIterator potionIterator = chest.Iterator(ItemType.POTION); | ||||
| 		while (potionIterator.hasNext()) { | ||||
| 			System.out.println(potionIterator.next()); | ||||
| 		} | ||||
|  | ||||
| 		System.out.println("----------"); | ||||
|  | ||||
| 		ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON); | ||||
| 		while (weaponIterator.hasNext()) { | ||||
| 			System.out.println(weaponIterator.next()); | ||||
| 		} | ||||
|  | ||||
| 		System.out.println("----------"); | ||||
|  | ||||
| 		ItemIterator it = chest.Iterator(ItemType.ANY); | ||||
| 		while (it.hasNext()) { | ||||
| 			System.out.println(it.next()); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,25 +1,30 @@ | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| public class Item { | ||||
|  | ||||
| 	private ItemType type; | ||||
| 	private String name; | ||||
|  | ||||
| 	public Item(ItemType type, String name) { | ||||
| 		this.setType(type); | ||||
| 		this.name = name; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return name; | ||||
| 	} | ||||
|  | ||||
| 	public ItemType getType() { | ||||
| 		return type; | ||||
| 	} | ||||
|  | ||||
| 	public void setType(ItemType type) { | ||||
| 		this.type = type; | ||||
| 	} | ||||
| } | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Item | ||||
|  * | ||||
|  */ | ||||
| public class Item { | ||||
|  | ||||
| 	private ItemType type; | ||||
| 	private String name; | ||||
|  | ||||
| 	public Item(ItemType type, String name) { | ||||
| 		this.setType(type); | ||||
| 		this.name = name; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		return name; | ||||
| 	} | ||||
|  | ||||
| 	public ItemType getType() { | ||||
| 		return type; | ||||
| 	} | ||||
|  | ||||
| 	public void setType(ItemType type) { | ||||
| 		this.type = type; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,13 +1,13 @@ | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Iterator interface. | ||||
|  *  | ||||
|  */ | ||||
| public interface ItemIterator { | ||||
|  | ||||
| 	boolean hasNext(); | ||||
|  | ||||
| 	Item next(); | ||||
| } | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * ItemIterator interface. | ||||
|  *  | ||||
|  */ | ||||
| public interface ItemIterator { | ||||
|  | ||||
| 	boolean hasNext(); | ||||
|  | ||||
| 	Item next(); | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,12 @@ | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| public enum ItemType { | ||||
|  | ||||
| 	ANY, WEAPON, RING, POTION | ||||
|  | ||||
| } | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * ItemType enumeration | ||||
|  * | ||||
|  */ | ||||
| public enum ItemType { | ||||
|  | ||||
| 	ANY, WEAPON, RING, POTION | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,39 +1,39 @@ | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Collection class. | ||||
|  *  | ||||
|  */ | ||||
| public class TreasureChest { | ||||
|  | ||||
| 	private List<Item> items; | ||||
|  | ||||
| 	public TreasureChest() { | ||||
| 		items = new ArrayList<>(); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of courage")); | ||||
| 		items.add(new Item(ItemType.RING, "Ring of shadows")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of wisdom")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of blood")); | ||||
| 		items.add(new Item(ItemType.WEAPON, "Sword of silver +1")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of rust")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of healing")); | ||||
| 		items.add(new Item(ItemType.RING, "Ring of armor")); | ||||
| 		items.add(new Item(ItemType.WEAPON, "Steel halberd")); | ||||
| 		items.add(new Item(ItemType.WEAPON, "Dagger of poison")); | ||||
| 	} | ||||
|  | ||||
| 	ItemIterator Iterator(ItemType type) { | ||||
| 		return new TreasureChestItemIterator(this, type); | ||||
| 	} | ||||
|  | ||||
| 	public List<Item> getItems() { | ||||
| 		ArrayList<Item> list = new ArrayList<>(); | ||||
| 		list.addAll(items); | ||||
| 		return list; | ||||
| 	} | ||||
|  | ||||
| } | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * TreasureChest, the collection class. | ||||
|  *  | ||||
|  */ | ||||
| public class TreasureChest { | ||||
|  | ||||
| 	private List<Item> items; | ||||
|  | ||||
| 	public TreasureChest() { | ||||
| 		items = new ArrayList<>(); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of courage")); | ||||
| 		items.add(new Item(ItemType.RING, "Ring of shadows")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of wisdom")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of blood")); | ||||
| 		items.add(new Item(ItemType.WEAPON, "Sword of silver +1")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of rust")); | ||||
| 		items.add(new Item(ItemType.POTION, "Potion of healing")); | ||||
| 		items.add(new Item(ItemType.RING, "Ring of armor")); | ||||
| 		items.add(new Item(ItemType.WEAPON, "Steel halberd")); | ||||
| 		items.add(new Item(ItemType.WEAPON, "Dagger of poison")); | ||||
| 	} | ||||
|  | ||||
| 	ItemIterator Iterator(ItemType type) { | ||||
| 		return new TreasureChestItemIterator(this, type); | ||||
| 	} | ||||
|  | ||||
| 	public List<Item> getItems() { | ||||
| 		ArrayList<Item> list = new ArrayList<>(); | ||||
| 		list.addAll(items); | ||||
| 		return list; | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,49 +1,54 @@ | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| public class TreasureChestItemIterator implements ItemIterator { | ||||
|  | ||||
| 	private TreasureChest chest; | ||||
| 	private int idx; | ||||
| 	private ItemType type; | ||||
|  | ||||
| 	public TreasureChestItemIterator(TreasureChest chest, ItemType type) { | ||||
| 		this.chest = chest; | ||||
| 		this.type = type; | ||||
| 		this.idx = -1; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean hasNext() { | ||||
| 		return findNextIdx() != -1; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Item next() { | ||||
| 		idx = findNextIdx(); | ||||
| 		if (idx != -1) { | ||||
| 			return chest.getItems().get(idx); | ||||
| 		} | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| 	private int findNextIdx() { | ||||
|  | ||||
| 		List<Item> items = chest.getItems(); | ||||
| 		boolean found = false; | ||||
| 		int tempIdx = idx; | ||||
| 		while (!found) { | ||||
| 			tempIdx++; | ||||
| 			if (tempIdx >= items.size()) { | ||||
| 				tempIdx = -1; | ||||
| 				break; | ||||
| 			} | ||||
| 			if (type.equals(ItemType.ANY) | ||||
| 					|| items.get(tempIdx).getType().equals(type)) { | ||||
| 				break; | ||||
| 			} | ||||
| 		} | ||||
| 		return tempIdx; | ||||
| 	} | ||||
| } | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * TreasureChestItemIterator | ||||
|  * | ||||
|  */ | ||||
| public class TreasureChestItemIterator implements ItemIterator { | ||||
|  | ||||
| 	private TreasureChest chest; | ||||
| 	private int idx; | ||||
| 	private ItemType type; | ||||
|  | ||||
| 	public TreasureChestItemIterator(TreasureChest chest, ItemType type) { | ||||
| 		this.chest = chest; | ||||
| 		this.type = type; | ||||
| 		this.idx = -1; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public boolean hasNext() { | ||||
| 		return findNextIdx() != -1; | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Item next() { | ||||
| 		idx = findNextIdx(); | ||||
| 		if (idx != -1) { | ||||
| 			return chest.getItems().get(idx); | ||||
| 		} | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| 	private int findNextIdx() { | ||||
|  | ||||
| 		List<Item> items = chest.getItems(); | ||||
| 		boolean found = false; | ||||
| 		int tempIdx = idx; | ||||
| 		while (!found) { | ||||
| 			tempIdx++; | ||||
| 			if (tempIdx >= items.size()) { | ||||
| 				tempIdx = -1; | ||||
| 				break; | ||||
| 			} | ||||
| 			if (type.equals(ItemType.ANY) | ||||
| 					|| items.get(tempIdx).getType().equals(type)) { | ||||
| 				break; | ||||
| 			} | ||||
| 		} | ||||
| 		return tempIdx; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,14 +1,19 @@ | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| import org.junit.Test; | ||||
|  | ||||
| import com.iluwatar.iterator.App; | ||||
|  | ||||
| public class AppTest { | ||||
|  | ||||
| 	@Test | ||||
| 	public void test() { | ||||
| 		String[] args = {}; | ||||
| 		App.main(args); | ||||
| 	} | ||||
| } | ||||
| package com.iluwatar.iterator; | ||||
|  | ||||
| import org.junit.Test; | ||||
|  | ||||
| import com.iluwatar.iterator.App; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Application test | ||||
|  * | ||||
|  */ | ||||
| public class AppTest { | ||||
|  | ||||
| 	@Test | ||||
| 	public void test() { | ||||
| 		String[] args = {}; | ||||
| 		App.main(args); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user