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