Replace get() and set() to getValue() and setValue() (property delegates)
This commit is contained in:
@@ -115,11 +115,11 @@ public object Delegates {
|
||||
private class NotNullVar<T: Any>() : ReadWriteProperty<Any?, T> {
|
||||
private var value: T? = null
|
||||
|
||||
public override fun get(thisRef: Any?, property: PropertyMetadata): T {
|
||||
public override fun getValue(thisRef: Any?, property: PropertyMetadata): T {
|
||||
return value ?: throw IllegalStateException("Property ${property.name} should be initialized before get.")
|
||||
}
|
||||
|
||||
public override fun set(thisRef: Any?, property: PropertyMetadata, value: T) {
|
||||
public override fun setValue(thisRef: Any?, property: PropertyMetadata, value: T) {
|
||||
this.value = value
|
||||
}
|
||||
}
|
||||
@@ -152,11 +152,11 @@ public abstract class ObservableProperty<T>(initialValue: T) : ReadWriteProperty
|
||||
*/
|
||||
protected open fun afterChange (property: PropertyMetadata, oldValue: T, newValue: T): Unit {}
|
||||
|
||||
public override fun get(thisRef: Any?, property: PropertyMetadata): T {
|
||||
public override fun getValue(thisRef: Any?, property: PropertyMetadata): T {
|
||||
return value
|
||||
}
|
||||
|
||||
public override fun set(thisRef: Any?, property: PropertyMetadata, value: T) {
|
||||
public override fun setValue(thisRef: Any?, property: PropertyMetadata, value: T) {
|
||||
val oldValue = this.value
|
||||
if (!beforeChange(property, oldValue, value)) {
|
||||
return
|
||||
@@ -179,7 +179,7 @@ private fun unescape(value: Any?): Any? {
|
||||
private class LazyVal<T>(private val initializer: () -> T) : ReadOnlyProperty<Any?, T> {
|
||||
private var value: Any? = null
|
||||
|
||||
public override fun get(thisRef: Any?, property: PropertyMetadata): T {
|
||||
public override fun getValue(thisRef: Any?, property: PropertyMetadata): T {
|
||||
if (value == null) {
|
||||
value = escape(initializer())
|
||||
}
|
||||
@@ -191,7 +191,7 @@ private class BlockingLazyVal<T>(lock: Any?, private val initializer: () -> T) :
|
||||
private val lock = lock ?: this
|
||||
@Volatile private var value: Any? = null
|
||||
|
||||
public override fun get(thisRef: Any?, property: PropertyMetadata): T {
|
||||
public override fun getValue(thisRef: Any?, property: PropertyMetadata): T {
|
||||
val _v1 = value
|
||||
if (_v1 != null) {
|
||||
return unescape(_v1) as T
|
||||
@@ -239,7 +239,7 @@ public abstract class MapVal<T, K, out V>() : ReadOnlyProperty<T, V> {
|
||||
throw NoSuchElementException("The value for property ${property.name} is missing in $ref.")
|
||||
}
|
||||
|
||||
public override fun get(thisRef: T, property: PropertyMetadata) : V {
|
||||
public override fun getValue(thisRef: T, property: PropertyMetadata) : V {
|
||||
val map = map(thisRef)
|
||||
val key = key(property)
|
||||
return map.getOrElse(key, { default(thisRef, property) }) as V
|
||||
@@ -255,10 +255,16 @@ public abstract class MapVal<T, K, out V>() : ReadOnlyProperty<T, V> {
|
||||
public abstract class MapVar<T, K, V>() : MapVal<T, K, V>(), ReadWriteProperty<T, V> {
|
||||
protected abstract override fun map(ref: T): MutableMap<in K, Any?>
|
||||
|
||||
public override fun set(thisRef: T, property: PropertyMetadata, value: V) {
|
||||
public override fun setValue(thisRef: T, property: PropertyMetadata, value: V) {
|
||||
val map = map(thisRef)
|
||||
map.put(key(property), value)
|
||||
}
|
||||
|
||||
override fun get(thisRef: T, property: PropertyMetadata) = getValue(thisRef, property)
|
||||
|
||||
override fun getValue(thisRef: T, property: PropertyMetadata): V {
|
||||
return super<MapVal>.getValue(thisRef, property)
|
||||
}
|
||||
}
|
||||
|
||||
private val propertyNameSelector: (PropertyMetadata) -> String = {it.name}
|
||||
|
||||
@@ -17,7 +17,11 @@ public interface ReadOnlyProperty<in R, out T> {
|
||||
* @param property the metadata for the property.
|
||||
* @return the property value.
|
||||
*/
|
||||
public fun get(thisRef: R, property: PropertyMetadata): T
|
||||
public fun getValue(thisRef: R, property: PropertyMetadata): T = get(thisRef, property)
|
||||
|
||||
//TODO drop after bootstrap
|
||||
@Deprecated("Use getValue() instead.", ReplaceWith("getValue(thisRef, property)"))
|
||||
public fun get(thisRef: R, property: PropertyMetadata): T = getValue(thisRef, property)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +40,11 @@ public interface ReadWriteProperty<in R, T> {
|
||||
* @param property the metadata for the property.
|
||||
* @return the property value.
|
||||
*/
|
||||
public fun get(thisRef: R, property: PropertyMetadata): T
|
||||
public fun getValue(thisRef: R, property: PropertyMetadata): T = get(thisRef, property)
|
||||
|
||||
//TODO drop after bootstrap
|
||||
@Deprecated("Use getValue() instead.", ReplaceWith("getValue(thisRef, property)"))
|
||||
public fun get(thisRef: R, property: PropertyMetadata): T = getValue(thisRef, property)
|
||||
|
||||
/**
|
||||
* Sets the value of the property for the given object.
|
||||
@@ -44,5 +52,11 @@ public interface ReadWriteProperty<in R, T> {
|
||||
* @param property the metadata for the property.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public fun set(thisRef: R, property: PropertyMetadata, value: T)
|
||||
public fun setValue(thisRef: R, property: PropertyMetadata, value: T) {
|
||||
set(thisRef, property, value)
|
||||
}
|
||||
|
||||
//TODO drop after bootstrap
|
||||
@Deprecated("Use setValue() instead.", ReplaceWith("setValue(thisRef, property, value)"))
|
||||
public fun set(thisRef: R, property: PropertyMetadata, value: T) = setValue(thisRef, property, value)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@ package kotlin.properties
|
||||
*
|
||||
* @throws NoSuchElementException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [withDefault]).
|
||||
*/
|
||||
public fun <V> Map<in String, *>.getValue(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
|
||||
|
||||
@Deprecated("Use getValue() instead")
|
||||
public fun <V> Map<in String, *>.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
|
||||
|
||||
/**
|
||||
@@ -22,6 +25,10 @@ public fun <V> Map<in String, *>.get(thisRef: Any?, property: PropertyMetadata):
|
||||
* @throws NoSuchElementException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [withDefault]).
|
||||
*/
|
||||
@kotlin.jvm.JvmName("getVar")
|
||||
public fun <V> MutableMap<in String, in V>.getValue(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
|
||||
|
||||
@Deprecated("Use getValue() instead")
|
||||
@kotlin.jvm.JvmName("getVarDeprecated")
|
||||
public fun <V> MutableMap<in String, in V>.get(thisRef: Any?, property: PropertyMetadata): V = getOrImplicitDefault(property.name) as V
|
||||
|
||||
/**
|
||||
@@ -30,6 +37,11 @@ public fun <V> MutableMap<in String, in V>.get(thisRef: Any?, property: Property
|
||||
* @param property the metadata for the property, used to get the name of property and store the value associated with that name in the map.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public fun <V> MutableMap<in String, in V>.setValue(thisRef: Any?, property: PropertyMetadata, value: V) {
|
||||
this.put(property.name, value)
|
||||
}
|
||||
|
||||
@Deprecated("Use setValue() instead")
|
||||
public fun <V> MutableMap<in String, in V>.set(thisRef: Any?, property: PropertyMetadata, value: V) {
|
||||
this.put(property.name, value)
|
||||
}
|
||||
|
||||
@@ -74,6 +74,9 @@ public fun lazy<T>(lock: Any?, initializer: () -> T): Lazy<T> = SynchronizedLazy
|
||||
* This extension allows to use instances of Lazy for property delegation:
|
||||
* `val property: String by lazy { initializer }`
|
||||
*/
|
||||
public fun <T> Lazy<T>.getValue(thisRef: Any?, property: PropertyMetadata): T = value
|
||||
|
||||
@Deprecated("Use getValue() instead")
|
||||
public fun <T> Lazy<T>.get(thisRef: Any?, property: PropertyMetadata): T = value
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user