|
| 1 | +/** |
| 2 | + * The MIT License Copyright (c) 2014-2016 Ilkka Seppälä |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 5 | + * associated documentation files (the "Software"), to deal in the Software without restriction, |
| 6 | + * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| 7 | + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 11 | + * substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 14 | + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 15 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 16 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | + */ |
| 19 | +package com.iluwatar.iterator; |
| 20 | + |
| 21 | +import static com.iluwatar.iterator.list.ItemType.ANY; |
| 22 | +import static com.iluwatar.iterator.list.ItemType.POTION; |
| 23 | +import static com.iluwatar.iterator.list.ItemType.RING; |
| 24 | +import static com.iluwatar.iterator.list.ItemType.WEAPON; |
| 25 | + |
| 26 | +import com.iluwatar.iterator.bst.BstIterator; |
| 27 | +import com.iluwatar.iterator.bst.TreeNode; |
| 28 | +import com.iluwatar.iterator.list.ItemType; |
| 29 | +import com.iluwatar.iterator.list.TreasureChest; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * The Iterator pattern is a design pattern in which an iterator is used to traverse a container and |
| 35 | + * access the container's elements. The Iterator pattern decouples algorithms from containers. |
| 36 | + * <p> |
| 37 | + * In this example the Iterator ({@link Iterator}) adds abstraction layer on top of a collection |
| 38 | + * ({@link TreasureChest}). This way the collection can change its internal implementation without |
| 39 | + * affecting its clients. |
| 40 | + */ |
| 41 | +public class App { |
| 42 | + |
| 43 | + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |
| 44 | + |
| 45 | + private static final TreasureChest TREASURE_CHEST = new TreasureChest(); |
| 46 | + |
| 47 | + private static void demonstrateTreasureChestIteratorForType(ItemType itemType) { |
| 48 | + LOGGER.info("------------------------"); |
| 49 | + LOGGER.info("Item Iterator for ItemType " + itemType + ": "); |
| 50 | + Iterator itemIterator = TREASURE_CHEST.iterator(itemType); |
| 51 | + while (itemIterator.hasNext()) { |
| 52 | + LOGGER.info(itemIterator.next().toString()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static void demonstrateBstIterator() { |
| 57 | + LOGGER.info("------------------------"); |
| 58 | + LOGGER.info("BST Iterator: "); |
| 59 | + TreeNode<Integer> root = buildIntegerBst(); |
| 60 | + BstIterator bstIterator = new BstIterator<>(root); |
| 61 | + while (bstIterator.hasNext()) { |
| 62 | + LOGGER.info("Next node: " + bstIterator.next().getVal()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static TreeNode<Integer> buildIntegerBst() { |
| 67 | + TreeNode<Integer> root = new TreeNode<>(8); |
| 68 | + |
| 69 | + root.insert(3); |
| 70 | + root.insert(10); |
| 71 | + root.insert(1); |
| 72 | + root.insert(6); |
| 73 | + root.insert(14); |
| 74 | + root.insert(4); |
| 75 | + root.insert(7); |
| 76 | + root.insert(13); |
| 77 | + |
| 78 | + return root; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Program entry point |
| 83 | + * |
| 84 | + * @param args command line args |
| 85 | + */ |
| 86 | + public static void main(String[] args) { |
| 87 | + demonstrateTreasureChestIteratorForType(RING); |
| 88 | + demonstrateTreasureChestIteratorForType(POTION); |
| 89 | + demonstrateTreasureChestIteratorForType(WEAPON); |
| 90 | + demonstrateTreasureChestIteratorForType(ANY); |
| 91 | + |
| 92 | + demonstrateBstIterator(); |
| 93 | + } |
| 94 | +} |
0 commit comments