public enum ItemType { ANY, WEAPON, RING, POTION
}
public interface Iterator<T> {
boolean hasNext();
T next(); }
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 final void setType(ItemType type) { this.type = type; } }
public class TreasureChest {
private List<Item> items;
public TreasureChest() { items = List.of( new Item(ItemType.POTION, "Potion of courage"), new Item(ItemType.RING, "Ring of shadows"), new Item(ItemType.POTION, "Potion of wisdom"), new Item(ItemType.POTION, "Potion of blood"), new Item(ItemType.WEAPON, "Sword of silver +1"), new Item(ItemType.POTION, "Potion of rust"), new Item(ItemType.POTION, "Potion of healing"), new Item(ItemType.RING, "Ring of armor"), new Item(ItemType.WEAPON, "Steel halberd"), new Item(ItemType.WEAPON, "Dagger of poison")); }
public Iterator<Item> iterator(ItemType itemType) { return new TreasureChestItemIterator(this, itemType); }
public List<Item> getItems() { return new ArrayList<>(items); }
}
public class TreasureChestItemIterator implements Iterator<Item> {
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() { var items = chest.getItems(); var tempIdx = idx; while (true) { tempIdx++; if (tempIdx >= items.size()) { tempIdx = -1; break; } if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) { break; } } return tempIdx; } }
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private static final TreasureChest TREASURE_CHEST = new TreasureChest();
private static void demonstrateTreasureChestIteratorForType(ItemType itemType) { LOGGER.info("------------------------"); LOGGER.info("Item Iterator for ItemType " + itemType + ": "); var itemIterator = TREASURE_CHEST.iterator(itemType); while (itemIterator.hasNext()) { LOGGER.info(itemIterator.next().toString()); } }
private static void demonstrateBstIterator() { LOGGER.info("------------------------"); LOGGER.info("BST Iterator: "); var root = buildIntegerBst(); var bstIterator = new BstIterator<Integer>(root); while (bstIterator.hasNext()) { LOGGER.info("Next node: " + bstIterator.next().getVal()); } }
private static TreeNode<Integer> buildIntegerBst() { var root = new TreeNode<>(8);
root.insert(3); root.insert(10); root.insert(1); root.insert(6); root.insert(14); root.insert(4); root.insert(7); root.insert(13);
return root; }
public static void main(String[] args) { demonstrateTreasureChestIteratorForType(RING); demonstrateTreasureChestIteratorForType(POTION); demonstrateTreasureChestIteratorForType(WEAPON); demonstrateTreasureChestIteratorForType(ANY);
demonstrateBstIterator(); } }
|