Skip to content

Commit 8f9d9e0

Browse files
Merge pull request #5 from ZoftWhere/release-2.0.0
Release 2.0.0
2 parents 5207e25 + f76d1e3 commit 8f9d9e0

File tree

10 files changed

+141
-19
lines changed

10 files changed

+141
-19
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
Release Notes v1.2.1
1+
Release Notes v2.0.0
22

3-
- Maven POM updated to module-info.java in source artifact.
4-
- Maven POM updated to fix/remove maven-javadoc-plugin not creating package-list.
5-
- Maven POM updated for known issue with maven-surefire-plugin; exceptions occurring when module-info.java file is in the path and/or reuseForks is false.
6-
- Updated archived artifact entries in production jar file (including fields in manifest file).
7-
- Updated read me with correct Maven execution for creating/installing artifacts to local repository.
3+
- Updated Copyright for 2020.
4+
- Updated JavaDoc for MutableValue class.
5+
- Updated Receiver class with return type for fluent API designs.
6+
- Added static method MutableValue.of(value) for easy creation.
7+
- Added static method MutableValue.empty() for easy creation.
8+
- Added method MutableValue.getValue() for exception-free retrieval of empty values.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Optional;
44
import java.util.function.Supplier;
55

6-
public interface PlaceHolder<T> extends Receiver<T>, Supplier<T> {
6+
public interface PlaceHolder<T> extends Receiver<T, Void>, Supplier<T> {
77

88
Optional<T> optional();
99

@@ -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
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package app.zoftwhere.function;
22

33
@SuppressWarnings("unused")
4-
public interface Receiver<T> {
4+
public interface Receiver<T, R> {
55

6-
void set(T t);
6+
R set(T t);
77
}

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

Lines changed: 78 additions & 3 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
21-
public void set(T value) {
58+
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
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>app.zoftwhere.mutable</groupId>
77
<artifactId>mutable-library</artifactId>
8-
<version>1.2.1</version>
8+
<version>2.0.0</version>
99

1010
<properties>
1111
<!-- Settings: maven-resource-plugin -->

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,6 @@ public class TransformerExample2 {
191191

192192
## License
193193

194-
Copyright (C) 2019 ZoftWhere
194+
Copyright (C) 2020 ZoftWhere
195195

196196
Licensed under the MIT License

test-java/app/zoftwhere/function/PlaceHolderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ public boolean isPresent() {
2727
}
2828

2929
@Override
30-
public void set(String value) {
30+
public Void set(String value) {
3131
internal = value;
32+
return null;
3233
}
3334

3435
@Override

test-java/app/zoftwhere/function/ReceiverTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class ReceiverTest {
1010
@Test
1111
void simpleTest() {
1212
final var mutable = new MutableValue<>("unset");
13-
final var receiver = new Receiver<String>() {
13+
final var receiver = new Receiver<String, Void>() {
1414
@Override
15-
public void set(String value) {
16-
mutable.set(value);
15+
public Void set(String value) {
16+
return mutable.set(value);
1717
}
1818
};
1919
assertEquals("unset", mutable.get());

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)