Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Validating additions to a list
Validating additions to a list [message #414106] Thu, 25 October 2007 03:59 Go to next message
Eclipse UserFriend
Originally posted by: andrew_steady.hotmail.com

Hi,

As there is no specific setter for adding a single value to a list, (we do
getX().add(y)) how can we prevent certain additions to the list - for
example we wish to prevent a relationship being added if it would result
in a cyclic dependancy.

We could do this with a post validation, or by restricting the options in
the first place - but I in this case we need to prevent on set.

What's the usual solution to this?

Andy
Re: Validating additions to a list [message #414108 is a reply to message #414106] Thu, 25 October 2007 05:01 Go to previous messageGo to next message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1341
Registered: July 2009
Senior Member
On 2007-10-24 20:59:57 -0700, andrew_steady@hotmail.com (Andy Steady) said:

> Hi,
>
> As there is no specific setter for adding a single value to a list, (we
> do getX().add(y)) how can we prevent certain additions to the list -
> for example we wish to prevent a relationship being added if it would
> result in a cyclic dependancy.
>
> We could do this with a post validation, or by restricting the options
> in the first place - but I in this case we need to prevent on set.
> What's the usual solution to this?
>
> Andy

Andy, this is really a job for EMF.edit I think, e.g. it is not a
validation issue per se but a model editing issue. In addition to
post-validation you can override the Objects item provider
createAddCommand to return UnexecutableCommand in the case where the
added command item would involve a cyclic dependency. hth..
Re: Validating additions to a list [message #414113 is a reply to message #414108] Thu, 25 October 2007 10:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------070103010003000902060305
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Miles,

Certainly if the things are being done by commands, that's a good
approach, but sometimes folks need to protect the model APIs themselves
from client programmers. For this type of scenario, one can specialize
the list implementation itself, i.e., specialize this method from the
base implementation which will handle set, add, and addAll:

/**
* Validates a new content object and returns the validated object.
* This implementation checks for null, if {@link #canContainNull
necessary} and returns the argument object.
* Clients may throw additional types of runtime exceptions
* in order to handle constraint violations.
* @param index the position of the new content.
* @param object the new content.
* @return the validated content.
* @exception IllegalArgumentException if a constraint prevents
the object from being added.
*/
protected E validate(int index, E object)
{
if (!canContainNull() && object == null)
{
throw new IllegalArgumentException("The 'no null' constraint
is violated");
}

return object;
}


Miles Parker wrote:
> On 2007-10-24 20:59:57 -0700, andrew_steady@hotmail.com (Andy Steady)
> said:
>
>> Hi,
>>
>> As there is no specific setter for adding a single value to a list,
>> (we do getX().add(y)) how can we prevent certain additions to the
>> list - for example we wish to prevent a relationship being added if
>> it would result in a cyclic dependancy.
>>
>> We could do this with a post validation, or by restricting the
>> options in the first place - but I in this case we need to prevent on
>> set.
>> What's the usual solution to this?
>>
>> Andy
>
> Andy, this is really a job for EMF.edit I think, e.g. it is not a
> validation issue per se but a model editing issue. In addition to
> post-validation you can override the Objects item provider
> createAddCommand to return UnexecutableCommand in the case where the
> added command item would involve a cyclic dependency. hth..
>


--------------070103010003000902060305
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Miles,<br>
<br>
Certainly if the things are being done by commands, that's a good
approach, but sometimes folks need to protect the model APIs themselves
from client programmers.&nbsp; For this type of scenario, one can specialize
the list implementation itself, i.e., specialize this method from the
base implementation which will handle set, add, and addAll:<small><br>
</small>
<blockquote><small>&nbsp; /**</small><br>
<small>&nbsp;&nbsp; * Validates a new content object and returns the validated
object.</small><br>
<small>&nbsp;&nbsp; * This implementation checks for null, if {@link
#canContainNull necessary} and returns the argument object.</small><br>
<small>&nbsp;&nbsp; * Clients may throw additional types of runtime exceptions </small><br>
<small>&nbsp;&nbsp; * in order to handle constraint violations.</small><br>
<small>&nbsp;&nbsp; * @param index the position of the new content.</small><br>
<small>&nbsp;&nbsp; * @param object the new content.</small><br>
<small>&nbsp;&nbsp; * @return the validated content.</small><br>
<small>&nbsp;&nbsp; * @exception IllegalArgumentException if a constraint
prevents the object from being added.</small><br>
<small>&nbsp;&nbsp; */</small><br>
<small>&nbsp; protected E validate(int index, E object)</small><br>
<small>&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp; if (!canContainNull() &amp;&amp; object == null)</small><br>
<small>&nbsp;&nbsp;&nbsp; {</small><br>
<small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new IllegalArgumentException("The 'no null'
constraint is violated");</small><br>
<small>&nbsp;&nbsp;&nbsp; }</small><br>
<br>
<small>&nbsp;&nbsp;&nbsp; return object;</small><br>
<small>&nbsp; }</small><br>
</blockquote>
&nbsp;<br>
Miles Parker wrote:
<blockquote cite="mid:ffp7tp$k0g$1@build.eclipse.org" type="cite">On
2007-10-24 20:59:57 -0700, <a class="moz-txt-link-abbreviated" href="mailto:andrew_steady@hotmail.com">andrew_steady@hotmail.com</a> (Andy Steady)
said:
<br>
<br>
<blockquote type="cite">Hi,
<br>
<br>
As there is no specific setter for adding a single value to a list, (we
do getX().add(y)) how can we prevent certain additions to the list -
for example we wish to prevent a relationship being added if it would
result in a cyclic dependancy.
<br>
<br>
We could do this with a post validation, or by restricting the options
in the first place - but I in this case we need to prevent on set.
<br>
What's the usual solution to this?
<br>
<br>
Andy
<br>
</blockquote>
<br>
Andy, this is really a job for EMF.edit I think, e.g. it is not a
validation issue per se but a model editing issue. In addition to
post-validation you can override the Objects item provider
createAddCommand to return UnexecutableCommand in the case where the
added command item would involve a cyclic dependency. hth..
<br>
<br>
</blockquote>
<br>
</body>
</html>

--------------070103010003000902060305--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Validating additions to a list [message #414135 is a reply to message #414113] Thu, 25 October 2007 17:22 Go to previous messageGo to next message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1341
Registered: July 2009
Senior Member
Right, but wouldn't Andrew's case need both in order to prevent the add
(or accomplish some alternative) rather then simply reporting it?

On 2007-10-25 03:18:29 -0700, Ed Merks <merks@ca.ibm.com> said:

>
> Miles,
>
> Certainly if the things are being done by commands, that's a good
> approach, but sometimes folks need to protect the model APIs themselves
> from client programmers. For this type of scenario, one can specialize
> the list implementation itself, i.e., specialize this method from the
> base implementation which will handle set, add, and addAll:
>
> /**
> * Validates a new content object and returns the validated object.
> * This implementation checks for null, if {@link #canContainNull
> necessary} and returns the argument object.
> * Clients may throw additional types of runtime exceptions
> * in order to handle constraint violations.
> * @param index the position of the new content.
> * @param object the new content.
> * @return the validated content.
> * @exception IllegalArgumentException if a constraint prevents
> the object from being added.
> */
> protected E validate(int index, E object)
> {
> if (!canContainNull() && object == null)
> {
> throw new IllegalArgumentException("The 'no null' constraint
> is violated");
> }
>
> return object;
> }
>
> Miles Parker wrote:
>> On 2007-10-24 20:59:57 -0700, andrew_steady@hotmail.com (Andy Steady) said:
>>
>>> Hi,
>>>
>>> As there is no specific setter for adding a single value to a list, (we
>>> do getX().add(y)) how can we prevent certain additions to the list -
>>> for example we wish to prevent a relationship being added if it would
>>> result in a cyclic dependancy.
>>>
>>> We could do this with a post validation, or by restricting the options
>>> in the first place - but I in this case we need to prevent on set.
>>> What's the usual solution to this?
>>>
>>> Andy
>>
>> Andy, this is really a job for EMF.edit I think, e.g. it is not a
>> validation issue per se but a model editing issue. In addition to
>> post-validation you can override the Objects item provider
>> createAddCommand to return UnexecutableCommand in the case where the
>> added command item would involve a cyclic dependency. hth..
>>
>
>
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
> </head>
> <body bgcolor="#ffffff" text="#000000">
> Miles,<br>
> <br>
> Certainly if the things are being done by commands, that's a good
> approach, but sometimes folks need to protect the model APIs themselves
> from client programmers.&nbsp; For this type of scenario, one can specialize
> the list implementation itself, i.e., specialize this method from the
> base implementation which will handle set, add, and addAll:<small><br>
> </small>
> <blockquote><small>&nbsp; /**</small><br>
> <small>&nbsp;&nbsp; * Validates a new content object and returns the
> validated
> object.</small><br>
> <small>&nbsp;&nbsp; * This implementation checks for null, if {@link
> #canContainNull necessary} and returns the argument object.</small><br>
> <small>&nbsp;&nbsp; * Clients may throw additional types of runtime
> exceptions </small><br>
> <small>&nbsp;&nbsp; * in order to handle constraint violations.</small><br>
> <small>&nbsp;&nbsp; * @param index the position of the new
> content.</small><br>
> <small>&nbsp;&nbsp; * @param object the new content.</small><br>
> <small>&nbsp;&nbsp; * @return the validated content.</small><br>
> <small>&nbsp;&nbsp; * @exception IllegalArgumentException if a constraint
> prevents the object from being added.</small><br>
> <small>&nbsp;&nbsp; */</small><br>
> <small>&nbsp; protected E validate(int index, E object)</small><br>
> <small>&nbsp; {</small><br>
> <small>&nbsp;&nbsp;&nbsp; if (!canContainNull() &amp;&amp; object ==
> null)</small><br>
> <small>&nbsp;&nbsp;&nbsp; {</small><br>
> <small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new
> IllegalArgumentException("The 'no null'
> constraint is violated");</small><br>
> <small>&nbsp;&nbsp;&nbsp; }</small><br>
> <br>
> <small>&nbsp;&nbsp;&nbsp; return object;</small><br>
> <small>&nbsp; }</small><br>
> </blockquote>
> &nbsp;<br>
> Miles Parker wrote:
> <blockquote cite="mid:ffp7tp$k0g$1@build.eclipse.org" type="cite">On
> 2007-10-24 20:59:57 -0700, <a class="moz-txt-link-abbreviated"
> href="mailto:andrew_steady@hotmail.com">andrew_steady@hotmail.com</a>
> (Andy Steady)
> said:
> <br>
> <br>
> <blockquote type="cite">Hi,
> <br>
> <br>
> As there is no specific setter for adding a single value to a list, (we
> do getX().add(y)) how can we prevent certain additions to the list -
> for example we wish to prevent a relationship being added if it would
> result in a cyclic dependancy.
> <br>
> <br>
> We could do this with a post validation, or by restricting the options
> in the first place - but I in this case we need to prevent on set.
> <br>
> What's the usual solution to this?
> <br>
> <br>
> Andy
> <br>
> </blockquote>
> <br>
> Andy, this is really a job for EMF.edit I think, e.g. it is not a
> validation issue per se but a model editing issue. In addition to
> post-validation you can override the Objects item provider
> createAddCommand to return UnexecutableCommand in the case where the
> added command item would involve a cyclic dependency. hth..
> <br>
> <br>
> </blockquote>
> <br>
> </body>
> </html>
Re: Validating additions to a list [message #414137 is a reply to message #414113] Thu, 25 October 2007 17:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: andrew_steady.hotmail.com

Hi,

Thanks guys - some interesting options.

We don't need to support programmatic client access to the model so
probably a specialized command and validation in the model will suffice.
Also nice to know how, if its required, we can protect the model.

Cheers,

Andy
Re: Validating additions to a list [message #414139 is a reply to message #414135] Thu, 25 October 2007 18:11 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Miles,

Yes, that's an excellent point. ;-)


Miles Parker wrote:
>
> Right, but wouldn't Andrew's case need both in order to prevent the
> add (or accomplish some alternative) rather then simply reporting it?
>
> On 2007-10-25 03:18:29 -0700, Ed Merks <merks@ca.ibm.com> said:
>
>>
>> Miles,
>>
>> Certainly if the things are being done by commands, that's a good
>> approach, but sometimes folks need to protect the model APIs
>> themselves from client programmers. For this type of scenario, one
>> can specialize the list implementation itself, i.e., specialize this
>> method from the base implementation which will handle set, add, and
>> addAll:
>>
>> /**
>> * Validates a new content object and returns the validated
>> object.
>> * This implementation checks for null, if {@link #canContainNull
>> necessary} and returns the argument object.
>> * Clients may throw additional types of runtime exceptions
>> * in order to handle constraint violations.
>> * @param index the position of the new content.
>> * @param object the new content.
>> * @return the validated content.
>> * @exception IllegalArgumentException if a constraint prevents
>> the object from being added.
>> */
>> protected E validate(int index, E object)
>> {
>> if (!canContainNull() && object == null)
>> {
>> throw new IllegalArgumentException("The 'no null' constraint
>> is violated");
>> }
>>
>> return object;
>> }
>>
>> Miles Parker wrote:
>>> On 2007-10-24 20:59:57 -0700, andrew_steady@hotmail.com (Andy
>>> Steady) said:
>>>
>>>> Hi,
>>>>
>>>> As there is no specific setter for adding a single value to a list,
>>>> (we do getX().add(y)) how can we prevent certain additions to the
>>>> list - for example we wish to prevent a relationship being added if
>>>> it would result in a cyclic dependancy.
>>>>
>>>> We could do this with a post validation, or by restricting the
>>>> options in the first place - but I in this case we need to prevent
>>>> on set.
>>>> What's the usual solution to this?
>>>>
>>>> Andy
>>>
>>> Andy, this is really a job for EMF.edit I think, e.g. it is not a
>>> validation issue per se but a model editing issue. In addition to
>>> post-validation you can override the Objects item provider
>>> createAddCommand to return UnexecutableCommand in the case where the
>>> added command item would involve a cyclic dependency. hth..
>>>
>>
>>
>>
>> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>> <html>
>> <head>
>> <meta content="text/html;charset=ISO-8859-1"
>> http-equiv="Content-Type">
>> </head>
>> <body bgcolor="#ffffff" text="#000000">
>> Miles,<br>
>> <br>
>> Certainly if the things are being done by commands, that's a good
>> approach, but sometimes folks need to protect the model APIs themselves
>> from client programmers.&nbsp; For this type of scenario, one can
>> specialize
>> the list implementation itself, i.e., specialize this method from the
>> base implementation which will handle set, add, and addAll:<small><br>
>> </small>
>> <blockquote><small>&nbsp; /**</small><br>
>> <small>&nbsp;&nbsp; * Validates a new content object and returns
>> the validated
>> object.</small><br>
>> <small>&nbsp;&nbsp; * This implementation checks for null, if {@link
>> #canContainNull necessary} and returns the argument object.</small><br>
>> <small>&nbsp;&nbsp; * Clients may throw additional types of runtime
>> exceptions </small><br>
>> <small>&nbsp;&nbsp; * in order to handle constraint
>> violations.</small><br>
>> <small>&nbsp;&nbsp; * @param index the position of the new
>> content.</small><br>
>> <small>&nbsp;&nbsp; * @param object the new content.</small><br>
>> <small>&nbsp;&nbsp; * @return the validated content.</small><br>
>> <small>&nbsp;&nbsp; * @exception IllegalArgumentException if a
>> constraint
>> prevents the object from being added.</small><br>
>> <small>&nbsp;&nbsp; */</small><br>
>> <small>&nbsp; protected E validate(int index, E object)</small><br>
>> <small>&nbsp; {</small><br>
>> <small>&nbsp;&nbsp;&nbsp; if (!canContainNull() &amp;&amp; object
>> == null)</small><br>
>> <small>&nbsp;&nbsp;&nbsp; {</small><br>
>> <small>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new
>> IllegalArgumentException("The 'no null'
>> constraint is violated");</small><br>
>> <small>&nbsp;&nbsp;&nbsp; }</small><br>
>> <br>
>> <small>&nbsp;&nbsp;&nbsp; return object;</small><br>
>> <small>&nbsp; }</small><br>
>> </blockquote>
>> &nbsp;<br>
>> Miles Parker wrote:
>> <blockquote cite="mid:ffp7tp$k0g$1@build.eclipse.org" type="cite">On
>> 2007-10-24 20:59:57 -0700, <a class="moz-txt-link-abbreviated"
>> href="mailto:andrew_steady@hotmail.com">andrew_steady@hotmail.com</a>
>> (Andy Steady)
>> said:
>> <br>
>> <br>
>> <blockquote type="cite">Hi,
>> <br>
>> <br>
>> As there is no specific setter for adding a single value to a list, (we
>> do getX().add(y)) how can we prevent certain additions to the list -
>> for example we wish to prevent a relationship being added if it would
>> result in a cyclic dependancy.
>> <br>
>> <br>
>> We could do this with a post validation, or by restricting the options
>> in the first place - but I in this case we need to prevent on set.
>> <br>
>> What's the usual solution to this?
>> <br>
>> <br>
>> Andy
>> <br>
>> </blockquote>
>> <br>
>> Andy, this is really a job for EMF.edit I think, e.g. it is not a
>> validation issue per se but a model editing issue. In addition to
>> post-validation you can override the Objects item provider
>> createAddCommand to return UnexecutableCommand in the case where the
>> added command item would involve a cyclic dependency. hth..
>> <br>
>> <br>
>> </blockquote>
>> <br>
>> </body>
>> </html>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:disabling / locking the new child/sibling Items
Next Topic:Simple TransactionalEditingDomain command
Goto Forum:
  


Current Time: Thu Apr 25 21:12:49 GMT 2024

Powered by FUDForum. Page generated in 0.03168 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top