You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Greg Thomas (JIRA)" <ji...@apache.org> on 2016/08/10 10:59:20 UTC

[jira] [Commented] (LOG4J2-1507) Allow Builders to be completely generic

    [ https://issues.apache.org/jira/browse/LOG4J2-1507?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15415114#comment-15415114 ] 

Greg Thomas commented on LOG4J2-1507:
-------------------------------------

A style I've often used is to supply two Generic types to an AbstractBuilder so you can strongly type the return value to {{build()}}, too - i.e. something like ...

{code}
protected abstract static class AbstractBuilder<B extends AbstractBuilder, T> {
        public B withName(final String name) {
            this.name = name;
            return asBuilder();
        }

        private B asBuilder() {
            return (B) this;
        }

        public abstract T build();
}
{code}

> Allow Builders to be completely generic
> ---------------------------------------
>
>                 Key: LOG4J2-1507
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1507
>             Project: Log4j 2
>          Issue Type: New Feature
>          Components: Configurators
>            Reporter: Gary Gregory
>            Assignee: Gary Gregory
>             Fix For: 2.7
>
>
> Allow Builders to be completely generic. This is not just supporting {{Builder<T>}}, which works in 2.6.2, but to allow declarations like {{Builder<B extends Builder<B>>}} combined with setters that return {{B}}.
> {code:java}
>     public static class Builder<B extends Builder<B>> implements org.apache.logging.log4j.core.util.Builder<ValidatingPluginWithGenericBuilder> {
>         @PluginBuilderAttribute
>         @Required(message = "The name given by the builder is null")
>         private String name;
>         public B withName(final String name) {
>             this.name = name;
>             return asBuilder();
>         }
>         @SuppressWarnings("unchecked")
>         private B asBuilder() {
>             return (B) this;
>         }
>         @Override
>         public ValidatingPluginWithGenericBuilder build() {
>             return new ValidatingPluginWithGenericBuilder(name);
>         }
>     }
> {code}
> (I have a patch for this)
> (The next step (and ticket) will be to allow to use a Builder that extends another Builder.)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org