java.util.Optional example

Setup

class PullRequest {
    
    String person;
    String summary;
    String comments;
    
    public PullRequest(String person, String summary, String comments) {
        super();
        this.person = person;
        this.summary = summary;
        this.comments = comments;
    }

    public String getPerson() {
        return person;
    }

    public String getSummary() {
        return summary;
    }

    public String getComments() {
        return comments;
    }
}

Google Guava

/**
 * Added suppression purely for demonstration purposes as the optional.of will never get called.
 * @return
 */
@SuppressWarnings("unused")
private Optional<PullRequest> getPullRequestUsingGuavaOptional () {

    PullRequest pr = null; // make some data access call, if it isn't there return null

    if (pr == null) {
        return Optional.absent();
    } else {
        return Optional.of(pr);   
    }
}


@Test
public void avoid_checking_for_null_with_guava_optional () {

    Optional<PullRequest> pr = getPullRequestUsingGuavaOptional ();
    
    if (pr.isPresent()) {

        PullRequest pullRequest = pr.get();
        
        // proceed with further processing
        logger.info(pullRequest);
        
    } else {
        // do nothing
        logger.info("object was null"); 
    }
    
    assertFalse(pr.isPresent());
}

Output

object was null

Get non null object

@Test
public void get_first_non_null_object () {
    
    Optional<String> first = Optional.absent();
    Optional<String> second = Optional.of("Game Day!");
    
    assertEquals(second.get(), first.or(second).get());
}

From nullable

@Test
public void from_nullable() {

    Optional<PullRequest> pullRequest = Optional
            .fromNullable(new PullRequest("Jack", "summary", "comments"));
    
    assertTrue(pullRequest.isPresent());
}

Present instances

@Test
public void present_instances () {
    
    List<Optional<PullRequest>> pullRequests = Lists.newArrayList();
    pullRequests.add(getPullRequestUsingGuavaOptional());
    pullRequests.add(Optional.of(new PullRequest("Graham", "a->b summary",  "please merge")));
    pullRequests.add(getPullRequestUsingGuavaOptional());
    pullRequests.add(Optional.of(new PullRequest("Jesse", "c->d summary",  "check code")));
    
    Iterable<PullRequest> presentInstances = Optional.presentInstances(pullRequests);
    
    assertThat(presentInstances, IsIterableWithSize.<PullRequest>iterableWithSize(2));
}

Straight up Java

private PullRequest getPullRequestReturningNull () {
    PullRequest pr = null; // make some data access call, if it isn't there return null
    return pr;
}

@Test
public void returning_null_in_java () {
    
    PullRequest pr = getPullRequestReturningNull();
    if (pr != null) {
        pr.getComments();
    }
}