Review equality tests with classes employing pimpl idiom

Bug #1209886 reported by Richard Gomes
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
JQuantLib
New
Medium
Unassigned

Bug Description

This tasks consists of finding and review classes which are exposing methods such like
    * equals
    * eq
    * ne

It's critical we review the way equality is being implemented by certain classes, in particular those classes exposing some kind of "eq" method or overriding Object#equals method.

Observe that 'in general' Object#hashCode() must be overriden when Object#equals() is overridden.

In particular, the snippet of code below shows an example of how hashCode() and equals() must be implemented when the pimpl idiom is being employed. There's also an example of how Object#toString() must be implemented.

    //
    // overrides Object
    //

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((impl_ == null) ? 0 : impl_.hashCode());
        result = prime * result + ((impl_ == null) ? 0 : name().hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || !(obj instanceof DayCounter)) {
            return false;
        }

        final DayCounter other = (DayCounter) obj;
        if (this.empty() && other.empty()) {
            return true;
        }
        if (this.name().equals(other.name())) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return (impl_ == null) ? "null" : impl_.name();
    }

=============
Relationships
=============
related to http://bugs.launchpad.net/bugs/jquantlib-409
child of http://bugs.launchpad.net/bugs/jquantlib-364

Tags: code-review
Revision history for this message
Richard Gomes (frgomes) wrote :

Dropping back to the pot.

Feel free to assign to yourself :)

Cheers

Richard Gomes

Revision history for this message
Richard Gomes (frgomes) wrote :

Dropping back to the pot.

Revision history for this message
Richard Gomes (frgomes) wrote :

kicked to v0.1.4

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.