Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3b409d01 authored by Steve McKay's avatar Steve McKay
Browse files

Push equals impl into SparseBooleanArray.

Also, implement hashcode, because it is naughty to not.

Change-Id: I2042dac6840cf07027871783a7b6763578fa805d
parent 0ab6b0e8
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -237,6 +237,41 @@ public class SparseBooleanArray implements Cloneable {
        mSize++;
    }

    @Override
    public int hashCode() {
        int hashCode = mSize;
        for (int i = 0; i < mSize; i++) {
            hashCode = 31 * hashCode + mKeys[i] | (mValues[i] ? 1 : 0);
        }
        return hashCode;
    }

    @Override
    public boolean equals(Object that) {
      if (this == that) {
          return true;
      }

      if (!(that instanceof SparseBooleanArray)) {
          return false;
      }

      SparseBooleanArray other = (SparseBooleanArray) that;
      if (mSize != other.mSize) {
          return false;
      }

      for (int i = 0; i < mSize; i++) {
          if (mKeys[i] != other.mKeys[i]) {
              return false;
          }
          if (mValues[i] != other.mValues[i]) {
              return false;
          }
      }
      return true;
    }

    /**
     * {@inheritDoc}
     *
+9 −9
Original line number Diff line number Diff line
@@ -438,22 +438,22 @@ public final class MultiSelectManager {
            return buffer.toString();
        }

        @Override
        public int hashCode() {
            return mSelection.hashCode();
        }

        @Override
        public boolean equals(Object that) {
          if (this == that) {
              return true;
          }

          if (that instanceof Selection) {
              Selection other = (Selection) that;
              for (int i = 0; i < mSelection.size(); i++) {
                  if (mSelection.keyAt(i) != other.mSelection.keyAt(i)) {
          if (!(that instanceof Selection)) {
              return false;
          }
              }
              return true;
          }
          return false;

          return mSelection.equals(((Selection) that).mSelection);
        }
    }

+17 −2
Original line number Diff line number Diff line
@@ -16,8 +16,7 @@

package com.android.documentsui;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import com.android.documentsui.MultiSelectManager.Selection;

@@ -80,6 +79,22 @@ public class MultiSelectManager_SelectionTest {
        other.add(5);
        other.add(9);
        assertEquals(selection, other);
        assertEquals(selection.hashCode(), other.hashCode());
    }

    @Test
    public void equalsCopy() {
        Selection other = new Selection();
        other.copyFrom(selection);
        assertEquals(selection, other);
        assertEquals(selection.hashCode(), other.hashCode());
    }

    @Test
    public void notEquals() {
        Selection other = new Selection();
        other.add(789);
        assertFalse(selection.equals(other));
    }

    @Test