@@ -62,12 +62,21 @@ public sealed class Delta<T>(public val type: DeltaType) {
62
62
* @throws PatchFailedException
63
63
*/
64
64
@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)
67
67
}
68
68
69
69
@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 >)
71
80
72
81
public abstract fun restore (target : MutableList <T >)
73
82
@@ -77,8 +86,7 @@ public sealed class Delta<T>(public val type: DeltaType) {
77
86
public abstract fun withChunks (original : Chunk <T >, revised : Chunk <T >): Delta <T >
78
87
}
79
88
public 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 >) {
82
90
val position: Int = source.position
83
91
val size: Int = source.size()
84
92
for (i in 0 until size) {
@@ -104,8 +112,7 @@ public data class ChangeDelta<T>(override val source: Chunk<T>, override val tar
104
112
}
105
113
106
114
public 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 >) {
109
116
val position = source.position
110
117
for (i in 0 until source.size()) {
111
118
target.removeAt(position)
@@ -124,8 +131,7 @@ public data class DeleteDelta<T>(override val source: Chunk<T>, override val tar
124
131
}
125
132
126
133
public 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 >) {
129
135
val position = this .source.position
130
136
this .target.lines.forEachIndexed { i, line ->
131
137
target.add(position + i, line)
@@ -143,7 +149,7 @@ public data class InsertDelta<T>(override val source: Chunk<T>, override val tar
143
149
}
144
150
145
151
public 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
147
153
148
154
override fun restore (target : MutableList <T >): Unit = Unit
149
155
0 commit comments