#107 Iterator example JavaDoc
This commit is contained in:
parent
5831d3239d
commit
0d2e033df6
@ -1,41 +1,45 @@
|
|||||||
package com.iluwatar.iterator;
|
package com.iluwatar.iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Iterator (ItemIterator) adds abstraction layer on top of a collection
|
* Iterator ({@link ItemIterator}) adds abstraction layer on top of a collection
|
||||||
* (TreasureChest). This way the collection can change its internal
|
* ({@link TreasureChest}). This way the collection can change its internal
|
||||||
* implementation without affecting its clients.
|
* implementation without affecting its clients.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
TreasureChest chest = new TreasureChest();
|
* Program entry point
|
||||||
|
* @param args command line args
|
||||||
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
*/
|
||||||
while (ringIterator.hasNext()) {
|
public static void main(String[] args) {
|
||||||
System.out.println(ringIterator.next());
|
TreasureChest chest = new TreasureChest();
|
||||||
}
|
|
||||||
|
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
||||||
System.out.println("----------");
|
while (ringIterator.hasNext()) {
|
||||||
|
System.out.println(ringIterator.next());
|
||||||
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
}
|
||||||
while (potionIterator.hasNext()) {
|
|
||||||
System.out.println(potionIterator.next());
|
System.out.println("----------");
|
||||||
}
|
|
||||||
|
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
||||||
System.out.println("----------");
|
while (potionIterator.hasNext()) {
|
||||||
|
System.out.println(potionIterator.next());
|
||||||
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
}
|
||||||
while (weaponIterator.hasNext()) {
|
|
||||||
System.out.println(weaponIterator.next());
|
System.out.println("----------");
|
||||||
}
|
|
||||||
|
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
||||||
System.out.println("----------");
|
while (weaponIterator.hasNext()) {
|
||||||
|
System.out.println(weaponIterator.next());
|
||||||
ItemIterator it = chest.Iterator(ItemType.ANY);
|
}
|
||||||
while (it.hasNext()) {
|
|
||||||
System.out.println(it.next());
|
System.out.println("----------");
|
||||||
}
|
|
||||||
}
|
ItemIterator it = chest.Iterator(ItemType.ANY);
|
||||||
}
|
while (it.hasNext()) {
|
||||||
|
System.out.println(it.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,25 +1,30 @@
|
|||||||
package com.iluwatar.iterator;
|
package com.iluwatar.iterator;
|
||||||
|
|
||||||
public class Item {
|
/**
|
||||||
|
*
|
||||||
private ItemType type;
|
* Item
|
||||||
private String name;
|
*
|
||||||
|
*/
|
||||||
public Item(ItemType type, String name) {
|
public class Item {
|
||||||
this.setType(type);
|
|
||||||
this.name = name;
|
private ItemType type;
|
||||||
}
|
private String name;
|
||||||
|
|
||||||
@Override
|
public Item(ItemType type, String name) {
|
||||||
public String toString() {
|
this.setType(type);
|
||||||
return name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemType getType() {
|
@Override
|
||||||
return type;
|
public String toString() {
|
||||||
}
|
return name;
|
||||||
|
}
|
||||||
public void setType(ItemType type) {
|
|
||||||
this.type = type;
|
public ItemType getType() {
|
||||||
}
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setType(ItemType type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.iluwatar.iterator;
|
package com.iluwatar.iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Iterator interface.
|
* ItemIterator interface.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface ItemIterator {
|
public interface ItemIterator {
|
||||||
|
|
||||||
boolean hasNext();
|
boolean hasNext();
|
||||||
|
|
||||||
Item next();
|
Item next();
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package com.iluwatar.iterator;
|
package com.iluwatar.iterator;
|
||||||
|
|
||||||
public enum ItemType {
|
/**
|
||||||
|
*
|
||||||
ANY, WEAPON, RING, POTION
|
* ItemType enumeration
|
||||||
|
*
|
||||||
}
|
*/
|
||||||
|
public enum ItemType {
|
||||||
|
|
||||||
|
ANY, WEAPON, RING, POTION
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,39 +1,39 @@
|
|||||||
package com.iluwatar.iterator;
|
package com.iluwatar.iterator;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Collection class.
|
* TreasureChest, the collection class.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class TreasureChest {
|
public class TreasureChest {
|
||||||
|
|
||||||
private List<Item> items;
|
private List<Item> items;
|
||||||
|
|
||||||
public TreasureChest() {
|
public TreasureChest() {
|
||||||
items = new ArrayList<>();
|
items = new ArrayList<>();
|
||||||
items.add(new Item(ItemType.POTION, "Potion of courage"));
|
items.add(new Item(ItemType.POTION, "Potion of courage"));
|
||||||
items.add(new Item(ItemType.RING, "Ring of shadows"));
|
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 wisdom"));
|
||||||
items.add(new Item(ItemType.POTION, "Potion of blood"));
|
items.add(new Item(ItemType.POTION, "Potion of blood"));
|
||||||
items.add(new Item(ItemType.WEAPON, "Sword of silver +1"));
|
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 rust"));
|
||||||
items.add(new Item(ItemType.POTION, "Potion of healing"));
|
items.add(new Item(ItemType.POTION, "Potion of healing"));
|
||||||
items.add(new Item(ItemType.RING, "Ring of armor"));
|
items.add(new Item(ItemType.RING, "Ring of armor"));
|
||||||
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
|
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
|
||||||
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
|
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemIterator Iterator(ItemType type) {
|
ItemIterator Iterator(ItemType type) {
|
||||||
return new TreasureChestItemIterator(this, type);
|
return new TreasureChestItemIterator(this, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Item> getItems() {
|
public List<Item> getItems() {
|
||||||
ArrayList<Item> list = new ArrayList<>();
|
ArrayList<Item> list = new ArrayList<>();
|
||||||
list.addAll(items);
|
list.addAll(items);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,49 +1,54 @@
|
|||||||
package com.iluwatar.iterator;
|
package com.iluwatar.iterator;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TreasureChestItemIterator implements ItemIterator {
|
/**
|
||||||
|
*
|
||||||
private TreasureChest chest;
|
* TreasureChestItemIterator
|
||||||
private int idx;
|
*
|
||||||
private ItemType type;
|
*/
|
||||||
|
public class TreasureChestItemIterator implements ItemIterator {
|
||||||
public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
|
|
||||||
this.chest = chest;
|
private TreasureChest chest;
|
||||||
this.type = type;
|
private int idx;
|
||||||
this.idx = -1;
|
private ItemType type;
|
||||||
}
|
|
||||||
|
public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
|
||||||
@Override
|
this.chest = chest;
|
||||||
public boolean hasNext() {
|
this.type = type;
|
||||||
return findNextIdx() != -1;
|
this.idx = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item next() {
|
public boolean hasNext() {
|
||||||
idx = findNextIdx();
|
return findNextIdx() != -1;
|
||||||
if (idx != -1) {
|
}
|
||||||
return chest.getItems().get(idx);
|
|
||||||
}
|
@Override
|
||||||
return null;
|
public Item next() {
|
||||||
}
|
idx = findNextIdx();
|
||||||
|
if (idx != -1) {
|
||||||
private int findNextIdx() {
|
return chest.getItems().get(idx);
|
||||||
|
}
|
||||||
List<Item> items = chest.getItems();
|
return null;
|
||||||
boolean found = false;
|
}
|
||||||
int tempIdx = idx;
|
|
||||||
while (!found) {
|
private int findNextIdx() {
|
||||||
tempIdx++;
|
|
||||||
if (tempIdx >= items.size()) {
|
List<Item> items = chest.getItems();
|
||||||
tempIdx = -1;
|
boolean found = false;
|
||||||
break;
|
int tempIdx = idx;
|
||||||
}
|
while (!found) {
|
||||||
if (type.equals(ItemType.ANY)
|
tempIdx++;
|
||||||
|| items.get(tempIdx).getType().equals(type)) {
|
if (tempIdx >= items.size()) {
|
||||||
break;
|
tempIdx = -1;
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
return tempIdx;
|
if (type.equals(ItemType.ANY)
|
||||||
}
|
|| items.get(tempIdx).getType().equals(type)) {
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tempIdx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
package com.iluwatar.iterator;
|
package com.iluwatar.iterator;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.iluwatar.iterator.App;
|
import com.iluwatar.iterator.App;
|
||||||
|
|
||||||
public class AppTest {
|
/**
|
||||||
|
*
|
||||||
@Test
|
* Application test
|
||||||
public void test() {
|
*
|
||||||
String[] args = {};
|
*/
|
||||||
App.main(args);
|
public class AppTest {
|
||||||
}
|
|
||||||
}
|
@Test
|
||||||
|
public void test() {
|
||||||
|
String[] args = {};
|
||||||
|
App.main(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user