Skip to content

Instantly share code, notes, and snippets.

@kevinpet
Created February 8, 2015 21:52
Show Gist options
  • Save kevinpet/0748c12517c2e882fa5e to your computer and use it in GitHub Desktop.
Save kevinpet/0748c12517c2e882fa5e to your computer and use it in GitHub Desktop.
Java default visitor
class DefaultFooVisitor<T> implements Foo.Visitor<T> {
T defaultValue;
private DefaultFooVisitor(T defaultValue) {
this.defaultValue = defaultValue;
}
@Override
public T caseBar(Bar b) {
return defaultValue;
}
@Override
public T caseBaz(Baz b) {
return defaultValue;
}
static int countBars(Foo f) {
return f.match(new DefaultFooVisitor<Integer>(0) {
@Override
public Integer caseBar(Bar b) {
return b.bar;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment