Refactor names for AttributesBuilder behavior

This commit is contained in:
Alexander Nozik 2024-03-27 08:12:39 +03:00
parent 1be6a5ca0e
commit 461e5a7c54
3 changed files with 12 additions and 9 deletions

View File

@ -84,7 +84,7 @@ public fun <A : Attribute<Unit>> Attributes.withAttribute(attribute: A): Attribu
* Create a new [Attributes] by modifying the current one * Create a new [Attributes] by modifying the current one
*/ */
public fun <T> Attributes.modify(block: AttributesBuilder<T>.() -> Unit): Attributes = Attributes<T> { public fun <T> Attributes.modify(block: AttributesBuilder<T>.() -> Unit): Attributes = Attributes<T> {
from(this@modify) putAll(this@modify)
block() block()
} }

View File

@ -34,13 +34,18 @@ public class AttributesBuilder<out O> internal constructor(
set(this, value) set(this, value)
} }
public fun from(attributes: Attributes) { public infix fun <V> Attribute<V>.put(value: V?) {
set(this, value)
}
/**
* Put all attributes for given [attributes]
*/
public fun putAll(attributes: Attributes) {
map.putAll(attributes.content) map.putAll(attributes.content)
} }
public fun <V> SetAttribute<V>.add( public infix fun <V> SetAttribute<V>.add(attrValue: V) {
attrValue: V,
) {
val currentSet: Set<V> = get(this) ?: emptySet() val currentSet: Set<V> = get(this) ?: emptySet()
map[this] = currentSet + attrValue map[this] = currentSet + attrValue
} }
@ -48,9 +53,7 @@ public class AttributesBuilder<out O> internal constructor(
/** /**
* Remove an element from [SetAttribute] * Remove an element from [SetAttribute]
*/ */
public fun <V> SetAttribute<V>.remove( public infix fun <V> SetAttribute<V>.remove(attrValue: V) {
attrValue: V,
) {
val currentSet: Set<V> = get(this) ?: emptySet() val currentSet: Set<V> = get(this) ?: emptySet()
map[this] = currentSet - attrValue map[this] = currentSet - attrValue
} }

View File

@ -43,7 +43,7 @@ public object CMOptimizerData : SetAttribute<SymbolIndexer.() -> OptimizationDat
* Specify Commons-maths optimization data. * Specify Commons-maths optimization data.
*/ */
public fun AttributesBuilder<FunctionOptimization<Double>>.cmOptimizationData(data: SymbolIndexer.() -> OptimizationData) { public fun AttributesBuilder<FunctionOptimization<Double>>.cmOptimizationData(data: SymbolIndexer.() -> OptimizationData) {
CMOptimizerData.add(data) CMOptimizerData add data
} }
public fun AttributesBuilder<FunctionOptimization<Double>>.simplexSteps(vararg steps: Pair<Symbol, Double>) { public fun AttributesBuilder<FunctionOptimization<Double>>.simplexSteps(vararg steps: Pair<Symbol, Double>) {