Let’s add a simple increment(Integer value) function to the Java 7 Counter Class to prove the pattern:
public void increment(Integer incrementValue) {
if (currentValue != null && endValue != null
&& incrementValue !=null // if-not-equal-null
&& currentValue < (endValue - incrementValue) // Select-some-values
) {
currentValue+=incrementValue; // do-something
}
}
Using an Optional object, you can rewrite the code as follows:
public void increment(Optional incrementValue) {
if (currentValue.isPresent() && endValue.isPresent() && incrementValue.isPresent()
&& currentValue.get() < (endValue.get() - incrementValue.get()))
{
currentValue = Optional.of(currentValue.get() + incrementValue.get());
}
}
By comparing the two codes, we notice that:
- The if-not-null pattern was rewritten with isPresent() and looks cleaner than the Java 7 code
- To use the values wrapped in the Optional object, we need the get() method, compared to the Java 7 code an overhead
- The Java 8 code also need Optional.Of to wrap the new calculated value back to the Optional object
Rewriting the Java 7 code as we did above is not the perfect way to use Optional, that will be the goal of the next post.
For the time being, the two codes should function the same way and to make this sure, let’s do some unit testing:
public class CounterTest {
private CounterJava7 counterJava7;
private CounterJava8 counterJava8;
private Integer target;
private Integer incrementValue;
@Test
public void currentValue_is_one_increment_is_two_test() {
target = (Integer) 3;
incrementValue = 2;
counterJava7 = new CounterJava7(20, 1);
counterJava7.increment(incrementValue);
assertEquals(target, counterJava7.getCurrentValue());
counterJava8 = new CounterJava8(20, 1);
counterJava8.increment(Optional.of(incrementValue));
assertEquals(target, counterJava8.getCurrentValue());
}
@Test
public void currentValue_plus_incrementValue_is_above_endValue_test() {
target = (Integer) 1;
incrementValue = 20;
counterJava7 = new CounterJava7(20, 1);
counterJava7.increment(incrementValue);
assertEquals(target, counterJava7.getCurrentValue());
counterJava8 = new CounterJava8(20, 1);
counterJava8.increment(Optional.of(incrementValue));
assertEquals(target, counterJava8.getCurrentValue());
}
@Test
public void currentValue_plus_incrementValue_is_below_endValue_test() {
target = (Integer) 11;
incrementValue = 10;
counterJava7 = new CounterJava7(20, 1);
counterJava7.increment(incrementValue);
assertEquals(target, counterJava7.getCurrentValue());
counterJava8 = new CounterJava8(20, 1);
counterJava8.increment(Optional.of(incrementValue));
assertEquals(target, counterJava8.getCurrentValue());
}
@Test
public void currentValue_and_endValue_are_default_test() {
target = (Integer) 5;
incrementValue = 5;
counterJava7 = new CounterJava7();
counterJava7.increment(incrementValue);
assertEquals(target, counterJava7.getCurrentValue());
counterJava8 = new CounterJava8();
counterJava8.increment(Optional.of(incrementValue));
assertEquals(target, counterJava8.getCurrentValue());
}
@Test
public void incrementValue_is_Null_test() {
target = (Integer) 15;
incrementValue = null;
counterJava7 = new CounterJava7(10, 15);
counterJava7.increment(incrementValue);
assertEquals(target, counterJava7.getCurrentValue());
counterJava8 = new CounterJava8(10, 15);
counterJava8.increment(Optional.ofNullable(incrementValue));
assertEquals(target, counterJava8.getCurrentValue());
}
@Test
public void currentValue_is_null_test() {
try {
counterJava8 = new CounterJava8(Integer.MIN_VALUE, null);
fail();
} catch (NullPointerException e) {}
try {
counterJava7 = new CounterJava7(Integer.MIN_VALUE, null);
fail();
} catch (IllegalArgumentException e) {}
}
@Test
public void currentValue_and_endValue_are_null_test() {
try {
counterJava8 = new CounterJava8(null, null);
fail();
} catch (NullPointerException e) {}
try {
counterJava7 = new CounterJava7(null, null);
fail();
} catch (IllegalArgumentException e) {}
}
}
and the output: