|  | Creating restrictions on the values returned |  | |
| | | nbhat |  |
| Posted: Mon Jun 30, 2008 11:52 am Post subject: Creating restrictions on the values returned |  |
I am creating an interface which has a method that returns a list of objects. I want to have restriction on number of elements returned by the implementation class. Though number of max elements allowed is passed as an argument to the method there is no guaranty that the implementation class will adhere to the numbers. How can I have stronger contract here ?
One of the ways could be change the return type to an iterator and stop iterating once the number of object reaches the required number of objects. Are there other better ways of doing this ?
- nbhat |
| |
| | | S Perryman |  |
| Posted: Mon Jun 30, 2008 2:19 pm Post subject: Re: Creating restrictions on the values returned |  |
| |  | |
nbhat wrote:
| Quote: | I am creating an interface which has a method that returns a list of objects. I want to have restriction on number of elements returned by the implementation class. Though number of max elements allowed is passed as an argument to the method there is no guaranty that the implementation class will adhere to the numbers. How can I have stronger contract here ?
|
You cannot. The weakest post-condition that you can have for the interface is :
type T { elements(int Max) : collection(Element) // post : RESULT.size <= Max }
An implementation can obviously strengthen the contract. For example :
MyT confirms to T { elements(int Max) : collection(Element) // post : RESULT.size <= MIN(5,Max) }
| Quote: | One of the ways could be change the return type to an iterator and stop iterating once the number of object reaches the required number of objects.
|
You have merely translated the problem into another form here. How many iterator.advance() invocations can you make (in relation to Max) ??
Regards, Steven Perryman |
| |
| | | S Perryman |  |
| Posted: Tue Jul 01, 2008 7:51 am Post subject: Re: Creating restrictions on the values returned |  |
| |  | |
nbhat wrote:
| Quote: | On Jun 30, 9:19 pm, S Perryman <q...@q.com> wrote:
nbhat wrote:
One of the ways could be change the return type to an iterator and stop iterating once the number of object reaches the required number of objects.
You have merely translated the problem into another form here. How many iterator.advance() invocations can you make (in relation to Max) ??
My thinking behind using iterator was at least the calling class can stop calling iterator after the Max advances and not worry about dealing a huge number of elements returned by the implementer of Type T.
|
The user should do no such thing. If T only returns N elements, then the iterator created by the elements op should allow N invocations of an 'advance' op. After which a 'more' op should return false.
Which of course makes the post-condition for the elements op more complex to define, as you need a predicate that when given an iterator can count the number of advances need to reach the end. And then you must ensure the count value <= Max.
Regards, Steven Perryman |
| |
| | | nbhat |  |
| Posted: Tue Jul 01, 2008 9:08 am Post subject: Re: Creating restrictions on the values returned |  |
| |  | |
On Jun 30, 9:19 pm, S Perryman <q...@q.com> wrote:
| Quote: | nbhat wrote: I am creating an interface which has a method that returns a list of objects. I want to have restriction on number of elements returned by the implementation class. Though number of max elements allowed is passed as an argument to the method there is no guaranty that the implementation class will adhere to the numbers. How can I have stronger contract here ?
You cannot. The weakest post-condition that you can have for the interface is :
type T { elements(int Max) : collection(Element) // post : RESULT.size <= Max
}
An implementation can obviously strengthen the contract. For example :
MyT confirms to T { elements(int Max) : collection(Element) // post : RESULT.size <= MIN(5,Max)
} One of the ways could be change the return type to an iterator and stop iterating once the number of object reaches the required number of objects.
You have merely translated the problem into another form here. How many iterator.advance() invocations can you make (in relation to Max) ??
Regards, Steven Perryman
|
My thinking behind using iterator was at least the calling class can stop calling iterator after the Max advances and not worry about dealing a huge number of elements returned by the implementer of Type T. |
| |
| | | nbhat |  |
| Posted: Tue Jul 01, 2008 12:09 pm Post subject: Re: Creating restrictions on the values returned |  |
| |  | |
On Jul 1, 2:51 pm, S Perryman <q...@q.com> wrote:
| Quote: | nbhat wrote: On Jun 30, 9:19 pm, S Perryman <q...@q.com> wrote: nbhat wrote: One of the ways could be change the return type to an iterator and stop iterating once the number of object reaches the required number of objects. You have merely translated the problem into another form here. How many iterator.advance() invocations can you make (in relation to Max) ?? My thinking behind using iterator was at least the calling class can stop calling iterator after the Max advances and not worry about dealing a huge number of elements returned by the implementer of Type T.
The user should do no such thing. If T only returns N elements, then the iterator created by the elements op should allow N invocations of an 'advance' op. After which a 'more' op should return false.
Which of course makes the post-condition for the elements op more complex to define, as you need a predicate that when given an iterator can count the number of advances need to reach the end. And then you must ensure the count value <= Max.
Regards, Steven Perryman
|
Got the point you were making. Thanks !
- nbhat |
| |
|
|