Skip to content

Commit 0d2e033

Browse files
committed
iluwatar#107 Iterator example JavaDoc
1 parent 5831d32 commit 0d2e033

File tree

7 files changed

+212
-188
lines changed

7 files changed

+212
-188
lines changed
Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
1-
package com.iluwatar.iterator;
2-
3-
/**
4-
*
5-
* Iterator (ItemIterator) adds abstraction layer on top of a collection
6-
* (TreasureChest). This way the collection can change its internal
7-
* implementation without affecting its clients.
8-
*
9-
*/
10-
public class App {
11-
12-
public static void main(String[] args) {
13-
TreasureChest chest = new TreasureChest();
14-
15-
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
16-
while (ringIterator.hasNext()) {
17-
System.out.println(ringIterator.next());
18-
}
19-
20-
System.out.println("----------");
21-
22-
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
23-
while (potionIterator.hasNext()) {
24-
System.out.println(potionIterator.next());
25-
}
26-
27-
System.out.println("----------");
28-
29-
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
30-
while (weaponIterator.hasNext()) {
31-
System.out.println(weaponIterator.next());
32-
}
33-
34-
System.out.println("----------");
35-
36-
ItemIterator it = chest.Iterator(ItemType.ANY);
37-
while (it.hasNext()) {
38-
System.out.println(it.next());
39-
}
40-
}
41-
}
1+
package com.iluwatar.iterator;
2+
3+
/**
4+
*
5+
* Iterator ({@link ItemIterator}) adds abstraction layer on top of a collection
6+
* ({@link TreasureChest}). This way the collection can change its internal
7+
* implementation without affecting its clients.
8+
*
9+
*/
10+
public class App {
11+
12+
/**
13+
* Program entry point
14+
* @param args command line args
15+
*/
16+
public static void main(String[] args) {
17+
TreasureChest chest = new TreasureChest();
18+
19+
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
20+
while (ringIterator.hasNext()) {
21+
System.out.println(ringIterator.next());
22+
}
23+
24+
System.out.println("----------");
25+
26+
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
27+
while (potionIterator.hasNext()) {
28+
System.out.println(potionIterator.next());
29+
}
30+
31+
System.out.println("----------");
32+
33+
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
34+
while (weaponIterator.hasNext()) {
35+
System.out.println(weaponIterator.next());
36+
}
37+
38+
System.out.println("----------");
39+
40+
ItemIterator it = chest.Iterator(ItemType.ANY);
41+
while (it.hasNext()) {
42+
System.out.println(it.next());
43+
}
44+
}
45+
}
Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
package com.iluwatar.iterator;
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-
}
1+
package com.iluwatar.iterator;
2+
3+
/**
4+
*
5+
* Item
6+
*
7+
*/
8+
public class Item {
9+
10+
private ItemType type;
11+
private String name;
12+
13+
public Item(ItemType type, String name) {
14+
this.setType(type);
15+
this.name = name;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return name;
21+
}
22+
23+
public ItemType getType() {
24+
return type;
25+
}
26+
27+
public void setType(ItemType type) {
28+
this.type = type;
29+
}
30+
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package com.iluwatar.iterator;
2-
3-
/**
4-
*
5-
* Iterator interface.
6-
*
7-
*/
8-
public interface ItemIterator {
9-
10-
boolean hasNext();
11-
12-
Item next();
13-
}
1+
package com.iluwatar.iterator;
2+
3+
/**
4+
*
5+
* ItemIterator interface.
6+
*
7+
*/
8+
public interface ItemIterator {
9+
10+
boolean hasNext();
11+
12+
Item next();
13+
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
package com.iluwatar.iterator;
2-
3-
public enum ItemType {
4-
5-
ANY, WEAPON, RING, POTION
6-
7-
}
1+
package com.iluwatar.iterator;
2+
3+
/**
4+
*
5+
* ItemType enumeration
6+
*
7+
*/
8+
public enum ItemType {
9+
10+
ANY, WEAPON, RING, POTION
11+
12+
}
Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
package com.iluwatar.iterator;
2-
3-
import java.util.ArrayList;
4-
import java.util.List;
5-
6-
/**
7-
*
8-
* Collection class.
9-
*
10-
*/
11-
public class TreasureChest {
12-
13-
private List<Item> items;
14-
15-
public TreasureChest() {
16-
items = new ArrayList<>();
17-
items.add(new Item(ItemType.POTION, "Potion of courage"));
18-
items.add(new Item(ItemType.RING, "Ring of shadows"));
19-
items.add(new Item(ItemType.POTION, "Potion of wisdom"));
20-
items.add(new Item(ItemType.POTION, "Potion of blood"));
21-
items.add(new Item(ItemType.WEAPON, "Sword of silver +1"));
22-
items.add(new Item(ItemType.POTION, "Potion of rust"));
23-
items.add(new Item(ItemType.POTION, "Potion of healing"));
24-
items.add(new Item(ItemType.RING, "Ring of armor"));
25-
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
26-
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
27-
}
28-
29-
ItemIterator Iterator(ItemType type) {
30-
return new TreasureChestItemIterator(this, type);
31-
}
32-
33-
public List<Item> getItems() {
34-
ArrayList<Item> list = new ArrayList<>();
35-
list.addAll(items);
36-
return list;
37-
}
38-
39-
}
1+
package com.iluwatar.iterator;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
*
8+
* TreasureChest, the collection class.
9+
*
10+
*/
11+
public class TreasureChest {
12+
13+
private List<Item> items;
14+
15+
public TreasureChest() {
16+
items = new ArrayList<>();
17+
items.add(new Item(ItemType.POTION, "Potion of courage"));
18+
items.add(new Item(ItemType.RING, "Ring of shadows"));
19+
items.add(new Item(ItemType.POTION, "Potion of wisdom"));
20+
items.add(new Item(ItemType.POTION, "Potion of blood"));
21+
items.add(new Item(ItemType.WEAPON, "Sword of silver +1"));
22+
items.add(new Item(ItemType.POTION, "Potion of rust"));
23+
items.add(new Item(ItemType.POTION, "Potion of healing"));
24+
items.add(new Item(ItemType.RING, "Ring of armor"));
25+
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
26+
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
27+
}
28+
29+
ItemIterator Iterator(ItemType type) {
30+
return new TreasureChestItemIterator(this, type);
31+
}
32+
33+
public List<Item> getItems() {
34+
ArrayList<Item> list = new ArrayList<>();
35+
list.addAll(items);
36+
return list;
37+
}
38+
39+
}
Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,54 @@
1-
package com.iluwatar.iterator;
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)
43-
|| items.get(tempIdx).getType().equals(type)) {
44-
break;
45-
}
46-
}
47-
return tempIdx;
48-
}
49-
}
1+
package com.iluwatar.iterator;
2+
3+
import java.util.List;
4+
5+
/**
6+
*
7+
* TreasureChestItemIterator
8+
*
9+
*/
10+
public class TreasureChestItemIterator implements ItemIterator {
11+
12+
private TreasureChest chest;
13+
private int idx;
14+
private ItemType type;
15+
16+
public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
17+
this.chest = chest;
18+
this.type = type;
19+
this.idx = -1;
20+
}
21+
22+
@Override
23+
public boolean hasNext() {
24+
return findNextIdx() != -1;
25+
}
26+
27+
@Override
28+
public Item next() {
29+
idx = findNextIdx();
30+
if (idx != -1) {
31+
return chest.getItems().get(idx);
32+
}
33+
return null;
34+
}
35+
36+
private int findNextIdx() {
37+
38+
List<Item> items = chest.getItems();
39+
boolean found = false;
40+
int tempIdx = idx;
41+
while (!found) {
42+
tempIdx++;
43+
if (tempIdx >= items.size()) {
44+
tempIdx = -1;
45+
break;
46+
}
47+
if (type.equals(ItemType.ANY)
48+
|| items.get(tempIdx).getType().equals(type)) {
49+
break;
50+
}
51+
}
52+
return tempIdx;
53+
}
54+
}

0 commit comments

Comments
 (0)