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

Petr Lancaric commented on WICKET-5518:
---------------------------------------

There is a discussion on pros and cons of immutable objects, so I considered as 
good practice to use immutable object:
http://programmers.stackexchange.com/questions/151733/if-immutable-objects-are-good-why-do-people-keep-creating-mutable-objects

My initial motivation was that I want to track changes on my value object, 
therefore getter returns copy of list and changes could be detected in setter. 
I am able to achieve this goal using mutable copy as shown bellow.

I understand task is not so easy as we have to find suitable collection type 
and create instance via reflection, which is accepted by setObject call as an 
argument.

My code of vaue object used in property model:

    public List<Integer> getBusinessFieldList() {
        // fails:
        return Collections.unmodifiableList(this.businessFieldList);
       // works: 
       //return Lists.newArrayList(this.businessFieldList);
    }

    public void setBusinessFieldList(List<Integer> value) {
        trackChange("businessField", this.businessFieldList, value);
        this.businessFieldList = value;
    }


> FormComponent.updateCollectionModel  does not handle unmodifiableList
> ---------------------------------------------------------------------
>
>                 Key: WICKET-5518
>                 URL: https://issues.apache.org/jira/browse/WICKET-5518
>             Project: Wicket
>          Issue Type: Bug
>    Affects Versions: 6.12.0
>            Reporter: Petr Lancaric
>            Priority: Minor
>
> FormComponent.updateCollectionModel should handle situation, when getter 
> returns unmodifiable list.
> Proposed solution:
>                       formComponent.modelChanging();
>                       booelan isChanged;
>                       try {
>                               collection.clear();
>                               if (convertedInput != null)
>                               {
>                                       collection.addAll(convertedInput);
>                               }
>                               isChanged = true;
>                       catch (Exception e)
>                       {
>                               // ignore this exception as Unmodifiable list 
> does not allow change                             
>                               logger.info("An error occurred while trying to 
> modify list attached to " + formComponent, e);
>                       }
>                       try
>                       {
>                               if(isChanged)                           
>                                       
> formComponent.getModel().setObject(collection);
>                               else 
>                                       // TODO: create here collection as 
> non-abstract successor of setObject declared argument
>                                       formComponent.getModel().setObject(new 
> ArrayList(convertedInput));
>                               isChanged = true;
>                       }
>                       catch (Exception e)
>                       {
>                               // ignore this exception because it could be 
> that there
>                               // is not setter for this collection.
>                               logger.info("An error occurred while trying to 
> set the new value for the property attached to " + formComponent, e);
>                       }
>                       // at least one update method should pass successfully  
>                 
>                       if(isChanged)
>                               formComponent.modelChanged();
>                       else
>                               throw new RuntimeException("An error occurred 
> while trying to modify value for the property attached to " + formComponent); 
>        



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to