Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > returning false from Collection.add()

Reply
Thread Tools

returning false from Collection.add()

 
 
VisionSet
Guest
Posts: n/a
 
      12-03-2003
Having read the API docs for Collection.add
What are the pitfalls, when my implementation decides not to add for its own
reasons and returns false.

My Collection disallows 'overlapping' objects, that is not to say they are
Object.equal
So I'm not creating a kind of Set.
There is only one way equals can be implemented (for any one class) compared
with (forgive pun) compare(), which can be implemented in many ways via
Comparator.

If I could have multiple equals methods I could have any 'overlapping'
object is equal equals().
But there isn't and there isn't a:

java.util.Set.add(Object obj, Comparator comp)

on which to judge object equality.

--
Mike W


 
Reply With Quote
 
 
 
 
=?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
Guest
Posts: n/a
 
      12-03-2003
On Wed, 03 Dec 2003 21:40:40 +0000, VisionSet wrote:

> Having read the API docs for Collection.add What are the pitfalls, when my
> implementation decides not to add for its own reasons and returns false.
>
> My Collection disallows 'overlapping' objects, that is not to say they are
> Object.equal
> So I'm not creating a kind of Set.


You _ARE_ creating a set, just using a different criteria as to what makes
Objects sufficiently unique.

> There is only one way equals can be implemented (for any one class)
> compared with (forgive pun) compare(), which can be implemented in many
> ways via Comparator.
>
> If I could have multiple equals methods I could have any 'overlapping'
> object is equal equals().
> But there isn't and there isn't a:
>
> java.util.Set.add(Object obj, Comparator comp)
>
> on which to judge object equality.


Wouldn't you want to use the same Comparator for all items in the Set?

Check out java.util.TreeSet to see if it meets your needs.

Aloha,
La'ie Techie

 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      12-04-2003
"La?ie Techie" <laie@wing_this_etsolutions.com> wrote in message
news:(E-Mail Removed) s.com...
>
> > There is only one way equals can be implemented (for any one class)
> > compared with (forgive pun) compare(), which can be implemented in many
> > ways via Comparator.
> >
> > If I could have multiple equals methods I could have any 'overlapping'
> > object is equal equals().
> > But there isn't and there isn't a:
> >
> > java.util.Set.add(Object obj, Comparator comp)
> >
> > on which to judge object equality.

>
> Wouldn't you want to use the same Comparator for all items in the Set?
>
> Check out java.util.TreeSet to see if it meets your needs.


Ahh!! A TreeX class, first time for everything, look Mum...

Cheers.
--
Mike W


 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      12-04-2003
VisionSet wrote:

> Having read the API docs for Collection.add
> What are the pitfalls, when my implementation decides not to add for its own
> reasons and returns false.


The only potential pitfall I see is with code that neglects to check the
return value of add(Object) and just assumes that the attempted addition
succeeded.

> My Collection disallows 'overlapping' objects, that is not to say they are
> Object.equal
> So I'm not creating a kind of Set.


Assuming that an object always overlaps itself, you are indeed creating
a set in the mathematical sense (as our Hawaiian friend already pointed
out) -- a more restrictive one than the general purpose versions
provided in the platform API. As you seem to be observing, however,
your set violates the general Set contract with respect to the
add(Object) method [and dependant methods]. It therefore should not
implement Set, even though it looks and feels a lot like one.

> There is only one way equals can be implemented (for any one class) compared
> with (forgive pun) compare(), which can be implemented in many ways via
> Comparator.
>
> If I could have multiple equals methods I could have any 'overlapping'
> object is equal equals().
> But there isn't and there isn't a:
>
> java.util.Set.add(Object obj, Comparator comp)
>
> on which to judge object equality.


Java Collections are inherently based on Object.equals(). If you only
need to worry about overlaps at object addition time then you can
probably do well by implementing the Collection interface directly,
perhaps with the help of AbstractCollection. If, however, you
anticipate a need to take overlaps into account elsewhere -- in contains
or remove functionality, for instance -- then you might be better off
just building your own. You could use one or another type of Collection
internally, if you wanted, but your class itself would have some
fundamental differences from a Java Collection.

If you were ambitious you might write a generalization of the
Collections API that could handle this sort of thing with a configurable
policy object that served the place of Object.equals().


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CustomValidator Returns False, but False is Ignored DJ ASP .Net 3 12-27-2007 10:29 AM
0 == False but [] != False? Rajarshi Python 20 05-29-2007 11:04 PM
debug="false" in web.config and <%@ debug="true" ...%> in aspx file => true or false? André ASP .Net 3 08-28-2006 12:02 PM
False positive, false intrusion, false alarm Nick Computer Security 3 04-26-2006 07:40 PM
false instance and false class and set_trace_func trans. (T. Onoma) Ruby 0 09-25-2004 05:34 PM



Advertisments