Skip to content

Commit 8fa0885

Browse files
committed
Updated PlaceHolder interface and MutableValue class.
Updated JavaDoc for MutableValue class. Updated Receiver class with return type for fluent API designs. Added static method MutableValue.of(value) for easy creation. Added static method MutableValue.empty() for easy creation. Added method MutableValue.getValue() for exception-free retrieval of empty values.
1 parent d978e7d commit 8fa0885

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

main-java/app/zoftwhere/function/PlaceHolder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ default boolean isEmpty() {
1313
return !isPresent();
1414
}
1515

16+
default T getValue() {
17+
return optional().orElse(null);
18+
}
19+
1620
default T orElse(T other) {
1721
return optional().orElse(other);
1822
}

main-java/app/zoftwhere/mutable/MutableValue.java

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,65 @@
55

66
import app.zoftwhere.function.PlaceHolder;
77

8+
/**
9+
* A mutable value.
10+
*
11+
* @param <T> the type
12+
*/
813
public class MutableValue<T> implements PlaceHolder<T> {
914

1015
private T value;
1116

17+
/**
18+
* Returns a {@code MutableValue} describing the given value.
19+
*
20+
* @param value the possibly-{@code null} value to describe
21+
* @param <T> the type of the value
22+
* @return {@code MutableValue} instance with the initial specified value
23+
*/
24+
public static <T> MutableValue<T> ofNullable(T value) {
25+
return new MutableValue<>(value);
26+
}
27+
28+
/**
29+
* Returns an empty {@code MutableValue}.
30+
*
31+
* @param <T> the type of the value
32+
* @return {@code MutableValue} instance with a null initial value.
33+
*/
34+
public static <T> MutableValue<T> empty() {
35+
return new MutableValue<>();
36+
}
37+
38+
/**
39+
* Constructs a mutable value with initial value of null.
40+
*/
1241
public MutableValue() {
1342
this.value = null;
1443
}
1544

45+
/**
46+
* Constructs a mutable value.
47+
*
48+
* @param value initial value
49+
*/
1650
public MutableValue(T value) {
1751
this.value = value;
1852
}
1953

54+
/**
55+
* Set mutable value.
56+
*/
2057
@Override
2158
public Void set(T value) {
2259
this.value = value;
60+
return null;
2361
}
2462

2563
/**
26-
* Retrieve variable value.
64+
* Retrieve mutable value.
2765
*
28-
* @return Stored value.
66+
* @return Stored value
2967
* @exception NoSuchElementException if no value is present
3068
*/
3169
@Override
@@ -36,18 +74,55 @@ public T get() {
3674
return value;
3775
}
3876

77+
/**
78+
* Retrieve mutable value.
79+
*
80+
* @return Stored value
81+
*/
82+
@Override
83+
public T getValue() {
84+
return value;
85+
}
86+
87+
/**
88+
* Returns an {@code Optional} describing the current value.
89+
*
90+
* @return an {@code Optional} with a present value if the specified value is non-{@code null}, otherwise an empty
91+
* {@code Optional}
92+
*/
3993
public Optional<T> optional() {
4094
return Optional.ofNullable(value);
4195
}
4296

97+
/**
98+
* Check if value is not null.
99+
*
100+
* @return {@code true} if a value is present, otherwise {@code false}
101+
*/
43102
@Override
44103
public boolean isPresent() {
45104
return value != null;
46105
}
47106

107+
/**
108+
* Check if the value is null.
109+
*
110+
* @return {@code true} if a value is null, otherwise {@code false}
111+
*/
48112
@Override
49113
public boolean isEmpty() {
50114
return value == null;
51115
}
52116

117+
/**
118+
* Retrieves non-null mutable value, otherwise returns {@code other}.
119+
*
120+
* @param other the value to be returned, if no value is present. May be {@code null}
121+
* @return the value, if present, otherwise {@code other}
122+
*/
123+
@Override
124+
public T orElse(T other) {
125+
return value != null ? value : other;
126+
}
127+
53128
}

test-java/app/zoftwhere/mutable/MutableValueTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.junit.jupiter.api.Assertions.assertEquals;
88
import static org.junit.jupiter.api.Assertions.assertFalse;
99
import static org.junit.jupiter.api.Assertions.assertNotNull;
10+
import static org.junit.jupiter.api.Assertions.assertNull;
1011
import static org.junit.jupiter.api.Assertions.assertTrue;
1112
import static org.junit.jupiter.api.Assertions.fail;
1213

@@ -22,6 +23,16 @@ void testConstructor() {
2223
assertEquals(value, actual.get());
2324
}
2425

26+
@Test
27+
void testOfNullable() {
28+
var value = "new value";
29+
var actual = MutableValue.ofNullable(value);
30+
assertNotNull(value);
31+
assertNotNull(actual);
32+
assertNotNull(actual.get());
33+
assertEquals(value, actual.get());
34+
}
35+
2536
@Test
2637
void testOptional() {
2738
assertFalse(new MutableValue<String>().optional().isPresent());
@@ -34,6 +45,20 @@ void testOptional() {
3445
assertEquals(input, fixed.optional().get());
3546
}
3647

48+
@Test
49+
void testEmpty() {
50+
final var value = MutableValue.<String>empty();
51+
assertNull(value.getValue());
52+
assertTrue(value.isEmpty());
53+
}
54+
55+
@Test
56+
void testGetValue() {
57+
final var value = MutableValue.<String>empty();
58+
value.set("Hello");
59+
assertEquals("Hello", value.getValue());
60+
}
61+
3762
@Test
3863
void testOrElse() {
3964
String output;
@@ -65,7 +90,6 @@ void testIsPresent() {
6590

6691
@Test
6792
void testIsEmpty() {
68-
assertTrue(new MutableValue<String>().isEmpty());
6993
assertTrue(new MutableValue<String>().isEmpty());
7094
assertFalse(new MutableValue<>("").isEmpty());
7195
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package app.zoftwhere.mutable.scope;
2+
3+
import app.zoftwhere.mutable.MutableValue;
4+
import org.junit.jupiter.api.Test;
5+
6+
class MutableValueScopeTest {
7+
8+
@Test
9+
void testOfNullable() {
10+
MutableValue.<String>ofNullable(null).set("");
11+
}
12+
13+
@Test
14+
void testEmpty() {
15+
MutableValue.<String>empty().set("");
16+
}
17+
}

0 commit comments

Comments
 (0)