You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Juan G. Arias" <ju...@gmail.com> on 2009/04/08 23:36:42 UTC

Default implementation of IChainingModel

Hi all,First of all, I'm using wicket 1.3.5

I'm writing a model and ai need it to be "chaineable". I mean, I need this
model to contain another model, so my model can obtain the data, for
example, from a property model.
Ok, I've been reading and this is solved by the IChainingModel.

But I couldn't find any default implementation of this interface.
There are two classes currently implementing this interface,
AbstractPropertyModel and CompoundPropertyModel.
Both classes has some code duplicated, specifically:
- void detach()
- IModel getChainedModel()
- void setChainedModel(IModel model)
- some lines of void setObject(Object object)
- the code in CompuntPropertyModel#getObject() and
AbstarctPropertyModel#getTarget() is different, but the logic is the same.

And I'm afraid my code will be the same as those classes.

So, finally, my point.
Is there any default implementation of this behavior? Is there a chance to
add a super-class with this code?

Thanks!
Juan Arias

Re: Default implementation of IChainingModel

Posted by Daniel Stoch <da...@gmail.com>.
Hi,

Jeremy Thomerson wrote:
> I don't think one was ever created and it fell off my radar.  If you create
> one, can you post yours and post a link to it back on this thread?

Here is my implementation for Wicket 1.4 (with generics). It is a
little bit different than Scott's one.


import org.apache.wicket.model.IChainingModel;
import org.apache.wicket.model.IDetachable;
import org.apache.wicket.model.IModel;

/**
 * Basic implementation of {@link IChainingModel} interface.
 *
 * @author Daniel Stoch
 *
 */
public class ChainingModel<T> implements IChainingModel<T> {

  /** Any model object (which may or may not implement IModel) */
  private Object target;

  public ChainingModel(final Object modelObject) {
    target = modelObject;
  }

  @Override
  public IModel<?> getChainedModel() {
    if (target instanceof IModel<?>) {
      return (IModel<?>)target;
    }
    return null;
  }

  @Override
  public void setChainedModel(IModel<?> model) {
    target = model;
  }

  @SuppressWarnings("unchecked")
  public T getObject() {
    if (target instanceof IModel) {
      return ((IModel<T>)target).getObject();
    }
    return (T)target;
  }

  @SuppressWarnings("unchecked")
  public void setObject(T object) {
    if (target instanceof IModel) {
      ((IModel<T>)target).setObject(object);
    } else {
      target = object;
    }
  }

  public void detach() {
    // Detach nested object if it's a detachable
    if (target instanceof IDetachable) {
      ((IDetachable)target).detach();
    }
  }

