Reformat rest of the design patterns - Issue #224

This commit is contained in:
Ankur Kaushal
2015-11-01 21:29:13 -05:00
parent 449340bd2b
commit 306b1f3d31
337 changed files with 6744 additions and 6851 deletions

View File

@ -2,48 +2,48 @@ package com.iluwatar.iterator;
/**
*
* 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.
* 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 ItemIterator}) adds abstraction layer on
* top of a collection ({@link TreasureChest}). This way the collection can change
* its internal implementation without affecting its clients.
* In this example the Iterator ({@link ItemIterator}) 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 {
/**
* Program entry point
* @param args command line args
*/
public static void main(String[] args) {
TreasureChest chest = new TreasureChest();
/**
* 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()) {
System.out.println(ringIterator.next());
}
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
while (ringIterator.hasNext()) {
System.out.println(ringIterator.next());
}
System.out.println("----------");
System.out.println("----------");
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
while (potionIterator.hasNext()) {
System.out.println(potionIterator.next());
}
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
while (potionIterator.hasNext()) {
System.out.println(potionIterator.next());
}
System.out.println("----------");
System.out.println("----------");
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
while (weaponIterator.hasNext()) {
System.out.println(weaponIterator.next());
}
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
while (weaponIterator.hasNext()) {
System.out.println(weaponIterator.next());
}
System.out.println("----------");
System.out.println("----------");
ItemIterator it = chest.Iterator(ItemType.ANY);
while (it.hasNext()) {
System.out.println(it.next());
}
}
ItemIterator it = chest.Iterator(ItemType.ANY);
while (it.hasNext()) {
System.out.println(it.next());
}
}
}

View File

@ -7,24 +7,24 @@ package com.iluwatar.iterator;
*/
public class Item {
private ItemType type;
private String name;
private ItemType type;
private String name;
public Item(ItemType type, String name) {
this.setType(type);
this.name = name;
}
public Item(ItemType type, String name) {
this.setType(type);
this.name = name;
}
@Override
public String toString() {
return name;
}
@Override
public String toString() {
return name;
}
public ItemType getType() {
return type;
}
public ItemType getType() {
return type;
}
public void setType(ItemType type) {
this.type = type;
}
public void setType(ItemType type) {
this.type = type;
}
}

View File

@ -7,7 +7,7 @@ package com.iluwatar.iterator;
*/
public interface ItemIterator {
boolean hasNext();
boolean hasNext();
Item next();
Item next();
}

View File

@ -7,6 +7,6 @@ package com.iluwatar.iterator;
*/
public enum ItemType {
ANY, WEAPON, RING, POTION
ANY, WEAPON, RING, POTION
}

View File

@ -10,30 +10,30 @@ import java.util.List;
*/
public class TreasureChest {
private List<Item> items;
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"));
}
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);
}
ItemIterator Iterator(ItemType type) {
return new TreasureChestItemIterator(this, type);
}
public List<Item> getItems() {
ArrayList<Item> list = new ArrayList<>();
list.addAll(items);
return list;
}
public List<Item> getItems() {
ArrayList<Item> list = new ArrayList<>();
list.addAll(items);
return list;
}
}

View File

@ -9,46 +9,45 @@ import java.util.List;
*/
public class TreasureChestItemIterator implements ItemIterator {
private TreasureChest chest;
private int idx;
private ItemType type;
private TreasureChest chest;
private int idx;
private ItemType type;
public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
this.chest = chest;
this.type = type;
this.idx = -1;
}
public TreasureChestItemIterator(TreasureChest chest, ItemType type) {
this.chest = chest;
this.type = type;
this.idx = -1;
}
@Override
public boolean hasNext() {
return findNextIdx() != -1;
}
@Override
public boolean hasNext() {
return findNextIdx() != -1;
}
@Override
public Item next() {
idx = findNextIdx();
if (idx != -1) {
return chest.getItems().get(idx);
}
return null;
}
@Override
public Item next() {
idx = findNextIdx();
if (idx != -1) {
return chest.getItems().get(idx);
}
return null;
}
private int findNextIdx() {
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;
}
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

@ -11,9 +11,9 @@ import com.iluwatar.iterator.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}