All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.realm.kotlin.internal.CollectionChangeSetBuilderImpl.kt Maven / Gradle / Ivy

Go to download

Library code for Realm Kotlin. This artifact is not supposed to be consumed directly, but through 'io.realm.kotlin:gradle-plugin:1.11.1' instead.

There is a newer version: 3.0.0
Show newest version
/*
 * Copyright 2022 Realm Inc.
 *
 * 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 io.realm.kotlin.internal

import io.realm.kotlin.internal.interop.ArrayAccessor
import io.realm.kotlin.internal.interop.CollectionChangeSetBuilder
import io.realm.kotlin.internal.interop.MapChangeSetBuilder
import io.realm.kotlin.internal.interop.RealmChangesPointer
import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.notifications.DictionaryChangeSet
import io.realm.kotlin.notifications.ListChangeSet
import io.realm.kotlin.notifications.ListChangeSet.Range
import io.realm.kotlin.notifications.SetChangeSet

// --------------------------------------------------------
// Collections: List and Set
// --------------------------------------------------------

internal abstract class CollectionChangeSetBuilderImpl(
    change: RealmChangesPointer
) : CollectionChangeSetBuilder() {

    init {
        RealmInterop.realm_collection_changes_get_indices(change, this)
        RealmInterop.realm_collection_changes_get_ranges(change, this)
    }

    override fun initIndicesArray(size: Int, indicesAccessor: ArrayAccessor): IntArray =
        IntArray(size) { index -> indicesAccessor(index) }

    override fun initRangesArray(
        size: Int,
        fromAccessor: ArrayAccessor,
        toAccessor: ArrayAccessor
    ): Array =
        Array(size) { index ->
            val from: Int = fromAccessor(index)
            val to: Int = toAccessor(index)
            Range(from, to - from)
        }
}

internal class ListChangeSetBuilderImpl(
    change: RealmChangesPointer
) : CollectionChangeSetBuilderImpl(change) {

    override fun build(): ListChangeSet = object : ListChangeSet {
        override val deletions: IntArray =
            [email protected]

        override val insertions: IntArray =
            [email protected]

        override val changes: IntArray =
            [email protected]

        override val deletionRanges: Array =
            [email protected]

        override val insertionRanges: Array =
            [email protected]

        override val changeRanges: Array =
            [email protected]
    }
}

internal class SetChangeSetBuilderImpl(
    change: RealmChangesPointer
) : CollectionChangeSetBuilderImpl(change) {

    override fun build(): SetChangeSet = object : SetChangeSet {
        override val insertions: Int
            get() = [email protected]
        override val deletions: Int
            get() = [email protected]
    }
}

// --------------------------------------------------------
// Dictionary: uses a different changeset internally
// --------------------------------------------------------

internal class DictionaryChangeSetBuilderImpl(
    change: RealmChangesPointer
) : MapChangeSetBuilder() {

    init {
        RealmInterop.realm_dictionary_get_changes(change, this)
    }

    override fun initDeletions(keys: Array) {
        this.deletedKeys = keys
    }

    override fun initInsertions(keys: Array) {
        this.insertedKeys = keys
    }

    override fun initModifications(keys: Array) {
        this.modifiedKeys = keys
    }

    override fun build(): DictionaryChangeSet {
        return object : DictionaryChangeSet {
            override val deletions: Array
                get() = [email protected]
            override val insertions: Array
                get() = [email protected]
            override val changes: Array
                get() = [email protected]
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy