Created
January 7, 2017 04:30
-
-
Save jvz/dd499ccddfb6ffc0336fe0308e32b344 to your computer and use it in GitHub Desktop.
Generic builder DSL
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
public abstract class AbstractAppender extends AbstractFilterable implements Appender { | |
public abstract static class Builder<T extends AbstractAppender, B extends Builder<T, B> & org.apache.logging.log4j.core.util.Builder<T>> | |
extends AbstractFilterable.Builder<T, B> { | |
// ... | |
} | |
} |
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
public abstract class AbstractFilterable extends AbstractLifeCycle implements Filterable { | |
public abstract static class Builder<T extends AbstractFilterable, B extends Builder<T, B> & org.apache.logging.log4j.core.util.Builder<T>> | |
implements org.apache.logging.log4j.core.util.Builder<T> { | |
// ... | |
} | |
} |
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
public class MockAppender extends AbstractAppender { | |
public static class Builder<B extends Builder<B> & org.apache.logging.log4j.core.util.Builder<MockAppender>> | |
extends AbstractAppender.Builder<MockAppender, B> { | |
// ... | |
public MockAppender build() { | |
return new MockAppender(); | |
} | |
} | |
@PluginBuilderFactory | |
public static <B extends Builder<B> & org.apache.logging.log4j.core.util.Builder<MockAppender>> Builder<B> newBuilder() { | |
return new Builder<>(); | |
} | |
public static <B extends Builder<B>> MockAppender createMockAppender(final String name, ...) { | |
return MockAppender.<B>newBuilder() | |
.setFoo(foo) | |
.setBar(bar) | |
// ... | |
// notice that ancestor builders are chained in order of closest parent as the generic type narrows | |
.withName(name) | |
.withIgnoreException(ignoreExceptions) | |
.withLayout(layout) | |
// with the top-most builder last | |
.withFilter(filter) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment