@@ -62,12 +62,21 @@ public sealed class Delta<T>(public val type: DeltaType) {
6262 * @throws PatchFailedException
6363 */
6464 @Throws(PatchFailedException ::class )
65- protected open fun verifyChunk (target : List <T >) {
66- source.verify(target)
65+ protected open fun verifyChunkToFitTarget (target : List <T >): VerifyChunk {
66+ return source.verify(target)
6767 }
6868
6969 @Throws(PatchFailedException ::class )
70- public abstract fun applyTo (target : MutableList <T >)
70+ public open fun verifyAndApplyTo (target : MutableList <T >): VerifyChunk {
71+ val verify: VerifyChunk = verifyChunkToFitTarget(target)
72+ if (verify == VerifyChunk .OK ) {
73+ applyTo(target)
74+ }
75+ return verify
76+ }
77+
78+ @Throws(PatchFailedException ::class )
79+ protected abstract fun applyTo (target : MutableList <T >)
7180
7281 public abstract fun restore (target : MutableList <T >)
7382
@@ -77,8 +86,7 @@ public sealed class Delta<T>(public val type: DeltaType) {
7786 public abstract fun withChunks (original : Chunk <T >, revised : Chunk <T >): Delta <T >
7887}
7988public data class ChangeDelta <T >(override val source : Chunk <T >, override val target : Chunk <T >) : Delta<T>(DeltaType .CHANGE ) {
80- override fun applyTo (target : MutableList <T >) {
81- verifyChunk(target)
89+ protected override fun applyTo (target : MutableList <T >) {
8290 val position: Int = source.position
8391 val size: Int = source.size()
8492 for (i in 0 until size) {
@@ -104,8 +112,7 @@ public data class ChangeDelta<T>(override val source: Chunk<T>, override val tar
104112}
105113
106114public data class DeleteDelta <T >(override val source : Chunk <T >, override val target : Chunk <T >) : Delta<T>(DeltaType .DELETE ) {
107- override fun applyTo (target : MutableList <T >) {
108- verifyChunk(target)
115+ protected override fun applyTo (target : MutableList <T >) {
109116 val position = source.position
110117 for (i in 0 until source.size()) {
111118 target.removeAt(position)
@@ -124,8 +131,7 @@ public data class DeleteDelta<T>(override val source: Chunk<T>, override val tar
124131}
125132
126133public data class InsertDelta <T >(override val source : Chunk <T >, override val target : Chunk <T >) : Delta<T>(DeltaType .INSERT ) {
127- override fun applyTo (target : MutableList <T >) {
128- verifyChunk(target)
134+ protected override fun applyTo (target : MutableList <T >) {
129135 val position = this .source.position
130136 this .target.lines.forEachIndexed { i, line ->
131137 target.add(position + i, line)
@@ -143,7 +149,7 @@ public data class InsertDelta<T>(override val source: Chunk<T>, override val tar
143149}
144150
145151public data class EqualDelta <T >(override val source : Chunk <T >, override val target : Chunk <T >) : Delta<T>(DeltaType .EQUAL ) {
146- override fun applyTo (target : MutableList <T >): Unit = verifyChunk(target)
152+ protected override fun applyTo (target : MutableList <T >): Unit = Unit
147153
148154 override fun restore (target : MutableList <T >): Unit = Unit
149155
0 commit comments