Equals method must be symmetric and transitive.

This commit is contained in:
akwizgran
2012-12-09 20:29:29 +00:00
parent a97dc0de6b
commit 15ab5be476
8 changed files with 38 additions and 33 deletions

View File

@@ -16,14 +16,14 @@ public class ContactId {
return id;
}
@Override
public int hashCode() {
return id;
}
@Override
public boolean equals(Object o) {
if(o instanceof ContactId) return id == ((ContactId) o).id;
return false;
}
@Override
public int hashCode() {
return id;
}
}

View File

@@ -3,37 +3,40 @@ package net.sf.briar.api.protocol;
import java.util.Map;
import java.util.TreeMap;
public class Transport extends TreeMap<String, String> {
private static final long serialVersionUID = 4900420175715429560L;
public class Transport {
private final TransportId id;
private final TreeMap<String, String> properties;
public Transport(TransportId id, Map<String, String> p) {
super(p);
this.id = id;
properties = new TreeMap<String, String>(p);
}
public Transport(TransportId id) {
super();
this.id = id;
properties = new TreeMap<String, String>();
}
public TransportId getId() {
return id;
}
@Override
public int hashCode() {
return id.hashCode();
public Map<String, String> getProperties() {
return properties;
}
@Override
public boolean equals(Object o) {
if(o instanceof Transport) {
Transport t = (Transport) o;
return id.equals(t.id) && super.equals(o);
return id.equals(t.id) && properties.equals(t.properties);
}
return false;
}
@Override
public int hashCode() {
return id.hashCode();
}
}

View File

@@ -1207,7 +1207,7 @@ abstract class JdbcDatabase implements Database<Connection> {
t = new Transport(id);
transports.add(t);
}
t.put(rs.getString(2), rs.getString(3));
t.getProperties().put(rs.getString(2), rs.getString(3));
lastId = id;
}
rs.close();
@@ -2763,7 +2763,7 @@ abstract class JdbcDatabase implements Database<Connection> {
int batchSize = 0;
for(Transport t : transports) {
ps.setBytes(2, t.getId().getBytes());
for(Entry<String, String> e1 : t.entrySet()) {
for(Entry<String, String> e1 : t.getProperties().entrySet()) {
ps.setString(3, e1.getKey());
ps.setString(4, e1.getValue());
ps.addBatch();

View File

@@ -27,13 +27,13 @@ class GroupImpl implements Group {
return publicKey;
}
@Override
public boolean equals(Object o) {
return o instanceof Group && id.equals(((Group) o).getId());
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object o) {
return o instanceof Group && id.equals(((Group) o).getId());
}
}

View File

@@ -71,13 +71,13 @@ class MessageImpl implements Message {
return bodyLength;
}
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message) o).getId());
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message) o).getId());
}
}

View File

@@ -148,7 +148,7 @@ class ProtocolWriterImpl implements ProtocolWriter {
for(Transport p : t.getTransports()) {
w.writeStructId(Types.TRANSPORT);
w.writeBytes(p.getId().getBytes());
w.writeMap(p);
w.writeMap(p.getProperties());
}
w.writeListEnd();
w.writeInt64(t.getTimestamp());

View File

@@ -77,13 +77,13 @@ class TestMessage implements Message {
return new ByteArrayInputStream(raw);
}
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message)o).getId());
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message)o).getId());
}
}

View File

@@ -15,6 +15,7 @@ import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
@@ -158,10 +159,11 @@ public class ConstantsTest extends BriarTestCase {
for(int i = 0; i < MAX_TRANSPORTS; i++) {
TransportId id = new TransportId(TestUtils.getRandomId());
Transport t = new Transport(id);
Map<String, String> m = t.getProperties();
for(int j = 0; j < MAX_PROPERTIES_PER_TRANSPORT; j++) {
String key = createRandomString(MAX_PROPERTY_LENGTH);
String value = createRandomString(MAX_PROPERTY_LENGTH);
t.put(key, value);
m.put(key, value);
}
transports.add(t);
}