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; return id;
} }
@Override
public int hashCode() {
return id;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if(o instanceof ContactId) return id == ((ContactId) o).id; if(o instanceof ContactId) return id == ((ContactId) o).id;
return false; 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.Map;
import java.util.TreeMap; import java.util.TreeMap;
public class Transport extends TreeMap<String, String> { public class Transport {
private static final long serialVersionUID = 4900420175715429560L;
private final TransportId id; private final TransportId id;
private final TreeMap<String, String> properties;
public Transport(TransportId id, Map<String, String> p) { public Transport(TransportId id, Map<String, String> p) {
super(p);
this.id = id; this.id = id;
properties = new TreeMap<String, String>(p);
} }
public Transport(TransportId id) { public Transport(TransportId id) {
super();
this.id = id; this.id = id;
properties = new TreeMap<String, String>();
} }
public TransportId getId() { public TransportId getId() {
return id; return id;
} }
@Override public Map<String, String> getProperties() {
public int hashCode() { return properties;
return id.hashCode();
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if(o instanceof Transport) { if(o instanceof Transport) {
Transport t = (Transport) o; Transport t = (Transport) o;
return id.equals(t.id) && super.equals(o); return id.equals(t.id) && properties.equals(t.properties);
} }
return false; 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); t = new Transport(id);
transports.add(t); transports.add(t);
} }
t.put(rs.getString(2), rs.getString(3)); t.getProperties().put(rs.getString(2), rs.getString(3));
lastId = id; lastId = id;
} }
rs.close(); rs.close();
@@ -2763,7 +2763,7 @@ abstract class JdbcDatabase implements Database<Connection> {
int batchSize = 0; int batchSize = 0;
for(Transport t : transports) { for(Transport t : transports) {
ps.setBytes(2, t.getId().getBytes()); 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(3, e1.getKey());
ps.setString(4, e1.getValue()); ps.setString(4, e1.getValue());
ps.addBatch(); ps.addBatch();

View File

@@ -27,13 +27,13 @@ class GroupImpl implements Group {
return publicKey; return publicKey;
} }
@Override
public boolean equals(Object o) {
return o instanceof Group && id.equals(((Group) o).getId());
}
@Override @Override
public int hashCode() { public int hashCode() {
return id.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; return bodyLength;
} }
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message) o).getId());
}
@Override @Override
public int hashCode() { public int hashCode() {
return id.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()) { for(Transport p : t.getTransports()) {
w.writeStructId(Types.TRANSPORT); w.writeStructId(Types.TRANSPORT);
w.writeBytes(p.getId().getBytes()); w.writeBytes(p.getId().getBytes());
w.writeMap(p); w.writeMap(p.getProperties());
} }
w.writeListEnd(); w.writeListEnd();
w.writeInt64(t.getTimestamp()); w.writeInt64(t.getTimestamp());

View File

@@ -77,13 +77,13 @@ class TestMessage implements Message {
return new ByteArrayInputStream(raw); return new ByteArrayInputStream(raw);
} }
@Override
public boolean equals(Object o) {
return o instanceof Message && id.equals(((Message)o).getId());
}
@Override @Override
public int hashCode() { public int hashCode() {
return id.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.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map;
import net.sf.briar.BriarTestCase; import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils; import net.sf.briar.TestUtils;
@@ -158,10 +159,11 @@ public class ConstantsTest extends BriarTestCase {
for(int i = 0; i < MAX_TRANSPORTS; i++) { for(int i = 0; i < MAX_TRANSPORTS; i++) {
TransportId id = new TransportId(TestUtils.getRandomId()); TransportId id = new TransportId(TestUtils.getRandomId());
Transport t = new Transport(id); Transport t = new Transport(id);
Map<String, String> m = t.getProperties();
for(int j = 0; j < MAX_PROPERTIES_PER_TRANSPORT; j++) { for(int j = 0; j < MAX_PROPERTIES_PER_TRANSPORT; j++) {
String key = createRandomString(MAX_PROPERTY_LENGTH); String key = createRandomString(MAX_PROPERTY_LENGTH);
String value = createRandomString(MAX_PROPERTY_LENGTH); String value = createRandomString(MAX_PROPERTY_LENGTH);
t.put(key, value); m.put(key, value);
} }
transports.add(t); transports.add(t);
} }