mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 14:59:25 +00:00
Refactored App.java to remove duplicate code and elegantly demonstrate each implementation of the Iterator interface. Removed the redundant ItemIterator interface. Added insert() method to TreeNode class to allow for more elegant construction of BSTs.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator;
|
||||
|
||||
import static com.iluwatar.iterator.list.ItemType.ANY;
|
||||
import static com.iluwatar.iterator.list.ItemType.POTION;
|
||||
import static com.iluwatar.iterator.list.ItemType.RING;
|
||||
import static com.iluwatar.iterator.list.ItemType.WEAPON;
|
||||
|
||||
import com.iluwatar.iterator.bst.BstIterator;
|
||||
import com.iluwatar.iterator.bst.TreeNode;
|
||||
import com.iluwatar.iterator.list.ItemType;
|
||||
import com.iluwatar.iterator.list.TreasureChest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The Iterator pattern is a design pattern in which an iterator is used to traverse a container and
|
||||
* access the container's elements. The Iterator pattern decouples algorithms from containers.
|
||||
* <p>
|
||||
* In this example the Iterator ({@link Iterator}) 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 {
|
||||
|
||||
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 + ": ");
|
||||
Iterator itemIterator = TREASURE_CHEST.iterator(itemType);
|
||||
while (itemIterator.hasNext()) {
|
||||
LOGGER.info(itemIterator.next().toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static void demonstrateBstIterator() {
|
||||
LOGGER.info("------------------------");
|
||||
LOGGER.info("BST Iterator: ");
|
||||
TreeNode<Integer> root = buildIntegerBst();
|
||||
BstIterator bstIterator = new BstIterator<>(root);
|
||||
while (bstIterator.hasNext()) {
|
||||
LOGGER.info("Next node: " + bstIterator.next().getVal());
|
||||
}
|
||||
}
|
||||
|
||||
private static TreeNode<Integer> buildIntegerBst() {
|
||||
TreeNode<Integer> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
demonstrateTreasureChestIteratorForType(RING);
|
||||
demonstrateTreasureChestIteratorForType(POTION);
|
||||
demonstrateTreasureChestIteratorForType(WEAPON);
|
||||
demonstrateTreasureChestIteratorForType(ANY);
|
||||
|
||||
demonstrateBstIterator();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.interfaces;
|
||||
package com.iluwatar.iterator;
|
||||
|
||||
/**
|
||||
* Iterator interface to be implemented by iterators over various data structures
|
||||
@@ -1,78 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.binarysearchtree;
|
||||
|
||||
import com.iluwatar.iterator.interfaces.Iterator;
|
||||
import java.util.ArrayDeque;
|
||||
|
||||
/**
|
||||
* This BstIterator iterates IN order. For example, given a BST with Integer values,
|
||||
* expect to retrieve TreeNodes according to the Integer's natural ordering (1, 2, 3...)
|
||||
* @param <T> This Iterator has been implemented with generic typing to allow traversal of TreeNodes of various types
|
||||
*/
|
||||
public class BstIterator<T> implements Iterator {
|
||||
|
||||
private ArrayDeque<TreeNode<T>> pathStack;
|
||||
|
||||
public BstIterator(TreeNode<T> root) {
|
||||
pathStack = new ArrayDeque<>();
|
||||
pushPathToNextSmallest(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* This BstIterator manages to use O(h) extra space, where h is the height of the tree
|
||||
* It achieves this by maintaining a stack of the nodes to handle (pushing all left nodes first),
|
||||
* before handling self or right node
|
||||
* @param node TreeNode that acts as root of the subtree we're interested in.
|
||||
*/
|
||||
private void pushPathToNextSmallest(TreeNode<T> node) {
|
||||
while (node != null) {
|
||||
pathStack.push(node);
|
||||
node = node.getLeft();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this iterator has a "next" element
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !pathStack.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return TreeNode next. The next element according to our in-order traversal of the given BST
|
||||
* @throws IllegalStateException if this iterator does not have a next element
|
||||
*/
|
||||
@Override
|
||||
public TreeNode<T> next() throws IllegalStateException {
|
||||
if (pathStack.isEmpty()) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
TreeNode<T> next = pathStack.pop();
|
||||
pushPathToNextSmallest(next.getRight());
|
||||
return next;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.binarysearchtree;
|
||||
|
||||
/**
|
||||
* This TreeNode class allows for a generically typed value.
|
||||
* @param <T> generically typed to accept various data types for the val property
|
||||
*/
|
||||
public class TreeNode<T> {
|
||||
|
||||
private T val;
|
||||
private TreeNode<T> left;
|
||||
private TreeNode<T> right;
|
||||
|
||||
/**
|
||||
* Creates a TreeNode with a given value, and null children
|
||||
* @param val The value of the given node
|
||||
*/
|
||||
public TreeNode(T val) {
|
||||
this.val = val;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
T getVal() {
|
||||
return val;
|
||||
}
|
||||
|
||||
TreeNode<T> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
void setLeft(TreeNode<T> left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
TreeNode<T> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
void setRight(TreeNode<T> right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return val.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.bst;
|
||||
|
||||
import com.iluwatar.iterator.Iterator;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* An in-order implementation of a BST Iterator. For example, given a BST with Integer values,
|
||||
* expect to retrieve TreeNodes according to the Integer's natural ordering (1, 2, 3...)
|
||||
*
|
||||
* @param <T> This Iterator has been implemented with generic typing to allow for TreeNodes of
|
||||
* different value types
|
||||
*/
|
||||
public class BstIterator<T extends Comparable<T>> implements Iterator<TreeNode<T>> {
|
||||
|
||||
private ArrayDeque<TreeNode<T>> pathStack;
|
||||
|
||||
public BstIterator(TreeNode<T> root) {
|
||||
pathStack = new ArrayDeque<>();
|
||||
pushPathToNextSmallest(root);
|
||||
}
|
||||
|
||||
/**
|
||||
* This BstIterator manages to use O(h) extra space, where h is the height of the tree It achieves
|
||||
* this by maintaining a stack of the nodes to handle (pushing all left nodes first), before
|
||||
* handling self or right node
|
||||
*
|
||||
* @param node TreeNode that acts as root of the subtree we're interested in.
|
||||
*/
|
||||
private void pushPathToNextSmallest(TreeNode<T> node) {
|
||||
while (node != null) {
|
||||
pathStack.push(node);
|
||||
node = node.getLeft();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this iterator has a "next" element
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !pathStack.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TreeNode next. The next element according to our in-order traversal of the given BST
|
||||
* @throws NoSuchElementException if this iterator does not have a next element
|
||||
*/
|
||||
@Override
|
||||
public TreeNode<T> next() throws NoSuchElementException {
|
||||
if (pathStack.isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
TreeNode<T> next = pathStack.pop();
|
||||
pushPathToNextSmallest(next.getRight());
|
||||
return next;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -9,7 +9,7 @@ this iterator will return nodes according to "In Order" binary tree traversal.
|
||||
This means that given a binary search tree like the following, the iterator would
|
||||
return values in order: 1, 3, 4, 6, 7, 8, 10, 13, 14.
|
||||
|
||||

|
||||

|
||||
|
||||
### How It's Done
|
||||
**The trivial solution** to a binary search tree iterator would be to construct a List (or similar
|
||||
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.bst;
|
||||
|
||||
/**
|
||||
* TreeNode Class, representing one node in a Binary Search Tree. Allows for a generically typed
|
||||
* value.
|
||||
*
|
||||
* @param <T> generically typed to accept various data types for the val property
|
||||
*/
|
||||
public class TreeNode<T extends Comparable<T>> {
|
||||
|
||||
private T val;
|
||||
private TreeNode<T> left;
|
||||
private TreeNode<T> right;
|
||||
|
||||
/**
|
||||
* Creates a TreeNode with a given value, and null children
|
||||
*
|
||||
* @param val The value of the given node
|
||||
*/
|
||||
public TreeNode(T val) {
|
||||
this.val = val;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
|
||||
public T getVal() {
|
||||
return val;
|
||||
}
|
||||
|
||||
public TreeNode<T> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
private void setLeft(TreeNode<T> left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public TreeNode<T> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
private void setRight(TreeNode<T> right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts new TreeNode based on a given value into the subtree represented by self
|
||||
*
|
||||
* @param valToInsert The value to insert as a new TreeNode
|
||||
*/
|
||||
public void insert(T valToInsert) {
|
||||
TreeNode<T> parent = getParentNodeOfValueToBeInserted(valToInsert);
|
||||
parent.insertNewChild(valToInsert);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the Parent TreeNode for a given value to insert into the BST.
|
||||
*
|
||||
* @param valToInsert Value of the new TreeNode to be inserted
|
||||
* @return Parent TreeNode of `valToInsert`
|
||||
*/
|
||||
private TreeNode<T> getParentNodeOfValueToBeInserted(T valToInsert) {
|
||||
TreeNode<T> parent = null;
|
||||
TreeNode<T> curr = this;
|
||||
|
||||
while (curr != null) {
|
||||
parent = curr;
|
||||
curr = curr.traverseOneLevelDown(valToInsert);
|
||||
}
|
||||
|
||||
return parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns left or right child of self based on a value that would be inserted; maintaining the
|
||||
* integrity of the BST.
|
||||
*
|
||||
* @param value The value of the TreeNode that would be inserted beneath self
|
||||
* @return The child TreeNode of self which represents the subtree where `value` would be inserted
|
||||
*/
|
||||
private TreeNode<T> traverseOneLevelDown(T value) {
|
||||
if (this.isGreaterThan(value)) {
|
||||
return this.left;
|
||||
}
|
||||
return this.right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new Child TreeNode of given value to self. WARNING: This method is destructive (will
|
||||
* overwrite existing tree structure, if any), and should be called only by this class's insert()
|
||||
* method.
|
||||
*
|
||||
* @param valToInsert Value of the new TreeNode to be inserted
|
||||
*/
|
||||
private void insertNewChild(T valToInsert) {
|
||||
if (this.isLessThanOrEqualTo(valToInsert)) {
|
||||
this.setRight(new TreeNode<>(valToInsert));
|
||||
} else {
|
||||
this.setLeft(new TreeNode<>(valToInsert));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isGreaterThan(T val) {
|
||||
return this.val.compareTo(val) > 0;
|
||||
}
|
||||
|
||||
private boolean isLessThanOrEqualTo(T val) {
|
||||
return this.val.compareTo(val) < 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return val.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.list;
|
||||
|
||||
import com.iluwatar.iterator.interfaces.Iterator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Iterator pattern is a design pattern in which an iterator is used to traverse a container and
|
||||
* access the container's elements. The Iterator pattern decouples algorithms from containers.
|
||||
* <p>
|
||||
* In this example the Iterator ({@link Iterator}) 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 {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* 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()) {
|
||||
LOGGER.info(ringIterator.next().toString());
|
||||
}
|
||||
|
||||
LOGGER.info("----------");
|
||||
|
||||
ItemIterator potionIterator = chest.iterator(ItemType.POTION);
|
||||
while (potionIterator.hasNext()) {
|
||||
LOGGER.info(potionIterator.next().toString());
|
||||
}
|
||||
|
||||
LOGGER.info("----------");
|
||||
|
||||
ItemIterator weaponIterator = chest.iterator(ItemType.WEAPON);
|
||||
while (weaponIterator.hasNext()) {
|
||||
LOGGER.info(weaponIterator.next().toString());
|
||||
}
|
||||
|
||||
LOGGER.info("----------");
|
||||
|
||||
ItemIterator it = chest.iterator(ItemType.ANY);
|
||||
while (it.hasNext()) {
|
||||
LOGGER.info(it.next().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.list;
|
||||
|
||||
/**
|
||||
*
|
||||
* ItemIterator interface.
|
||||
*
|
||||
*/
|
||||
public interface ItemIterator {
|
||||
|
||||
boolean hasNext();
|
||||
|
||||
Item next();
|
||||
}
|
||||
@@ -1,24 +1,20 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.list;
|
||||
|
||||
@@ -26,9 +22,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* TreasureChest, the collection class.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class TreasureChest {
|
||||
|
||||
@@ -51,7 +47,7 @@ public class TreasureChest {
|
||||
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
|
||||
}
|
||||
|
||||
ItemIterator iterator(ItemType itemType) {
|
||||
public TreasureChestItemIterator iterator(ItemType itemType) {
|
||||
return new TreasureChestItemIterator(this, itemType);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +1,32 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
* The MIT License Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
* associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
||||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.iterator.list;
|
||||
|
||||
import com.iluwatar.iterator.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* TreasureChestItemIterator
|
||||
*
|
||||
*/
|
||||
public class TreasureChestItemIterator implements ItemIterator {
|
||||
public class TreasureChestItemIterator implements Iterator {
|
||||
|
||||
private TreasureChest chest;
|
||||
private int idx;
|
||||
@@ -59,7 +56,6 @@ public class TreasureChestItemIterator implements ItemIterator {
|
||||
}
|
||||
|
||||
private int findNextIdx() {
|
||||
|
||||
List<Item> items = chest.getItems();
|
||||
boolean found = false;
|
||||
int tempIdx = idx;
|
||||
|
||||
Reference in New Issue
Block a user