Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Object

Creating restrictions on the values returned

 
Jump to:  
 
nbhat
PostPosted: 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
PostPosted: 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
PostPosted: 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
PostPosted: 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
PostPosted: 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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Mieszkania Kraków apartamenty Warszawa literatura Wózki widłowe Agregaty pompowe