Skip to content

Commit 91ed79e

Browse files
committed
added iterator sample
1 parent 71d7c87 commit 91ed79e

File tree

8 files changed

+184
-0
lines changed

8 files changed

+184
-0
lines changed

iterator/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>iterator</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>iterator</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
TreasureChest chest = new TreasureChest();
8+
9+
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
10+
while (ringIterator.hasNext()) {
11+
System.out.println(ringIterator.next());
12+
}
13+
14+
System.out.println("----------");
15+
16+
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
17+
while (potionIterator.hasNext()) {
18+
System.out.println(potionIterator.next());
19+
}
20+
21+
System.out.println("----------");
22+
23+
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
24+
while (weaponIterator.hasNext()) {
25+
System.out.println(weaponIterator.next());
26+
}
27+
28+
System.out.println("----------");
29+
30+
ItemIterator it = chest.Iterator(ItemType.ANY);
31+
while (it.hasNext()) {
32+
System.out.println(it.next());
33+
}
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.iluwatar;
2+
3+
public class Item {
4+
5+
private ItemType type;
6+
private String name;
7+
8+
public Item(ItemType type, String name) {
9+
this.setType(type);
10+
this.name = name;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return name;
16+
}
17+
18+
public ItemType getType() {
19+
return type;
20+
}
21+
22+
public void setType(ItemType type) {
23+
this.type = type;
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar;
2+
3+
public interface ItemIterator {
4+
5+
boolean hasNext();
6+
7+
Item next();
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar;
2+
3+
public enum ItemType {
4+
5+
ANY,
6+
WEAPON,
7+
RING,
8+
POTION
9+
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.iluwatar;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class TreasureChest {
7+
8+
private List<Item> items;
9+
10+
public TreasureChest() {
11+
items = new ArrayList<>();
12+
items.add(new Item(ItemType.POTION, "Potion of courage"));
13+
items.add(new Item(ItemType.RING, "Ring of shadows"));
14+
items.add(new Item(ItemType.POTION, "Potion of wisdom"));
15+
items.add(new Item(ItemType.POTION, "Potion of blood"));
16+
items.add(new Item(ItemType.WEAPON, "Sword of silver +1"));
17+
items.add(new Item(ItemType.POTION, "Potion of rust"));
18+
items.add(new Item(ItemType.POTION, "Potion of healing"));
19+
items.add(new Item(ItemType.RING, "Ring of armor"));
20+
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
21+
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
22+
}
23+
24+
ItemIterator Iterator(ItemType type) {
25+
return new TreasureChestItemIterator(this, type);
26+
}
27+
28+
public List<Item> getItems() {
29+
ArrayList<Item> list = new ArrayList<>();
30+
list.addAll(items);
31+
return list;
32+
}
33+
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.iluwatar;
2+
3+
import java.util.List;
4+
5+
public class TreasureChestItemIterator implements ItemIterator {
6+
7+
private TreasureChest chest;
8+
private int idx;
9+
private ItemType type;
10+
11+
public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
12+
this.chest = chest;
13+
this.type = type;
14+
this.idx = -1;
15+
}
16+
17+
@Override
18+
public boolean hasNext() {
19+
return findNextIdx() != -1;
20+
}
21+
22+
@Override
23+
public Item next() {
24+
idx = findNextIdx();
25+
if (idx != -1) {
26+
return chest.getItems().get(idx);
27+
}
28+
return null;
29+
}
30+
31+
private int findNextIdx() {
32+
33+
List<Item> items = chest.getItems();
34+
boolean found = false;
35+
int tempIdx = idx;
36+
while (!found) {
37+
tempIdx++;
38+
if (tempIdx >= items.size()) {
39+
tempIdx = -1;
40+
break;
41+
}
42+
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
43+
break;
44+
}
45+
}
46+
return tempIdx;
47+
}
48+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<module>chain</module>
3434
<module>command</module>
3535
<module>interpreter</module>
36+
<module>iterator</module>
3637
</modules>
3738

3839
<build>

0 commit comments

Comments
 (0)