commonMain.io.realm.kotlin.ext.IterableExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of library-base-jvm Show documentation
Show all versions of library-base-jvm Show documentation
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.
/*
* 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.ext
import io.realm.kotlin.internal.UnmanagedRealmDictionary
import io.realm.kotlin.internal.UnmanagedRealmList
import io.realm.kotlin.internal.UnmanagedRealmSet
import io.realm.kotlin.types.RealmDictionary
import io.realm.kotlin.types.RealmDictionaryEntrySet
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmSet
/**
* Instantiates an **unmanaged** [RealmList] containing all the elements of this iterable.
*/
public fun Iterable.toRealmList(): RealmList {
if (this is Collection) {
return when (size) {
0 -> UnmanagedRealmList()
1 -> realmListOf(if (this is List) get(0) else iterator().next())
else -> UnmanagedRealmList().apply { addAll(this@toRealmList) }
}
}
return UnmanagedRealmList().apply { addAll(this@toRealmList) }
}
/**
* Instantiates an **unmanaged** [RealmSet] containing all the elements of this iterable.
*/
public fun Iterable.toRealmSet(): RealmSet {
if (this is Collection) {
return when (size) {
0 -> UnmanagedRealmSet()
1 -> realmSetOf(if (this is List) get(0) else iterator().next())
else -> UnmanagedRealmSet().apply { addAll(this@toRealmSet) }
}
}
return UnmanagedRealmSet().apply { addAll(this@toRealmSet) }
}
/**
* Instantiates an **unmanaged** [RealmDictionary] containing all the elements of this iterable of
* [Pair]s of [String]s and [T]s.
*/
public fun Iterable>.toRealmDictionary(): RealmDictionary {
if (this is Collection) {
return when (size) {
0 -> UnmanagedRealmDictionary()
1 -> realmDictionaryOf(if (this is List) get(0) else iterator().next())
else -> UnmanagedRealmDictionary().apply {
this.putAll(this@toRealmDictionary)
}
}
}
return UnmanagedRealmDictionary().apply {
this.putAll(this@toRealmDictionary)
}
}
/**
* Instantiates an **unmanaged** [RealmDictionary] containing all the elements of the receiver
* [RealmDictionaryEntrySet].
*/
public fun RealmDictionaryEntrySet.toRealmDictionary(): RealmDictionary {
return when (size) {
0 -> UnmanagedRealmDictionary()
else -> map { Pair(it.key, it.value) }
.toRealmDictionary()
}
}
/**
* Instantiates an **unmanaged** [RealmDictionary] containing all the elements of the receiver
* dictionary represented by a [Map] of [String] to [T] pairs.
*/
public fun Map.toRealmDictionary(): RealmDictionary =
UnmanagedRealmDictionary(this)
© 2015 - 2024 Weber Informatics LLC | Privacy Policy