Add abstractions to implement handlers for kotlin collections

This commit is contained in:
Vitaliy.Bibaev
2017-11-15 18:12:08 +03:00
committed by Yan Zhulanow
parent cd4577ea78
commit 8fc64490ca
8 changed files with 117 additions and 81 deletions
@@ -1,8 +1,7 @@
package com.intellij.debugger.streams.kotlin.lib
import com.intellij.debugger.streams.kotlin.resolve.FilterOrderResolver
import com.intellij.debugger.streams.kotlin.trace.impl.handler.collections.FilterIntermediateHandler
import com.intellij.debugger.streams.kotlin.trace.impl.handler.collections.FilterTerminatorHandler
import com.intellij.debugger.streams.kotlin.trace.impl.handler.collections.*
import com.intellij.debugger.streams.kotlin.trace.impl.interpret.FilterTraceInterpreter
import com.intellij.debugger.streams.lib.IntermediateOperation
import com.intellij.debugger.streams.lib.TerminalOperation
@@ -20,9 +19,7 @@ import com.intellij.debugger.streams.wrapper.TerminatorStreamCall
*/
class KotlinCollectionLibrarySupport : LibrarySupportBase() {
init {
addOperation(FilterOperation("filter",
{ num, call, dsl -> FilterIntermediateHandler(num, call, dsl) },
{ call, resultExpression, dsl -> FilterTerminatorHandler(call, resultExpression, dsl)}))
addOperation(FilterOperation("filter", FilterCallHandler()))
}
private fun addOperation(operation: CollectionOperation) {
@@ -31,23 +28,21 @@ class KotlinCollectionLibrarySupport : LibrarySupportBase() {
}
private abstract class CollectionOperation(override val name: String,
private val intermediateHandlerProvider: IntermediateHandlerProvider,
private val terminatorCallProvider: TerminatorHandlerProvider)
handler: BothSemanticsHandler)
: IntermediateOperation, TerminalOperation {
private val wrapper = BothSemanticCallWrapper(handler)
override fun getTraceHandler(callOrder: Int, call: IntermediateStreamCall, dsl: Dsl): IntermediateCallHandler =
intermediateHandlerProvider(callOrder, call, dsl)
wrapper.createIntermediateHandler(callOrder, call, dsl)
override fun getTraceHandler(call: TerminatorStreamCall, resultExpression: String, dsl: Dsl): TerminatorCallHandler =
terminatorCallProvider(call, resultExpression, dsl)
wrapper.createTerminatorHandler(call, resultExpression, dsl)
}
private class FilterOperation(name: String, intermediateHandlerProvider: IntermediateHandlerProvider,
terminatorHandlerProvider: TerminatorHandlerProvider)
: CollectionOperation(name, intermediateHandlerProvider, terminatorHandlerProvider) {
private class FilterOperation(name: String, handler: BothSemanticsHandler)
: CollectionOperation(name, handler) {
override val traceInterpreter: CallTraceInterpreter = FilterTraceInterpreter()
override val valuesOrderResolver: ValuesOrderResolver = FilterOrderResolver()
}
}
private typealias IntermediateHandlerProvider = (Int, IntermediateStreamCall, Dsl) -> IntermediateCallHandler
private typealias TerminatorHandlerProvider = (TerminatorStreamCall, String, Dsl) -> TerminatorCallHandler
@@ -0,0 +1,21 @@
package com.intellij.debugger.streams.kotlin.trace.impl.handler.collections
import com.intellij.debugger.streams.trace.IntermediateCallHandler
import com.intellij.debugger.streams.trace.TerminatorCallHandler
import com.intellij.debugger.streams.trace.dsl.Dsl
import com.intellij.debugger.streams.wrapper.IntermediateStreamCall
import com.intellij.debugger.streams.wrapper.TerminatorStreamCall
/**
* Unlike java streams, most operations in kotlin collections are intermediate and terminal simultaneously.
* To avoid using of the same code in two places we will implement common logic in the {@link BothSemanticsHandler}.
*
* @author Vitaliy.Bibaev
*/
class BothSemanticCallWrapper(private val handler: BothSemanticsHandler) {
fun createIntermediateHandler(order: Int, call: IntermediateStreamCall, dsl: Dsl): IntermediateCallHandler =
CollectionIntermediateHandler(order, call, dsl, handler)
fun createTerminatorHandler(call: TerminatorStreamCall, resultExpression: String, dsl: Dsl): TerminatorCallHandler =
CollectionTerminatorHandler(call, resultExpression, dsl, handler)
}
@@ -0,0 +1,7 @@
package com.intellij.debugger.streams.kotlin.trace.impl.handler.collections
/**
* @author Vitaliy.Bibaev
*/
interface BothSemanticsHandler {
}
@@ -0,0 +1,38 @@
package com.intellij.debugger.streams.kotlin.trace.impl.handler.collections
import com.intellij.debugger.streams.trace.IntermediateCallHandler
import com.intellij.debugger.streams.trace.dsl.CodeBlock
import com.intellij.debugger.streams.trace.dsl.Dsl
import com.intellij.debugger.streams.trace.dsl.Expression
import com.intellij.debugger.streams.trace.dsl.VariableDeclaration
import com.intellij.debugger.streams.wrapper.IntermediateStreamCall
/**
* @author Vitaliy.Bibaev
*/
class CollectionIntermediateHandler(
private val order: Int,
private val call: IntermediateStreamCall,
private val dsl: Dsl,
private val internalHandler: BothSemanticsHandler)
: IntermediateCallHandler {
override fun additionalVariablesDeclaration(): MutableList<VariableDeclaration> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun prepareResult(): CodeBlock {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun additionalCallsBefore(): MutableList<IntermediateStreamCall> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun additionalCallsAfter(): MutableList<IntermediateStreamCall> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getResultExpression(): Expression {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
@@ -0,0 +1,34 @@
package com.intellij.debugger.streams.kotlin.trace.impl.handler.collections
import com.intellij.debugger.streams.trace.TerminatorCallHandler
import com.intellij.debugger.streams.trace.dsl.CodeBlock
import com.intellij.debugger.streams.trace.dsl.Dsl
import com.intellij.debugger.streams.trace.dsl.Expression
import com.intellij.debugger.streams.trace.dsl.VariableDeclaration
import com.intellij.debugger.streams.wrapper.IntermediateStreamCall
import com.intellij.debugger.streams.wrapper.TerminatorStreamCall
/**
* @author Vitaliy.Bibaev
*/
class CollectionTerminatorHandler(private val internalHandler: TerminatorStreamCall,
private val resultExpression: String,
private val dsl: Dsl,
private val handler: BothSemanticsHandler)
: TerminatorCallHandler {
override fun additionalVariablesDeclaration(): MutableList<VariableDeclaration> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun prepareResult(): CodeBlock {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun additionalCallsBefore(): MutableList<IntermediateStreamCall> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getResultExpression(): Expression {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
@@ -0,0 +1,7 @@
package com.intellij.debugger.streams.kotlin.trace.impl.handler.collections
/**
* @author Vitaliy.Bibaev
*/
class FilterCallHandler : BothSemanticsHandler {
}
@@ -1,38 +0,0 @@
package com.intellij.debugger.streams.kotlin.trace.impl.handler.collections
import com.intellij.debugger.streams.trace.dsl.*
import com.intellij.debugger.streams.trace.dsl.impl.TextExpression
import com.intellij.debugger.streams.trace.impl.handler.unified.HandlerBase
import com.intellij.debugger.streams.wrapper.IntermediateStreamCall
/**
* @author Vitaliy.Bibaev
*/
class FilterIntermediateHandler(number: Int, call: IntermediateStreamCall, dsl: Dsl) : HandlerBase.Intermediate(dsl) {
private val itemsMap = dsl.linkedMap(dsl.types.INT, call.typeBefore, "filter${number}ItemsMap")
override fun additionalCallsAfter(): List<IntermediateStreamCall> {
return emptyList()
}
override fun additionalCallsBefore(): List<IntermediateStreamCall> {
return emptyList()
}
override fun transformCall(call: IntermediateStreamCall): IntermediateStreamCall {
// TODO: transform it
return call
}
override fun additionalVariablesDeclaration(): List<VariableDeclaration> {
return listOf(itemsMap.defaultDeclaration())
}
override fun getResultExpression(): Expression {
return dsl.newArray(dsl.types.ANY, TextExpression("values"))
}
override fun prepareResult(): CodeBlock {
return itemsMap.convertToArray(dsl, "values")
}
}
@@ -1,28 +0,0 @@
package com.intellij.debugger.streams.kotlin.trace.impl.handler.collections
import com.intellij.debugger.streams.trace.dsl.CodeBlock
import com.intellij.debugger.streams.trace.dsl.Dsl
import com.intellij.debugger.streams.trace.dsl.Expression
import com.intellij.debugger.streams.trace.dsl.VariableDeclaration
import com.intellij.debugger.streams.trace.dsl.impl.TextExpression
import com.intellij.debugger.streams.trace.impl.handler.unified.HandlerBase
import com.intellij.debugger.streams.wrapper.IntermediateStreamCall
import com.intellij.debugger.streams.wrapper.TerminatorStreamCall
/**
* @author Vitaliy.Bibaev
*/
class FilterTerminatorHandler(call: TerminatorStreamCall, resultExpression: String, dsl: Dsl)
: HandlerBase.Terminal(dsl) {
override fun additionalCallsBefore(): List<IntermediateStreamCall> = emptyList()
override fun additionalVariablesDeclaration(): List<VariableDeclaration> {
return emptyList()
}
override fun getResultExpression(): Expression = TextExpression("1")
override fun prepareResult(): CodeBlock {
return dsl.block { }
}
}