org.jetbrains.kotlin.cli.common.repl.ReplState.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-client-embeddable Show documentation
Show all versions of kotlin-compiler-client-embeddable Show documentation
Kotlin compiler client embeddable
The newest version!
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.cli.common.repl
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write
interface ILineId : Comparable {
val no: Int
val generation: Int
}
data class ReplHistoryRecord (val id: ILineId, val item: T)
interface IReplStageHistory : List> {
fun peek(): ReplHistoryRecord? = lock.read { lastOrNull() }
fun push(id: ILineId, item: T)
fun pop(): ReplHistoryRecord?
fun verifiedPop(id: ILineId): ReplHistoryRecord? = lock.write {
if (lastOrNull()?.id == id) pop()
else null
}
fun reset(): Iterable
fun resetTo(id: ILineId): Iterable
val lock: ReentrantReadWriteLock
}
interface IReplStageState {
val history: IReplStageHistory
val lock: ReentrantReadWriteLock
val currentGeneration: Int
fun getNextLineNo(): Int = history.peek()?.id?.no?.let { it + 1 } ?: REPL_CODE_LINE_FIRST_NO // TODO: it should be more robust downstream (e.g. use atomic)
@Suppress("UNCHECKED_CAST")
fun > asState(target: Class): StateT =
if (target.isAssignableFrom(this::class.java)) this as StateT
else throw IllegalArgumentException("$this is not an expected instance of IReplStageState")
fun dispose() {
}
}
fun IReplStageHistory.firstMismatch(other: Sequence): Pair?, ILineId?>? =
lock.read {
iterator().asSequence().zip(other.asSequence()).firstOrNull { it.first.id != it.second }?.let { it.first to it.second }
}
fun IReplStageHistory.firstMismatchFiltered(other: Sequence, predicate: (ReplHistoryRecord) -> Boolean): Pair?, ILineId?>? =
lock.read {
iterator().asSequence().filter(predicate).zip(other.asSequence()).firstOrNull { it.first.id != it.second }?.let { it.first to it.second }
}
fun IReplStageHistory.firstMismatchWhile(other: Sequence, predicate: (ReplHistoryRecord) -> Boolean): Pair?, ILineId?>? =
lock.read {
iterator().asSequence().takeWhile(predicate).zip(other.asSequence()).firstOrNull { it.first.id != it.second }?.let { it.first to it.second }
}