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
> 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.
> 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