Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* MIT License | ||
* <p> | ||
* Copyright (c) 2017 James | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* <p> | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* <p> | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Iterator | ||
*/ | ||
public class Application { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); | ||
|
||
public static void main(String[] args) { | ||
// 查找小说 | ||
ItemIterator iterator = new BookShelfIterator(ItemType.FICTION, new BookShelf()); | ||
LOGGER.info("正在查找小说类图书"); | ||
while (iterator.hasNext()) { | ||
Item nextItem = iterator.next(); | ||
LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString()); | ||
} | ||
|
||
// 查找IT | ||
ItemIterator iterator2 = new BookShelfIterator(ItemType.IT, new BookShelf()); | ||
LOGGER.info("正在查找IT类图书"); | ||
while (iterator2.hasNext()) { | ||
Item nextItem = iterator2.next(); | ||
LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString()); | ||
} | ||
|
||
// 查找漫画 | ||
ItemIterator iterator3 = new BookShelfIterator(ItemType.CARTOON, new BookShelf()); | ||
LOGGER.info("正在查找漫画类图书"); | ||
while (iterator3.hasNext()) { | ||
Item nextItem = iterator3.next(); | ||
LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* MIT License | ||
* <p> | ||
* Copyright (c) 2017 James | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* <p> | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* <p> | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 书架 | ||
*/ | ||
public class BookShelf { | ||
|
||
private List<Item> items; | ||
|
||
public BookShelf() { | ||
items = new ArrayList<>(); | ||
items.add(new Item(ItemType.FICTION, "西游记")); | ||
items.add(new Item(ItemType.FICTION, "水浒传")); | ||
items.add(new Item(ItemType.FICTION, "三国演义")); | ||
items.add(new Item(ItemType.FICTION, "红楼梦")); | ||
items.add(new Item(ItemType.CARTOON, "阿衰")); | ||
items.add(new Item(ItemType.CARTOON, "七龙珠")); | ||
items.add(new Item(ItemType.CARTOON, "火影忍者")); | ||
items.add(new Item(ItemType.IT, "设计模式-可复用面向对象软件的基础")); | ||
items.add(new Item(ItemType.IT, "重构-改善既有代码的设计")); | ||
items.add(new Item(ItemType.IT, "Effective Java")); | ||
items.add(new Item(ItemType.IT, "Java编程思想")); | ||
} | ||
|
||
public List<Item> getItemList() { | ||
List<Item> list = new ArrayList<>(); | ||
list.addAll(items); | ||
return list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* MIT License | ||
* <p> | ||
* Copyright (c) 2017 James | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* <p> | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* <p> | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 图书的迭代器 | ||
*/ | ||
public class BookShelfIterator implements ItemIterator { | ||
|
||
private ItemType type; | ||
private BookShelf shelf; | ||
private int idx; | ||
|
||
public BookShelfIterator(ItemType type, BookShelf shelf) { | ||
this.type = type; | ||
this.shelf = shelf; | ||
this.idx = -1; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return -1 != getNexIdx(); | ||
} | ||
|
||
@Override | ||
public Item next() { | ||
idx = getNexIdx(); | ||
if (-1 != idx) { | ||
return shelf.getItemList().get(idx); | ||
} | ||
return null; | ||
} | ||
|
||
private int getNexIdx() { | ||
List<Item> list = shelf.getItemList(); | ||
int tempIdx = idx; | ||
boolean found = false; | ||
while (!found) { | ||
tempIdx++; | ||
if (tempIdx >= list.size()) { | ||
tempIdx = -1; | ||
break; | ||
} | ||
if (list.get(tempIdx).getType().equals(type)) { | ||
break; | ||
} | ||
} | ||
return tempIdx; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,6 @@ public ItemType getType() { | |
|
||
@Override | ||
public String toString() { | ||
return super.toString(); | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* MIT License | ||
* <p> | ||
* Copyright (c) 2017 James | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* <p> | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* <p> | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* 迭代器接口 | ||
*/ | ||
public interface ItemIterator { | ||
|
||
boolean hasNext(); | ||
|
||
Item next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,5 @@ | |
*/ | ||
public enum ItemType { | ||
|
||
ANY, VEHICLE, ANIMAL, FOOD; | ||
IT, FICTION, CARTOON | ||
} |