Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating restrictions on the values returned

0 views
Skip to first unread message

nbhat

unread,
Jun 30, 2008, 7:52:22 AM6/30/08
to
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

unread,
Jun 30, 2008, 12:19:14 PM6/30/08
to
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

nbhat

unread,
Jul 1, 2008, 5:08:31 AM7/1/08
to

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.

S Perryman

unread,
Jul 1, 2008, 5:51:23 AM7/1/08
to
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

nbhat

unread,
Jul 1, 2008, 8:09:14 AM7/1/08
to

Got the point you were making. Thanks !

- nbhat

0 new messages