Merged all map-transformations into one class

This commit is contained in:
Valentin Kipyatkov
2016-04-22 12:20:04 +03:00
parent b6e05e058e
commit 4efc8fa2b1
4 changed files with 26 additions and 85 deletions
@@ -49,19 +49,7 @@ class AddToCollectionTransformation(
} }
is MapTransformation -> { is MapTransformation -> {
MapToTransformation.create(loop, previousTransformation.inputVariable, null, targetCollection, previousTransformation.mapping, mapNotNull = false) MapToTransformation.create(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, targetCollection, previousTransformation.mapping, previousTransformation.mapNotNull)
}
is MapNotNullTransformation -> {
MapToTransformation.create(loop, previousTransformation.inputVariable, null, targetCollection, previousTransformation.mapping, mapNotNull = true)
}
is MapIndexedTransformation -> {
MapToTransformation.create(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, targetCollection, previousTransformation.mapping, mapNotNull = false)
}
is MapIndexedNotNullTransformation -> {
MapToTransformation.create(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, targetCollection, previousTransformation.mapping, mapNotNull = true)
} }
is FlatMapTransformation -> { is FlatMapTransformation -> {
@@ -144,7 +132,7 @@ class AddToCollectionTransformation(
AssignToListTransformation(state.outerLoop, collectionInitialization) AssignToListTransformation(state.outerLoop, collectionInitialization)
} }
else { else {
val mapTransformation = MapTransformation(state.outerLoop, state.inputVariable, addOperationArgument) val mapTransformation = MapTransformation(state.outerLoop, state.inputVariable, null, addOperationArgument, mapNotNull = false)
AssignSequenceTransformationResultTransformation(mapTransformation, collectionInitialization) AssignSequenceTransformationResultTransformation(mapTransformation, collectionInitialization)
} }
return ResultTransformationMatch(transformation) return ResultTransformationMatch(transformation)
@@ -176,7 +164,7 @@ class AddToCollectionTransformation(
return ResultTransformationMatch(assignToSetTransformation) return ResultTransformationMatch(assignToSetTransformation)
} }
else { else {
val mapTransformation = MapTransformation(state.outerLoop, state.inputVariable, addOperationArgument) val mapTransformation = MapTransformation(state.outerLoop, state.inputVariable, null, addOperationArgument, mapNotNull = false)
return ResultTransformationMatch(assignToSetTransformation, mapTransformation) return ResultTransformationMatch(assignToSetTransformation, mapTransformation)
} }
} }
@@ -164,11 +164,10 @@ class FilterIsInstanceTransformation(
class FilterNotNullTransformation(override val loop: KtForExpression) : SequenceTransformation { class FilterNotNullTransformation(override val loop: KtForExpression) : SequenceTransformation {
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): SequenceTransformation? { override fun mergeWithPrevious(previousTransformation: SequenceTransformation): SequenceTransformation? {
return when (previousTransformation) { if (previousTransformation is MapTransformation) {
is MapTransformation -> MapNotNullTransformation(loop, previousTransformation.inputVariable, previousTransformation.mapping) return MapTransformation(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, previousTransformation.mapping, mapNotNull = true)
is MapIndexedTransformation -> MapIndexedNotNullTransformation(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, previousTransformation.mapping)
else -> null
} }
return null
} }
override val affectsIndex: Boolean override val affectsIndex: Boolean
@@ -65,7 +65,7 @@ class FlatMapTransformation(
if (state.indexVariable != null && state.indexVariable.hasUsages(transform)) { if (state.indexVariable != null && state.indexVariable.hasUsages(transform)) {
// if nested loop range uses index, convert to "mapIndexed {...}.flatMap { it }" // if nested loop range uses index, convert to "mapIndexed {...}.flatMap { it }"
val mapIndexedTransformation = MapIndexedTransformation(state.outerLoop, state.inputVariable, state.indexVariable, transform) val mapIndexedTransformation = MapTransformation(state.outerLoop, state.inputVariable, state.indexVariable, transform, mapNotNull = false)
val inputVarExpression = KtPsiFactory(nestedLoop).createExpressionByPattern("$0", state.inputVariable.nameAsSafeName) val inputVarExpression = KtPsiFactory(nestedLoop).createExpressionByPattern("$0", state.inputVariable.nameAsSafeName)
val flatMapTransformation = FlatMapTransformation(state.outerLoop, state.inputVariable, inputVarExpression) val flatMapTransformation = FlatMapTransformation(state.outerLoop, state.inputVariable, inputVarExpression)
val newState = state.copy( val newState = state.copy(
@@ -23,18 +23,28 @@ import org.jetbrains.kotlin.psi.*
class MapTransformation( class MapTransformation(
override val loop: KtForExpression, override val loop: KtForExpression,
val inputVariable: KtCallableDeclaration, val inputVariable: KtCallableDeclaration,
val mapping: KtExpression val indexVariable: KtCallableDeclaration?,
val mapping: KtExpression,
val mapNotNull: Boolean
) : SequenceTransformation { ) : SequenceTransformation {
private val functionName = if (indexVariable != null)
if (mapNotNull) "mapIndexedNotNull" else "mapIndexed"
else
if (mapNotNull) "mapNotNull" else "map"
override val affectsIndex: Boolean override val affectsIndex: Boolean
get() = false get() = mapNotNull
override val presentation: String override val presentation: String
get() = "map{}" get() = "$functionName{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, mapping) val lambda = if (indexVariable != null)
return chainedCallGenerator.generate("map$0:'{}'", lambda) generateLambda(mapping, indexVariable, inputVariable)
else
generateLambda(inputVariable, mapping)
return chainedCallGenerator.generate("$functionName$0:'{}'", lambda)
} }
/** /**
@@ -62,9 +72,9 @@ class MapTransformation(
if (mapping.containsEmbeddedBreakOrContinue()) return null if (mapping.containsEmbeddedBreakOrContinue()) return null
val transformation = if (state.indexVariable != null && state.indexVariable.hasUsages(mapping)) val transformation = if (state.indexVariable != null && state.indexVariable.hasUsages(mapping))
MapIndexedNotNullTransformation(state.outerLoop, state.inputVariable, state.indexVariable, mapping) MapTransformation(state.outerLoop, state.inputVariable, state.indexVariable, mapping, mapNotNull = true)
else else
MapNotNullTransformation(state.outerLoop, state.inputVariable, mapping) MapTransformation(state.outerLoop, state.inputVariable, null, mapping, mapNotNull = true)
val newState = state.copy(statements = restStatements, inputVariable = declaration) val newState = state.copy(statements = restStatements, inputVariable = declaration)
return SequenceTransformationMatch(transformation, newState) return SequenceTransformationMatch(transformation, newState)
} }
@@ -72,67 +82,11 @@ class MapTransformation(
if (initializer.containsEmbeddedBreakOrContinue()) return null if (initializer.containsEmbeddedBreakOrContinue()) return null
val transformation = if (state.indexVariable != null && state.indexVariable.hasUsages(initializer)) val transformation = if (state.indexVariable != null && state.indexVariable.hasUsages(initializer))
MapIndexedTransformation(state.outerLoop, state.inputVariable, state.indexVariable, initializer) MapTransformation(state.outerLoop, state.inputVariable, state.indexVariable, initializer, mapNotNull = false)
else else
MapTransformation(state.outerLoop, state.inputVariable, initializer) MapTransformation(state.outerLoop, state.inputVariable, null, initializer, mapNotNull = false)
val newState = state.copy(statements = restStatements, inputVariable = declaration) val newState = state.copy(statements = restStatements, inputVariable = declaration)
return SequenceTransformationMatch(transformation, newState) return SequenceTransformationMatch(transformation, newState)
} }
} }
} }
class MapIndexedTransformation(
override val loop: KtForExpression,
val inputVariable: KtCallableDeclaration,
val indexVariable: KtCallableDeclaration,
val mapping: KtExpression
) : SequenceTransformation {
override val affectsIndex: Boolean
get() = false
override val presentation: String
get() = "mapIndexed{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(mapping, indexVariable, inputVariable)
return chainedCallGenerator.generate("mapIndexed $0:'{}'", lambda)
}
}
class MapNotNullTransformation(
override val loop: KtForExpression,
val inputVariable: KtCallableDeclaration,
val mapping: KtExpression
) : SequenceTransformation {
override val affectsIndex: Boolean
get() = true
override val presentation: String
get() = "mapNotNull{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, mapping)
return chainedCallGenerator.generate("mapNotNull$0:'{}'", lambda)
}
}
class MapIndexedNotNullTransformation(
override val loop: KtForExpression,
val inputVariable: KtCallableDeclaration,
val indexVariable: KtCallableDeclaration,
val mapping: KtExpression
) : SequenceTransformation {
override val affectsIndex: Boolean
get() = false
override val presentation: String
get() = "mapIndexedNotNull{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(mapping, indexVariable, inputVariable)
return chainedCallGenerator.generate("mapIndexedNotNull $0:'{}'", lambda)
}
}