Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
Fixes builder regression: pop() used to be nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrjr committed Aug 13, 2015
1 parent e94f3e0 commit e954c55
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

Version 0.12 *(2015-08-13)*
------
* Fix: History.Builder#pop is nullable again, and adds History.Builder#isEmpty.

Version 0.11 *(2015-08-11)*
------
* Fix: No longer persists an empty list of states if the filter excludes everything.
Expand Down
11 changes: 10 additions & 1 deletion flow/src/main/java/flow/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,19 @@ public Builder addAll(Collection<Object> c) {
}

public Object peek() {
return history.peek().state;
final Entry peek = history.peek();
return peek == null ? null : peek.state;
}

public boolean isEmpty() {
return peek() == null;
}

/** @throws IllegalStateException if empty */
public Object pop() {
if (isEmpty()) {
throw new IllegalStateException("Cannot pop from an empty builder");
}
Entry entry = history.pop();
entryMemory.put(entry.state, entry);
return entry.state;
Expand Down
23 changes: 23 additions & 0 deletions flow/src/test/java/flow/FlowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static flow.Flow.TraversalCallback;
import static java.util.Arrays.asList;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Fail.fail;

public class FlowTest {
static class Uno {
Expand Down Expand Up @@ -287,4 +288,26 @@ public int hashCode() {

assertThat(flow.goBack()).isFalse();
}

@Test public void emptyBuilderPeekIsNullable() {
assertThat(History.emptyBuilder().peek()).isNull();
}

@Test public void emptyBuilderPopThrows() {
try {
History.emptyBuilder().pop();
fail("Should throw");
} catch (IllegalStateException e) {
// pass
}
}

@Test public void isEmpty() {
final History.Builder builder = History.emptyBuilder();
assertThat(builder.isEmpty()).isTrue();
builder.push("foo");
assertThat(builder.isEmpty()).isFalse();
builder.pop();
assertThat(builder.isEmpty()).isTrue();
}
}

0 comments on commit e954c55

Please sign in to comment.