Created
February 8, 2015 21:52
-
-
Save kevinpet/0748c12517c2e882fa5e to your computer and use it in GitHub Desktop.
Java default visitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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