scala設計モードの反復器モードを実現
23161 ワード
package com.linewell.modeldesgin.iterator
import scala.collection.mutable.ArrayBuffer
/** * * Created by ctao on 2015/9/1. */ trait AbstractIterator {
/** * */ def next(): Unit
/** * * @return */ def isLast: Boolean
/** * */ def previous(): Unit
/** * * @return */ def isFirst: Boolean
/** * * @return */ def getNextItem: Any
/** * * @return */ def getPreviousItem: Any
}
/** * * @param objects */ abstract class AbstractAnyArrayBuffer(objects: ArrayBuffer[Any]) {
def addAny(any: Any) = objects += any
def removeAny(any: Any) = objects -= any
def getAny = this.objects
def createIterator: AbstractIterator
}
/** * * @param objects */ class ProductArrayBuffer(objects: ArrayBuffer[Any]) extends AbstractAnyArrayBuffer(objects) {
override def createIterator = new ProductIterator
/** * */ protected class ProductIterator extends AbstractIterator {
/** * */ private var cursor1: Int = 0
/** * */ private var cursor2: Int = objects.length - 1
override def next(): Unit = {
if (cursor1 < objects.length) {
cursor1 += 1
}
}
override def isLast: Boolean = cursor1 == objects.length
override def previous(): Unit = {
if (cursor2 > -1) {
cursor2 -= 1
}
}
override def isFirst: Boolean = cursor2 == -1
override def getNextItem = objects(cursor1)
override def getPreviousItem = objects(cursor2)
}
}
/** * * @param objects */ class ProductArrayBuffer2(objects: ArrayBuffer[Any]) extends AbstractAnyArrayBuffer(objects) {
override def createIterator = new ProductIterator2(this)
}
/** * * @param productArrayBuffer2 */ class ProductIterator2(productArrayBuffer2: ProductArrayBuffer2) extends AbstractIterator {
private val products = productArrayBuffer2.getAny
private var cursor1: Int = 0
private var cursor2: Int = products.length - 1
override def next(): Unit = {
if (cursor1 < products.length) {
cursor1 += 1
}
}
override def isLast: Boolean = cursor1 == products.length
override def previous(): Unit = {
if (cursor2 > -1) {
cursor2 -= 1
}
}
override def isFirst: Boolean = cursor2 == -1
override def getNextItem = products(cursor1)
override def getPreviousItem = products(cursor2)
}
package com.linewell.modeldesgin.iterator
import scala.collection.mutable.ArrayBuffer
/** * * , scala java , * reverse * * Created by ctao on 2015/9/1. */ object Client extends App {
var products = new ArrayBuffer[Any]()
products += "a"
products += "b"
products += "c"
products += "d"
products += "e"
println(" ")
products.foreach(x =>print(x+", "))
println()
println("-------------------------")
println(" ")
products.reverse.foreach(x =>print(x+", "))
println()
val arrayBuffer1: AbstractAnyArrayBuffer = new ProductArrayBuffer(products)
arrayBuffer1.removeAny("a")
arrayBuffer1.addAny("cc")
val iterator1: AbstractIterator = arrayBuffer1.createIterator
println(" ")
while (!iterator1.isLast) {
print(iterator1.getNextItem + ", ")
iterator1.next()
}
println()
println("---------------------------")
println(" ")
while (!iterator1.isFirst) {
print(iterator1.getPreviousItem + ", ")
iterator1.previous()
}
println()
val arrayBuffer2: AbstractAnyArrayBuffer = new ProductArrayBuffer2(products)
arrayBuffer2.removeAny("b")
arrayBuffer2.addAny("cca")
val iterator2: AbstractIterator = arrayBuffer2.createIterator
println(" ")
while (!iterator2.isLast) {
print(iterator2.getNextItem + ", ")
iterator2.next()
}
println()
println("---------------------------")
println(" ")
while (!iterator2.isFirst) {
print(iterator2.getPreviousItem + ", ")
iterator2.previous()
}
}