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

Martijn Dashorst commented on WICKET-4972:
------------------------------------------

We use the following class to wrap around StringResourceModel and I think it 
has a much better API:

{code:java}
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;

public class I18N implements Serializable
{
        private static final long serialVersionUID = 1L;

        private String key;

        private Component component;

        private IModel< ? > model;

        private String defaultValue;

        private List<Serializable> parameters = new ArrayList<>();

        private I18N(String key)
        {
                this.key = key;
        }

        public static I18N of(String key)
        {
                return new I18N(key);
        }

        public I18N from(Component component)
        {
                this.component = component;
                return this;
        }

        public I18N withModel(IModel< ? > model)
        {
                this.model = model;
                return this;
        }

        public I18N with(Serializable parameter)
        {
                parameters.add(parameter);
                return this;
        }

        public I18N withDefault(String defaultValue)
        {
                this.defaultValue = defaultValue;
                return this;
        }

        public IModel<String> toStringModel()
        {
                return new StringResourceModel(key, component, model, 
defaultValue, parameters.toArray());
        }

        @Override
        public String toString()
        {
                return toStringModel().getObject();
        }
}
{code}

> Remove (or disarm) varargs contructors of StringResourceModel (more of a 
> pitfall than convenience)
> --------------------------------------------------------------------------------------------------
>
>                 Key: WICKET-4972
>                 URL: https://issues.apache.org/jira/browse/WICKET-4972
>             Project: Wicket
>          Issue Type: Wish
>          Components: wicket
>    Affects Versions: 6.4.0
>            Reporter: Peter Parson
>            Assignee: Martin Grigorov
>             Fix For: 7.0.0-M1
>
>
> The introduction of varargs constructors for StringResourceModel also 
> introduced a pitfall which (IMO) outweighs the convenience of not explicitely 
> having to create an array.
> When using this constructor:
> public StringResourceModel(final String resourceKey, final Component 
> component, final IModel<?> model, final Object... parameters)
> one runs into problems when parameters are of type String (which probably 
> happens more often than not), since the VM acutally ends up calling
> public StringResourceModel(final String resourceKey, final Component 
> component, final IModel<?> model, final String defaultValue, final Object... 
> parameters)
> From my experience, this happens repeatedly to Wicket newbies, but still to 
> "veterans" sometimes.
> Example (does not work as one would expect):
> new StringResourceModel("my.resource.key", this, getModel(), "first param to 
> replace", "second param to replace");
> Forcing to do either
> new StringResourceModel("my.resource.key", this, getModel(), null, "first 
> param to replace", "second param to replace");
> or
> new StringResourceModel("my.resource.key", this, getModel(), 
> newObject[]{"first param to replace", "second param to replace"});
> I think varargs is a convenient feature, but in this special case it 
> introduces confusion, outweighing the convenience by far, thus I suggest to 
> expect explicit array like before.
> Another solution might be to expect a Model<String> as defaultValue or to 
> change the order of constructor arguments.



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

Reply via email to