forked from plusone/plusone-commons
在 Ref 对象内部修改 value 的方法定义为 transformValue;新增 Ref 相关方法;删除各种衍生的 XxxRef
This commit is contained in:
@@ -27,78 +27,20 @@ class RefTests {
|
||||
|
||||
@Test
|
||||
void testRef() {
|
||||
Ref<String> strRef = new Ref<>("ZhouXY");
|
||||
Ref<String> strRef = Ref.of("ZhouXY");
|
||||
assertTrue(strRef.checkValue("ZhouXY"::equals));
|
||||
assertFalse(strRef.checkValue("ZhouXY1"::equals));
|
||||
apply(strRef);
|
||||
assertEquals("Hello ZhouXY", strRef.getValue());
|
||||
assertTrue(strRef.checkValue("Hello ZhouXY"::equals));
|
||||
log.info("strRef: {}", strRef);
|
||||
|
||||
Ref<String> intStringRef = Ref.of("108");
|
||||
Ref<Integer> integerRef = intStringRef.transform(Integer::parseInt);
|
||||
assertEquals(108, integerRef.getValue());
|
||||
}
|
||||
|
||||
void apply(Ref<String> strRef) {
|
||||
strRef.transform(str -> "Hello " + str);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBoolRef() {
|
||||
BoolRef boolRef = new BoolRef(false);
|
||||
apply(boolRef);
|
||||
assertTrue(boolRef.getValue());
|
||||
log.info("boolRef: {}", boolRef);
|
||||
}
|
||||
|
||||
void apply(BoolRef boolRef) {
|
||||
boolRef.setValue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCharRef() {
|
||||
CharRef charRef = new CharRef('T');
|
||||
|
||||
apply(false, charRef);
|
||||
assertEquals('0', charRef.getValue());
|
||||
log.info("charRef: {}", charRef);
|
||||
|
||||
apply(true, charRef);
|
||||
assertEquals('1', charRef.getValue());
|
||||
log.info("charRef: {}", charRef);
|
||||
}
|
||||
|
||||
void apply(boolean condition, CharRef charRef) {
|
||||
charRef.apply(c -> condition ? '1' : '0');
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoubleRef() {
|
||||
DoubleRef doubleRef = new DoubleRef(2.33);
|
||||
apply(88.108, doubleRef);
|
||||
assertEquals(2.33 * 88.108, doubleRef.getValue());
|
||||
log.info("doubleRef: {}", doubleRef);
|
||||
}
|
||||
|
||||
void apply(double num, DoubleRef doubleRef) {
|
||||
doubleRef.apply(d -> d * num);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntRef() {
|
||||
IntRef intRef = new IntRef(108);
|
||||
apply(88, intRef);
|
||||
assertEquals(108 - 88, intRef.getValue());
|
||||
log.info("intRef: {}", intRef);
|
||||
}
|
||||
|
||||
void apply(int num, IntRef intRef) {
|
||||
intRef.apply(d -> d - num);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLongRef() {
|
||||
LongRef longRef = new LongRef(108L);
|
||||
apply(88L, longRef);
|
||||
assertEquals(108L + 88L, longRef.getValue());
|
||||
log.info("intRef: {}", longRef);
|
||||
}
|
||||
|
||||
void apply(long num, LongRef longRef) {
|
||||
longRef.apply(d -> d + num);
|
||||
strRef.transformValue(str -> "Hello " + str);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user