  @Override
  public String toString() {
    StringBuffer sb = new StringBuffer("Model:classname=[");
    sb.append(getClass().getName()).append("]");
    sb.append(":nestedModel=[").append(target).append("]");
    return sb.toString();
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((target == null) ? 0 : target.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    ChainingModel<?> other = (ChainingModel<?>)obj;
    if (target == null) {
      if (other.target != null) return false;
    } else if (!target.equals(other.target)) return false;
    return true;
  }

}


--
Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Default implementation of IChainingModel

Posted by Scott Swank <sc...@gmail.com>.
https://issues.apache.org/jira/browse/WICKET-2498

We're still on Wicket 1.3.5, so I put in a 1 hour estimate for someone
to add generic typing.

Scott


On Wed, Sep 30, 2009 at 3:58 PM, Jeremy Thomerson
<je...@wickettraining.com> wrote:
> I don't think one was ever created and it fell off my radar.  If you create
> one, can you post yours and post a link to it back on this thread?
>
> Thanks!
>
> --
> Jeremy Thomerson
> http://www.wickettraining.com
>
>
>
> On Wed, Sep 30, 2009 at 4:45 PM, Scott Swank <sc...@gmail.com> wrote:
>
>> I searched the JIRA for IChainingModel and didn't get any hits.  Did
>> anyone create a JIRA issue?  Here's an implementation of mine.
>>
>> public class BaseChainingModel implements IChainingModel {
>>        private static final long serialVersionUID = 1L;
>>        private Object target;
>>
>>        public BaseChainingModel(Object modelObject) {
>>                if (modelObject == null) {
>>                        throw new IllegalArgumentException("Parameter
>> modelObject cannot be null");
>>                }
>>                else {
>>                        target = modelObject;
>>                }
>>        }
>>
>>        @Override
>>        public void detach() {
>>                if (target instanceof IDetachable)
>>                        ((IDetachable) target).detach();
>>        }
>>
>>        @Override
>>        public IModel getChainedModel() {
>>                if (target instanceof IModel)
>>                        return (IModel) target;
>>                else
>>                        return null;
>>        }
>>
>>        @Override
>>        public void setChainedModel(IModel model) {
>>                target = model;
>>        }
>>
>>        @Override
>>        public Object getObject() {
>>                Object object = target;
>>                while (object instanceof IModel) {
>>                        Object tmp = ((IModel) object).getObject();
>>                        if (tmp == object) {
>>                                break; // pathological
>>                        }
>>                        object = tmp;
>>                }
>>                return object;
>>        }
>>
>>        @Override
>>        public void setObject(Object obj)       {
>>                if (target instanceof IModel)
>>                        ((IModel) target).setObject(obj);
>>                else if (obj == null || obj instanceof Serializable)
>>                        target = obj;
>>                else
>>                        throw new WicketRuntimeException("Model object must
>> be Serializable");
>>        }
>>
>> }
>>
>> Scott
>>
>>
>> On Wed, Apr 8, 2009 at 2:53 PM, Jeremy Thomerson
>> <je...@wickettraining.com> wrote:
>> > I think that's a good idea - I have done a similar thing in my own
>> > projects.  Please open a JIRA so this idea doesn't get lost, but this is
>> one
>> > I may try to do soon.
>> >
>> > --
>> > Jeremy Thomerson
>> > http://www.wickettraining.com
>> >
>> >
>> >
>> > On Wed, Apr 8, 2009 at 4:36 PM, Juan G. Arias <ju...@gmail.com>
>> wrote:
>> >
>> >> Hi all,First of all, I'm using wicket 1.3.5
>> >>
>> >> I'm writing a model and ai need it to be "chaineable". I mean, I need
>> this
>> >> model to contain another model, so my model can obtain the data, for
>> >> example, from a property model.
>> >> Ok, I've been reading and this is solved by the IChainingModel.
>> >>
>> >> But I couldn't find any default implementation of this interface.
>> >> There are two classes currently implementing this interface,
>> >> AbstractPropertyModel and CompoundPropertyModel.
>> >> Both classes has some code duplicated, specifically:
>> >> - void detach()
>> >> - IModel getChainedModel()
>> >> - void setChainedModel(IModel model)
>> >> - some lines of void setObject(Object object)
>> >> - the code in CompuntPropertyModel#getObject() and
>> >> AbstarctPropertyModel#getTarget() is different, but the logic is the
>> same.
>> >>
>> >> And I'm afraid my code will be the same as those classes.
>> >>
>> >> So, finally, my point.
>> >> Is there any default implementation of this behavior? Is there a chance
>> to
>> >> add a super-class with this code?
>> >>
>> >> Thanks!
>> >> Juan Arias
>> >>
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Default implementation of IChainingModel

Posted by Jeremy Thomerson <je...@wickettraining.com>.
I don't think one was ever created and it fell off my radar.  If you create
one, can you post yours and post a link to it back on this thread?

Thanks!

--
Jeremy Thomerson
http://www.wickettraining.com



On Wed, Sep 30, 2009 at 4:45 PM, Scott Swank <sc...@gmail.com> wrote:

> I searched the JIRA for IChainingModel and didn't get any hits.  Did
> anyone create a JIRA issue?  Here's an implementation of mine.
>
> public class BaseChainingModel implements IChainingModel {
>        private static final long serialVersionUID = 1L;
>        private Object target;
>
>        public BaseChainingModel(Object modelObject) {
>                if (modelObject == null) {
>                        throw new IllegalArgumentException("Parameter
> modelObject cannot be null");
>                }
>                else {
>                        target = modelObject;
>                }
>        }
>
>        @Override
>        public void detach() {
>                if (target instanceof IDetachable)
>                        ((IDetachable) target).detach();
>        }
>
>        @Override
>        public IModel getChainedModel() {
>                if (target instanceof IModel)
>                        return (IModel) target;
>                else
>                        return null;
>        }
>
>        @Override
>        public void setChainedModel(IModel model) {
>                target = model;
>        }
>
>        @Override
>        public Object getObject() {
>                Object object = target;
>                while (object instanceof IModel) {
>                        Object tmp = ((IModel) object).getObject();
>                        if (tmp == object) {
>                                break; // pathological
>                        }
>                        object = tmp;
>                }
>                return object;
>        }
>
>        @Override
>        public void setObject(Object obj)       {
>                if (target instanceof IModel)
>                        ((IModel) target).setObject(obj);
>                else if (obj == null || obj instanceof Serializable)
>                        target = obj;
>                else
>                        throw new WicketRuntimeException("Model object must
> be Serializable");
>        }
>
> }
>
> Scott
>
>
> On Wed, Apr 8, 2009 at 2:53 PM, Jeremy Thomerson
> <je...@wickettraining.com> wrote:
> > I think that's a good idea - I have done a similar thing in my own
> > projects.  Please open a JIRA so this idea doesn't get lost, but this is
> one
> > I may try to do soon.
> >
> > --
> > Jeremy Thomerson
> > http://www.wickettraining.com
> >
> >
> >
> > On Wed, Apr 8, 2009 at 4:36 PM, Juan G. Arias <ju...@gmail.com>
> wrote:
> >
> >> Hi all,First of all, I'm using wicket 1.3.5
> >>
> >> I'm writing a model and ai need it to be "chaineable". I mean, I need
> this
> >> model to contain another model, so my model can obtain the data, for
> >> example, from a property model.
> >> Ok, I've been reading and this is solved by the IChainingModel.
> >>
> >> But I couldn't find any default implementation of this interface.
> >> There are two classes currently implementing this interface,
> >> AbstractPropertyModel and CompoundPropertyModel.
> >> Both classes has some code duplicated, specifically:
> >> - void detach()
> >> - IModel getChainedModel()
> >> - void setChainedModel(IModel model)
> >> - some lines of void setObject(Object object)
> >> - the code in CompuntPropertyModel#getObject() and
> >> AbstarctPropertyModel#getTarget() is different, but the logic is the
> same.
> >>
> >> And I'm afraid my code will be the same as those classes.
> >>
> >> So, finally, my point.
> >> Is there any default implementation of this behavior? Is there a chance
> to
> >> add a super-class with this code?
> >>
> >> Thanks!
> >> Juan Arias
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Default implementation of IChainingModel

Posted by Scott Swank <sc...@gmail.com>.
I searched the JIRA for IChainingModel and didn't get any hits.  Did
anyone create a JIRA issue?  Here's an implementation of mine.

public class BaseChainingModel implements IChainingModel {
	private static final long serialVersionUID = 1L;
	private Object target;

	public BaseChainingModel(Object modelObject) {
		if (modelObject == null) {
			throw new IllegalArgumentException("Parameter modelObject cannot be null");
		}
		else {
			target = modelObject;
		}
	}

	@Override
	public void detach() {
		if (target instanceof IDetachable)
			((IDetachable) target).detach();
	}

	@Override
	public IModel getChainedModel() {
		if (target instanceof IModel)
			return (IModel) target;
		else
			return null;
	}

	@Override
	public void setChainedModel(IModel model) {
		target = model;
	}

	@Override
	public Object getObject() {
		Object object = target;
		while (object instanceof IModel) {
			Object tmp = ((IModel) object).getObject();
			if (tmp == object) {
				break; // pathological
			}
			object = tmp;
		}
		return object;
	}

	@Override
	public void setObject(Object obj) 	{
		if (target instanceof IModel)
			((IModel) target).setObject(obj);
		else if (obj == null || obj instanceof Serializable)
			target = obj;
		else
			throw new WicketRuntimeException("Model object must be Serializable");
	}

}

Scott


On Wed, Apr 8, 2009 at 2:53 PM, Jeremy Thomerson
<je...@wickettraining.com> wrote:
> I think that's a good idea - I have done a similar thing in my own
> projects.  Please open a JIRA so this idea doesn't get lost, but this is one
> I may try to do soon.
>
> --
> Jeremy Thomerson
> http://www.wickettraining.com
>
>
>
> On Wed, Apr 8, 2009 at 4:36 PM, Juan G. Arias <ju...@gmail.com> wrote:
>
>> Hi all,First of all, I'm using wicket 1.3.5
>>
>> I'm writing a model and ai need it to be "chaineable". I mean, I need this
>> model to contain another model, so my model can obtain the data, for
>> example, from a property model.
>> Ok, I've been reading and this is solved by the IChainingModel.
>>
>> But I couldn't find any default implementation of this interface.
>> There are two classes currently implementing this interface,
>> AbstractPropertyModel and CompoundPropertyModel.
>> Both classes has some code duplicated, specifically:
>> - void detach()
>> - IModel getChainedModel()
>> - void setChainedModel(IModel model)
>> - some lines of void setObject(Object object)
>> - the code in CompuntPropertyModel#getObject() and
>> AbstarctPropertyModel#getTarget() is different, but the logic is the same.
>>
>> And I'm afraid my code will be the same as those classes.
>>
>> So, finally, my point.
>> Is there any default implementation of this behavior? Is there a chance to
>> add a super-class with this code?
>>
>> Thanks!
>> Juan Arias
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Default implementation of IChainingModel

Posted by Jeremy Thomerson <je...@wickettraining.com>.
I think that's a good idea - I have done a similar thing in my own
projects.  Please open a JIRA so this idea doesn't get lost, but this is one
I may try to do soon.

--
Jeremy Thomerson
http://www.wickettraining.com



On Wed, Apr 8, 2009 at 4:36 PM, Juan G. Arias <ju...@gmail.com> wrote:

> Hi all,First of all, I'm using wicket 1.3.5
>
> I'm writing a model and ai need it to be "chaineable". I mean, I need this
> model to contain another model, so my model can obtain the data, for
> example, from a property model.
> Ok, I've been reading and this is solved by the IChainingModel.
>
> But I couldn't find any default implementation of this interface.
> There are two classes currently implementing this interface,
> AbstractPropertyModel and CompoundPropertyModel.
> Both classes has some code duplicated, specifically:
> - void detach()
> - IModel getChainedModel()
> - void setChainedModel(IModel model)
> - some lines of void setObject(Object object)
> - the code in CompuntPropertyModel#getObject() and
> AbstarctPropertyModel#getTarget() is different, but the logic is the same.
>
> And I'm afraid my code will be the same as those classes.
>
> So, finally, my point.
> Is there any default implementation of this behavior? Is there a chance to
> add a super-class with this code?
>
> Thanks!
> Juan Arias
>