修改、新增方法

dev
ZhouXY108 2024-03-15 09:28:09 +08:00
parent 9a0b6404cb
commit 3d5a3ddbee
2 changed files with 19 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.commons.base; package xyz.zhouxy.plusone.commons.base;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
@ -10,6 +11,10 @@ public final class Ref<T> {
private T value; private T value;
public Ref() {
this.value = null;
}
public Ref(T value) { public Ref(T value) {
this.value = value; this.value = value;
} }
@ -22,10 +27,22 @@ public final class Ref<T> {
this.value = value; this.value = value;
} }
public void apply(UnaryOperator<T> operator) { public void transform(UnaryOperator<T> operator) {
this.value = operator.apply(this.value); this.value = operator.apply(this.value);
} }
public boolean isNull() {
return this.value == null;
}
public boolean isNotNull() {
return this.value != null;
}
public void execute(Consumer<T> consumer) {
consumer.accept(value);
}
@Override @Override
public String toString() { public String toString() {
return String.format("Ref[%s]", value); return String.format("Ref[%s]", value);

View File

@ -24,7 +24,7 @@ class RefTests {
} }
void apply(Ref<String> strRef) { void apply(Ref<String> strRef) {
strRef.apply(str -> "Hello " + str); strRef.transform(str -> "Hello " + str);
} }
@Test @Test