AbstractInMemoryContainer has a Bug

In a MailClent I am using a Container that is fill with MailBox, with folders and folders with messages, the problem is that when I add meesages after to add 1 mail fox and 6 folders, the 6 fisrts messages are not show this is because when you add this nessages the method :

[code]
private ITEMCLASS internalAddAt(int position, ITEMIDTYPE itemId,

        ITEMCLASS item) {

[/code]in the If :

[code]
if (getAllItemIds().contains(itemId)) {

        return null;

    }

[/code]considers true and returns null, then this message object is not add,

in getALLIntemIds() you get:

[com.vaadin.demo.mobilemail.data.MailBox@30, com.vaadin.demo.mobilemail.data.Folder@31, com.vaadin.demo.mobilemail.data.Folder@32, com.vaadin.demo.mobilemail.data.Folder@33, com.vaadin.demo.mobilemail.data.Folder@34, com.vaadin.demo.mobilemail.data.Folder@35​]

and the
itmeId
of the message is by example:
com.vaadin.demo.mobilemail.data.Message@31
that is diffent excet the number after @ , then any
com.vaadin.demo.mobilemail.data.Message@nn
where nn is equal to any nn of the folder or Mail Box returns true in the
getAllItemIds().contains(itemId)
, then all that mesages are consider as that exist and that is not true because even the @nn is the same the class is diferent.

com.vaadin.demo.mobilemail.data.MailBox
com.vaadin.demo.mobilemail.data.Folder
com.vaadin.demo.mobilemail.data.Message

then
getAllItemIds().contains(itemId)
if itemid is from difenrent class should return false.

If I understood correctly your post, the issue you’re facing is caused by implementation of the equals method of your beans. You should implement equals in each class so that it compares the class too.

Hi Tapio, thanks for your quick answer.
well my beans are in a cotainer that is a subcclass BeamItemContainer that belongs to the com.vaadin.data.util of the Vaadin framework, (i can not just change this class as it belongs to Vaadin ) the method for filtering resides there, and when uses the
getAllItemIds().contains(itemId)
doesn’t takes in consideration the class only the ID, that you fill with setID, the workarround is to use a diferent ID for each Item even that are different class, but this adds complexity to the code, By example:

in this particular case , the folders ID are create and the ID is generated by code, but the messages uses the ID (message number) that the MailServer put to them, then you avoid to download duplicate email messages.

The confusing is that in the debug even tha mame says “IntemId” when you show contents it shows the class + @ + id by example :

com.vaadin.demo.mobilemail.data.MailBox@30

then this is different to

com.vaadin.demo.mobilemail.data.Message@30

I don’t see any scenary were diferent class (but with the same ID) should be consider as equal especially If yuo are usisng this to avoid instert duplicate items, different class are always diferent Items. By other hand consider them as different it helps to simplify your code.

Then I don’t know if here is the correct place to sugest a improvement, but should be the developer team of Vaadin who take in consideration this change or not.

What I meant was that
BeamItemContainer#getAllItemIds()
basicly returns just a normal List of Java. Its method contains uses normal
Object#equals(Object other)
method to make the comparison in order to check if there’s are matching item in the container.

So the result depends of the implementation of your POJOs. How have you defined
equals()
methods in MailBox and Message classes?

do you mean to implement this:

@Override public boolean equals(Object obj) { if (obj instanceof AbstractPojo) { AbstractPojo p = (AbstractPojo) obj; return p.getId() == getId(); } return false; } instead of the AbstractPojo (as is now) one in each class MailBox and Message by example for MailBox:

@Override
    public boolean equals(Object obj) {
        if (obj instanceof MailBox) {
            MailBox p = (MailBox) obj;
            return p.getId() == getId();
        }
        return false;
    }

That’s right. If all both POJOs are
AbstractPojo
s and then ids match, the first implementation returns true and therefore the objects are considered identica.

Yes, doing that way it works, Thank you very much for your help.

Then you can determine what is consider as duplicate implementing the equals(Object obj) in your POJO, this is interesting and more flexible than my previous proposal.