com.github.tonivade.purefun.effect.Ref Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun.effect;
import static com.github.tonivade.purefun.Unit.unit;
import static java.util.Objects.requireNonNull;
import java.util.concurrent.atomic.AtomicReference;
import com.github.tonivade.purefun.Operator1;
import com.github.tonivade.purefun.Unit;
public final class Ref {
private final AtomicReference value;
private Ref(AtomicReference value) {
this.value = requireNonNull(value);
}
public UIO get() {
return UIO.task(value::get);
}
public UIO set(A newValue) {
return UIO.task(() -> { value.set(newValue); return unit(); });
}
public UIO lazySet(A newValue) {
return UIO.task(() -> { value.lazySet(newValue); return unit(); });
}
public UIO getAndSet(A newValue) {
return UIO.task(() -> value.getAndSet(newValue));
}
public UIO updateAndGet(Operator1 update) {
return UIO.task(() -> value.updateAndGet(update::apply));
}
public UIO getAndUpdate(Operator1 update) {
return UIO.task(() -> value.getAndUpdate(update::apply));
}
public static Ref of(A value) {
return new Ref<>(new AtomicReference<>(value));
}
@Override
public String toString() {
return "Ref(" + value.get() + ")";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy