Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding hashCode() leads to objects with empty children #94

Open
Cy4n1d3 opened this issue Dec 15, 2015 · 1 comment
Open

Overriding hashCode() leads to objects with empty children #94

Cy4n1d3 opened this issue Dec 15, 2015 · 1 comment

Comments

@Cy4n1d3
Copy link

Cy4n1d3 commented Dec 15, 2015

I'm starting a new ticket for this as the other one is closed already.

Closely related to #78 I guess.

I'm kinda in the same spot right now as olavbg has been when he opened up this (#78) ticket.

I am overwriting equals() and hashCode() as I need to compare Lists and use contains() while disregarding any FilterValues which may have been (user)set before.

I have the following (pseudocode) classes:

class Filter extends RushObject {
    private int filterId;
    String name;
    String type;
    List<FilterValue> filterValues;

    // a bunch of constructors, getters, setters, methods

    @Override
    public int hashCode() {
        int hash = 17;
        hash = 31 * hash + filterId;
        hash = 31 * hash + (name != null ? name.hashCode() : 0);
        hash = 31 * hash + (type != null ? type.hashCode() : 0);

        return hash;
    }
}

class FilterValue extends RushObject {
    private int filterValueId;
    private String value;

    // snipped
}

On first start of the application (fresh database) while modifying and saving a Filter in my FilterFragment this Filter is being duplicated afterwards (without child objects - List is being omitted in this case).
This only happens on first start, while staying in the first (Home)View without switching activities and while overwriting hashCode.
If I comment out my custom hashCode(), this object duplication does not occur.

Possibly clarifying output from logcat - relevant parts being the Filter-object and the Rush='xxx' property:
hashCode() commented out, expected behaviour:

12-12 13:59:01.532    9579-9579/pg.recsys.app.debug W/FilterListAdapter﹕ Saving Filter (row toggled) Filter{fId=12, Rush='true', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='false', val='false'}]}
12-12 13:59:01.544    9579-9579/pg.recsys.app.debug W/Filter﹕ RUSH Saving: Filter{fId=12, Rush='true', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='true', val='false'}]}

custom hashCode() in place, unexpected behaviour:

12-12 14:02:47.633  11233-11233/pg.recsys.app.debug W/FilterListAdapter﹕ Saving Filter (row toggled) Filter{fId=12, Rush='false', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='false', val='false'}]}
12-12 14:02:47.642  11233-11233/pg.recsys.app.debug W/Filter﹕ RUSH Saving: Filter{fId=12, Rush='true', name='Raucherbereich', type='boolean', fVal=[FilterValue{fVId=23, Rush='true', val='true'}, FilterValue{fVId=24, Rush='true', val='false'}], uFVal=[FilterValue{fVId=23, Rush='true', val='false'}]}

Explanation of log output:

  • a textview with checkmark is toggled, the corresponding Filter is being modified and saved after
  • the first logcat snippet shows the wanted and expected behaviour: Rush='true', meaning the object has getId() != null; after saving, the List<> looks like I expected - no duplicated Filter objects, all children exist
  • the second logcat snipped shows something confusing: Rush='false' - afterwards we have duplicate entries inside our List<> without child objects

I could of course work around this problem by writing a custom contains() method which does not use equals() (meaning me not having to overwrite hashCode) but still I'd like to hear if such behaviour is to be expected? If yes, what's the reason for this?

PS: the same problem also applies to the FilterValue class which I've written a custom equals() and hashCode() for. As I don't need those right now I've commented them out for debugging purposes. If I use a custom hashCode on FilterValue as well, these objects also report Rush='false' on being modified after first start.

Kind regards,
Christian

@Cy4n1d3
Copy link
Author

Cy4n1d3 commented Jan 9, 2016

Any ideas? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant