added iterator sample

This commit is contained in:
Ilkka Seppala 2014-08-18 23:18:15 +03:00
parent 71d7c875d6
commit 91ed79e6bd
8 changed files with 184 additions and 0 deletions

23
iterator/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.iluwatar</groupId>
<artifactId>iterator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>iterator</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,35 @@
package com.iluwatar;
public class App
{
public static void main( String[] args )
{
TreasureChest chest = new TreasureChest();
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
while (ringIterator.hasNext()) {
System.out.println(ringIterator.next());
}
System.out.println("----------");
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
while (potionIterator.hasNext()) {
System.out.println(potionIterator.next());
}
System.out.println("----------");
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
while (weaponIterator.hasNext()) {
System.out.println(weaponIterator.next());
}
System.out.println("----------");
ItemIterator it = chest.Iterator(ItemType.ANY);
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

View File

@ -0,0 +1,25 @@
package com.iluwatar;
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 void setType(ItemType type) {
this.type = type;
}
}

View File

@ -0,0 +1,8 @@
package com.iluwatar;
public interface ItemIterator {
boolean hasNext();
Item next();
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public enum ItemType {
ANY,
WEAPON,
RING,
POTION
}

View File

@ -0,0 +1,34 @@
package com.iluwatar;
import java.util.ArrayList;
import java.util.List;
public class TreasureChest {
private List<Item> items;
public TreasureChest() {
items = new ArrayList<>();
items.add(new Item(ItemType.POTION, "Potion of courage"));
items.add(new Item(ItemType.RING, "Ring of shadows"));
items.add(new Item(ItemType.POTION, "Potion of wisdom"));
items.add(new Item(ItemType.POTION, "Potion of blood"));
items.add(new Item(ItemType.WEAPON, "Sword of silver +1"));
items.add(new Item(ItemType.POTION, "Potion of rust"));
items.add(new Item(ItemType.POTION, "Potion of healing"));
items.add(new Item(ItemType.RING, "Ring of armor"));
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
}
ItemIterator Iterator(ItemType type) {
return new TreasureChestItemIterator(this, type);
}
public List<Item> getItems() {
ArrayList<Item> list = new ArrayList<>();
list.addAll(items);
return list;
}
}

View File

@ -0,0 +1,48 @@
package com.iluwatar;
import java.util.List;
public class TreasureChestItemIterator implements ItemIterator {
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() {
List<Item> items = chest.getItems();
boolean found = false;
int tempIdx = idx;
while (!found) {
tempIdx++;
if (tempIdx >= items.size()) {
tempIdx = -1;
break;
}
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
break;
}
}
return tempIdx;
}
}

View File

@ -33,6 +33,7 @@
<module>chain</module>
<module>command</module>
<module>interpreter</module>
<module>iterator</module>
</modules>
<build>