fix hashCode and equals in account

This commit is contained in:
Daniel Gultsch 2023-03-08 12:57:24 +01:00
parent e971b77539
commit 9a0c2226c1
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
2 changed files with 41 additions and 17 deletions

View file

@ -1,7 +1,6 @@
package im.conversations.android.database.model;
import androidx.annotation.NonNull;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
@ -10,11 +9,12 @@ import com.google.common.primitives.Ints;
import im.conversations.android.IDs;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.parts.Resourcepart;
public class Account {
public final class Account {
public final long id;
@NonNull public final BareJid address;
@ -30,21 +30,6 @@ public class Account {
this.randomSeed = randomSeed;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Account account = (Account) o;
return Arrays.equals(randomSeed, account.randomSeed);
}
@Override
public int hashCode() {
// careful with hashCode and equals for byte arrays
return Objects.hashCode(super.hashCode(), Arrays.hashCode(randomSeed));
}
public boolean isOnion() {
final String domain = address.getDomain().toString();
return domain.endsWith(".onion");
@ -85,4 +70,21 @@ public class Account {
throw new RuntimeException(e);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Account account = (Account) o;
return id == account.id
&& address.equals(account.address)
&& Arrays.equals(randomSeed, account.randomSeed);
}
@Override
public int hashCode() {
int result = Objects.hash(id, address);
result = 31 * result + Arrays.hashCode(randomSeed);
return result;
}
}

View file

@ -0,0 +1,22 @@
package im.conversations.android.xmpp;
import im.conversations.android.IDs;
import im.conversations.android.database.model.Account;
import org.junit.Assert;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public class AccountTest {
@Test
public void testEquals() throws XmppStringprepException {
final var seed = IDs.seed();
final var accountOne = new Account(1L, JidCreate.bareFrom("test@example.com"), seed);
final var seedCopy = new byte[seed.length];
System.arraycopy(seed, 0, seedCopy, 0, seedCopy.length);
final var accountTwo = new Account(1L, JidCreate.bareFrom("test@example.com"), seedCopy);
Assert.assertEquals(accountOne, accountTwo);
Assert.assertEquals(accountOne.hashCode(), accountTwo.hashCode());
}
}