Changeset 11392 in josm


Ignore:
Timestamp:
2016-12-14T00:52:36+01:00 (8 years ago)
Author:
Don-vip
Message:

sonar - squid:S1206 - "equals(Object obj)" and "hashCode()" should be overridden in pairs (required equalsverifier 2.1.8)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/Storage.java

    r11342 r11392  
    33
    44import java.util.AbstractSet;
     5import java.util.Arrays;
    56import java.util.Collection;
    67import java.util.ConcurrentModificationException;
     
    239240    public synchronized int hashCode() {
    240241        int h = 0;
    241         for (T t : this) {
    242             h += hash.getHashCode(t);
     242        if (hash != null) {
     243            for (T t : this) {
     244                h += hash.getHashCode(t);
     245            }
    243246        }
    244247        return h;
     248    }
     249
     250    @Override
     251    public boolean equals(Object obj) {
     252        if (this == obj)
     253            return true;
     254        if (obj == null || getClass() != obj.getClass())
     255            return false;
     256        Storage<?> other = (Storage<?>) obj;
     257        return Arrays.equals(data, other.data)
     258                && hashCode() == obj.hashCode();
    245259    }
    246260
     
    321335        int hcode = rehash(ha.getHashCode(key));
    322336        int bucket = hcode & mask;
    323         while ((entry = data[bucket]) != null) {
     337        while (bucket < data.length && (entry = data[bucket]) != null) {
    324338            if (ha.equals(key, entry))
    325339                return bucket;
     
    500514        @Override
    501515        public boolean hasNext() {
     516            if (data == null) return false;
    502517            align();
    503518            return slot < data.length;
     
    533548        @Override
    534549        public boolean hasNext() {
     550            if (data == null) return false;
    535551            align();
    536552            return slot < data.length;
  • trunk/test/unit/org/openstreetmap/josm/data/osm/StorageTest.java

    r11325 r11392  
    22package org.openstreetmap.josm.data.osm;
    33
    4 import org.junit.Ignore;
    54import org.junit.Rule;
    65import org.junit.Test;
     
    98import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    109import nl.jqno.equalsverifier.EqualsVerifier;
     10import nl.jqno.equalsverifier.Warning;
    1111
    1212/**
     
    2626     */
    2727    @Test
    28     @Ignore("not ready")
    2928    public void testEqualsContract() {
    3029        EqualsVerifier.forClass(Storage.class).usingGetClass()
    31             /*.withPrefabValues(Collection.class, new HashSet<>(Arrays.asList(1)), new HashSet<>(Arrays.asList(2)))
    32             .withPrefabValues(AbstractCollection.class, new HashSet<>(Arrays.asList(1)), new HashSet<>(Arrays.asList(2)))
    33             .withPrefabValues(Set.class, new HashSet<>(Arrays.asList(1)), new HashSet<>(Arrays.asList(2)))
    34             .withPrefabValues(AbstractSet.class, new HashSet<>(Arrays.asList(1)), new HashSet<>(Arrays.asList(2)))*/
     30            .suppress(Warning.NONFINAL_FIELDS)
     31            .withIgnoredFields("arrayCopyNecessary", "hash", "mask", "modCount", "safeIterator", "size")
    3532            .withPrefabValues(Hash.class, Storage.<Integer>defaultHash(), Storage.<Boolean>defaultHash())
    3633            .verify();
Note: See TracChangeset for help on using the changeset viewer.