[
http://jira.jboss.com/jira/browse/JGRP-591?page=comments#action_12376819 ]
Bela Ban commented on JGRP-591:
-------------------------------
Forgot the code:
public static void main(String[] args) throws Exception {
Map<Address,Integer> map=new HashMap<Address,Integer>();
IpAddress addr1, addr2;
InetAddress ip1 = InetAddress.getByAddress( new byte[] {1,2,3,4} );
int port1 = 2;
int hash1 = ip1.hashCode() + port1;
System.out.println( "hash1 = " + hash1 );
InetAddress ip2 = InetAddress.getByAddress( new byte[] {1,2,3,5} );
int port2 = 1;
int hash2 = ip2.hashCode() + port2;
System.out.println( "hash2 = " + hash2 );
addr1=new IpAddress(ip1, port1);
addr2=new IpAddress(ip2, port2);
System.out.println("addr1 = " + addr1);
System.out.println("addr2 = " + addr2);
System.out.println("addr1.hashCode()=" + addr1.hashCode());
System.out.println("addr2.hashCode()=" + addr2.hashCode());
int ret=addr1.compareTo(addr2);
System.out.println("addr1.compareTo(addr2)=" + ret);
ret=addr2.compareTo(addr1);
System.out.println("addr2.compareTo(addr1)=" + ret);
map.put(addr1, 1);
map.put(addr2, 2);
System.out.println("map = " + map + ", size=" + map.size());
}
IpAddress.hashCode() not unique per ip/port
-------------------------------------------
Key: JGRP-591
URL:
http://jira.jboss.com/jira/browse/JGRP-591
Project: JGroups
Issue Type: Bug
Affects Versions: 2.4.1 SP4
Environment: windows xp sp2, jdk 1.6 update 2
Reporter: robert chou
Assigned To: Bela Ban
Fix For: 2.6, 2.5.1, 2.4.1 SP5
Current implementation produces hashcode where ip/port combos "1.2.3.4:2" and
"1.2.3.5:1" result in the same hash code.
Existing code:
public final int hashCode() {
return ip_addr != null ? ip_addr.hashCode() + port : port;
}
Manual test code:
InetAddress ip1 = InetAddress.getByAddress( new byte[] {1,2,3,4} );
int port1 = 2;
int hash1 = ip1.hashCode() + port1;
System.out.println( "hash1 = " + hash1 );
InetAddress ip2 = InetAddress.getByAddress( new byte[] {1,2,3,5} );
int port2 = 1;
int hash2 = ip2.hashCode() + port2;
System.out.println( "hash2 = " + hash2 );
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.jboss.com/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira