1

私はそのようないくつかのコードを持っています:

@Override
public int hashCode() {
    int hash = 5;
    hash = 47 * hash + Objects.hashCode(this.bendWidth);
    hash = 47 * hash + Objects.hashCode(this.bendSideLength);
    hash = 47 * hash + Objects.hashCode(this.thickness);
    hash = 47 * hash + Objects.hashCode(this.innerRadius);
    hash = 47 * hash + Objects.hashCode(this.bendAngle);
    hash = 47 * hash + Objects.hashCode(this.kfactor);
    hash = 47 * hash + Objects.hashCode(this.bendShortening);
    return hash;
}

このソースを 1.6 で動作させることを検討しています。

これまでのところ、グアバを試しました:

@Override
public int hashCode() {
    int hash = 5;


    Object[] objs = new Object[]{
        this.getPointND().getPoint()[0],
        this.getPointND().getPoint()[1],
        this.getPointND().getPoint()[2],
        this.getPointND().getPoint()[3],
        this.getPointND().getPoint()[4],
        this.kfactor,
        this.bendShortening
    };

    hash = 47 * hash + Objects.hashCode(objs);

    hash = 47 * hash + Objects.hashCode(this.bendWidth);
    hash = 47 * hash + Objects.hashCode(this.bendSideLength);
    hash = 47 * hash + Objects.hashCode(this.thickness);
    hash = 47 * hash + Objects.hashCode(this.innerRadius);
    hash = 47 * hash + Objects.hashCode(this.bendAngle);
    hash = 47 * hash + Objects.hashCode(this.kfactor);
    hash = 47 * hash + Objects.hashCode(this.bendShortening);
    return hash;
}

そして、私は解決策を試しました:

   @Override
    public int hashCode() {
        int hash = 5;


        Object[] objs = new Object[]{
            this.getPointND().getPoint()[0],
            this.getPointND().getPoint()[1],
            this.getPointND().getPoint()[2],
            this.getPointND().getPoint()[3],
            this.getPointND().getPoint()[4],
            this.kfactor,
            this.bendShortening
        };

        hash = 47 * hash + Objects.hashCode(objs);

        hash = 47 * hash + Objects.hashCode(this.bendWidth);
        hash = 47 * hash + Objects.hashCode(this.bendSideLength);
        hash = 47 * hash + Objects.hashCode(this.thickness);
        hash = 47 * hash + Objects.hashCode(this.innerRadius);
        hash = 47 * hash + Objects.hashCode(this.bendAngle);
        hash = 47 * hash + Objects.hashCode(this.kfactor);
        hash = 47 * hash + Objects.hashCode(this.bendShortening);
        return hash;
    }

しかし、それでもこのテストは失敗を返します:

@Test
public void testHashCodeIsDifferentHashCode() {
    try {
        DataPoint pointOne = new DataPoint();
        pointOne.setBendAngle(new Double(1));
        pointOne.setBendShortening(new Double(1));
        pointOne.setBendSideLength(1);
        pointOne.setBendWidth(1);
        pointOne.setInnerRadius(1);
        pointOne.setKfactor(new Double(1));
        pointOne.setThickness(1);

        DataPoint pointTwo = new DataPoint();
        pointTwo.setBendAngle(0);
        pointTwo.setBendShortening(new Double(0));
        pointTwo.setBendSideLength(0);
        pointTwo.setBendWidth(0);
        pointTwo.setInnerRadius(0);
        pointTwo.setKfactor(new Double(0));
        pointTwo.setThickness(0);

        DataPoint pointThree = new DataPoint();
        pointThree.setBendAngle(Double.NaN);
        pointThree.setBendShortening(Double.NaN);
        pointThree.setBendSideLength(Double.NaN);
        pointThree.setBendWidth(Double.NaN);
        pointThree.setInnerRadius(Double.NaN);
        pointThree.setKfactor(Double.NaN);
        pointThree.setThickness(Double.NaN);

        Set<DataPoint> map = new HashSet<DataPoint>();
        map.add(pointOne);
        map.add(pointTwo);
        assert (map.size() == 2);
    } catch (NullPointerException ex) {
        assert false : "failed due to null";
    } catch (Exception ex) {
        assert false : "failed, unknown error.";
    }
}

これを試しました:

@Override
public int hashCode() {
    int hash = 5;
    hash = 47 * hash + Objects.hashCode(this.bendWidth);
    hash = 47 * hash + Objects.hashCode(this.bendSideLength);
    hash = 47 * hash + Objects.hashCode(this.thickness);
    hash = 47 * hash + Objects.hashCode(this.innerRadius);
    hash = 47 * hash + Objects.hashCode(this.bendAngle);
    hash = 47 * hash + Objects.hashCode(this.kfactor);
    hash = 47 * hash + Objects.hashCode(this.bendShortening);
    return hash;
}
public class Objects {

    public static int hashCode(Object object) {
        return object == null ? 0 : object.hashCode();
    }
}

私のイコールソリューションは次のとおりです。

@Override
public boolean equals(Object obj) {
    if (obj instanceof DataPoint) {
        DataPoint od = (DataPoint) obj;
        return (this.bendAngle == od.bendAngle)
                && (this.bendShortening == od.bendShortening)
                && (this.bendSideLength == od.bendSideLength)
                && (this.bendWidth == od.bendWidth)
                && (this.innerRadius == od.innerRadius)
                && (this.kfactor == od.kfactor)
                && (this.thickness == od.thickness);
    }
    return false;
}
4

3 に答える 3

0

equals メソッドが問題でした。a new Double(0.0) == new Double(0.0) は false と評価されます。

于 2013-06-03T17:52:39.340 に答える