forked from plusone/plusone-commons
修改 apply 方法名为 transformValue;新增 int、long、double 的 checkValue 方法;新增相互转换的方法;XxxRef 全部设为 final 类。
parent
aa2bbc5b20
commit
fba195afc7
|
@ -22,7 +22,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
|||
import xyz.zhouxy.plusone.commons.function.BoolUnaryOperator;
|
||||
|
||||
@Beta
|
||||
public class BoolRef {
|
||||
public final class BoolRef {
|
||||
|
||||
private boolean value;
|
||||
|
||||
|
@ -43,10 +43,18 @@ public class BoolRef {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public void apply(BoolUnaryOperator operator) {
|
||||
public void transformValue(BoolUnaryOperator operator) {
|
||||
this.value = operator.applyAsBool(this.value);
|
||||
}
|
||||
|
||||
public boolean isTrue() {
|
||||
return getValue();
|
||||
}
|
||||
|
||||
public boolean isFalse() {
|
||||
return !value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("BoolRef[%s]", value);
|
||||
|
|
|
@ -22,7 +22,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
|||
import xyz.zhouxy.plusone.commons.function.ByteUnaryOperator;
|
||||
|
||||
@Beta
|
||||
public class ByteRef {
|
||||
public final class ByteRef {
|
||||
|
||||
private byte value;
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class ByteRef {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public void apply(ByteUnaryOperator operator) {
|
||||
public void transformValue(ByteUnaryOperator operator) {
|
||||
this.value = operator.applyAsByte(this.value);
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
|||
import xyz.zhouxy.plusone.commons.function.CharUnaryOperator;
|
||||
|
||||
@Beta
|
||||
public class CharRef {
|
||||
public final class CharRef {
|
||||
|
||||
private char value;
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class CharRef {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public void apply(CharUnaryOperator operator) {
|
||||
public void transformValue(CharUnaryOperator operator) {
|
||||
this.value = operator.applyAsChar(this.value);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import java.util.function.DoublePredicate;
|
||||
import java.util.function.DoubleUnaryOperator;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
@ -23,7 +24,7 @@ import com.google.common.annotations.Beta;
|
|||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
|
||||
@Beta
|
||||
public class DoubleRef {
|
||||
public final class DoubleRef {
|
||||
|
||||
private double value;
|
||||
|
||||
|
@ -44,10 +45,14 @@ public class DoubleRef {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public void apply(DoubleUnaryOperator operator) {
|
||||
public void transformValue(DoubleUnaryOperator operator) {
|
||||
this.value = operator.applyAsDouble(this.value);
|
||||
}
|
||||
|
||||
public boolean checkValue(DoublePredicate predicate) {
|
||||
return predicate.test(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("DoubleRef[%s]", value);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.function.IntUnaryOperator;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
@ -23,7 +24,7 @@ import com.google.common.annotations.Beta;
|
|||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
|
||||
@Beta
|
||||
public class IntRef {
|
||||
public final class IntRef {
|
||||
|
||||
private int value;
|
||||
|
||||
|
@ -44,10 +45,14 @@ public class IntRef {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public void apply(IntUnaryOperator operator) {
|
||||
public void transformValue(IntUnaryOperator operator) {
|
||||
this.value = operator.applyAsInt(this.value);
|
||||
}
|
||||
|
||||
public boolean checkValue(IntPredicate predicate) {
|
||||
return predicate.test(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("IntRef[%s]", value);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import java.util.function.LongPredicate;
|
||||
import java.util.function.LongUnaryOperator;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
@ -23,7 +24,7 @@ import com.google.common.annotations.Beta;
|
|||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
|
||||
@Beta
|
||||
public class LongRef {
|
||||
public final class LongRef {
|
||||
|
||||
private long value;
|
||||
|
||||
|
@ -44,10 +45,14 @@ public class LongRef {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public void apply(LongUnaryOperator operator) {
|
||||
public void transformValue(LongUnaryOperator operator) {
|
||||
this.value = operator.applyAsLong(this.value);
|
||||
}
|
||||
|
||||
public boolean checkValue(LongPredicate predicate) {
|
||||
return predicate.test(this.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("LongRef[%s]", value);
|
||||
|
|
|
@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.base;
|
|||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
@ -55,10 +57,22 @@ public final class Ref<T> {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public void transform(UnaryOperator<T> operator) {
|
||||
public void transformValue(UnaryOperator<T> operator) {
|
||||
this.value = operator.apply(this.value);
|
||||
}
|
||||
|
||||
public <R> Ref<R> transform(Function<? super T, R> function) {
|
||||
return Ref.of(function.apply(this.value));
|
||||
}
|
||||
|
||||
public boolean checkValue(Predicate<? super T> predicate) {
|
||||
return predicate.test(this.value);
|
||||
}
|
||||
|
||||
public void execute(Consumer<? super T> consumer) {
|
||||
consumer.accept(value);
|
||||
}
|
||||
|
||||
public boolean isNull() {
|
||||
return this.value == null;
|
||||
}
|
||||
|
@ -67,10 +81,44 @@ public final class Ref<T> {
|
|||
return this.value != null;
|
||||
}
|
||||
|
||||
public void execute(Consumer<? super T> consumer) {
|
||||
consumer.accept(value);
|
||||
// ================================
|
||||
// #region - from
|
||||
// ================================
|
||||
|
||||
@StaticFactoryMethod(Ref.class)
|
||||
public static Ref<Boolean> from(BoolRef boolRef) {
|
||||
return new Ref<>(boolRef.getValue());
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Ref.class)
|
||||
public static Ref<Byte> from(ByteRef byteRef) {
|
||||
return new Ref<>(byteRef.getValue());
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Ref.class)
|
||||
public static Ref<Character> from(CharRef charRef) {
|
||||
return new Ref<>(charRef.getValue());
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Ref.class)
|
||||
public static Ref<Double> from(DoubleRef doubleRef) {
|
||||
return new Ref<>(doubleRef.getValue());
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Ref.class)
|
||||
public static Ref<Integer> from(IntRef intRef) {
|
||||
return new Ref<>(intRef.getValue());
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(Ref.class)
|
||||
public static Ref<Long> from(LongRef longRef) {
|
||||
return new Ref<>(longRef.getValue());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - from
|
||||
// ================================
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Ref[%s]", value);
|
||||
|
|
|
@ -34,9 +34,12 @@ class RefTests {
|
|||
@Test
|
||||
void testRef() {
|
||||
Ref<String> strRef = new Ref<>("ZhouXY");
|
||||
assertTrue(strRef.checkValue("ZhouXY"::equals));
|
||||
apply(strRef);
|
||||
assertEquals("Hello ZhouXY", strRef.getValue());
|
||||
log.info("strRef: {}", strRef);
|
||||
|
||||
assertTrue(strRef.checkValue(str -> str.length() == 12));
|
||||
}
|
||||
|
||||
void apply(Ref<String> strRef) {
|
||||
|
@ -69,7 +72,7 @@ class RefTests {
|
|||
}
|
||||
|
||||
void apply(boolean condition, CharRef charRef) {
|
||||
charRef.apply(c -> condition ? '1' : '0');
|
||||
charRef.transformValue(c -> condition ? '1' : '0');
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -81,7 +84,7 @@ class RefTests {
|
|||
}
|
||||
|
||||
void apply(double num, DoubleRef doubleRef) {
|
||||
doubleRef.apply(d -> d * num);
|
||||
doubleRef.transformValue(d -> d * num);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -93,7 +96,7 @@ class RefTests {
|
|||
}
|
||||
|
||||
void apply(int num, IntRef intRef) {
|
||||
intRef.apply(d -> d - num);
|
||||
intRef.transformValue(d -> d - num);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -105,6 +108,6 @@ class RefTests {
|
|||
}
|
||||
|
||||
void apply(long num, LongRef longRef) {
|
||||
longRef.apply(d -> d + num);
|
||||
longRef.transformValue(d -> d + num);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue