Skip to content

Instantly share code, notes, and snippets.

@syxolk
Created December 17, 2018 09:56
Show Gist options
  • Save syxolk/0ddeac3a5864bef26aa4b72c4f566167 to your computer and use it in GitHub Desktop.
Save syxolk/0ddeac3a5864bef26aa4b72c4f566167 to your computer and use it in GitHub Desktop.
removeAll on keySet (with hashCode)
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class RemoveAllTest {
public static void main(String[] args) {
ServerName[] newReplicas = new ServerName[]{
new ServerName("127.0.0.1", 1234),
new ServerName("127.0.0.1", 5678)
};
HashMap<ServerName, Integer> replicas = new HashMap<>();
replicas.put(new ServerName("127.0.0.1", 1234), 1);
replicas.put(new ServerName("127.0.0.1", 5678), 2);
List<ServerName> addedReplicas = new ArrayList<>(Arrays.asList(newReplicas));
System.out.println(addedReplicas.removeAll(replicas.keySet()));
List<ServerName> removedReplicas = new ArrayList<>(replicas.keySet());
System.out.println(removedReplicas.removeAll(Arrays.asList(newReplicas)));
System.out.println(addedReplicas);
System.out.println(removedReplicas);
}
}
/**
* Immutable server hostname+port combination
*
* Automatically computes the hash.
*/
class ServerName implements Comparable<ServerName> {
private static final int MD5_HASH_LENGTH = 16;
private final String hostname;
private final int port;
private final byte[] hash;
/**
* Create a new server name with hostname and port
*
* The hash is computed here as well.
*
* @param hostname ip or domain name
* @param port port number
*/
public ServerName(String hostname, int port) {
this.hostname = hostname;
this.port = port;
this.hash = computeHash(hostname, port);
}
public ServerName(byte[] hash) {
this.hostname = "";
this.port = 0;
this.hash = hash;
}
public String getHostname() {
return hostname;
}
public int getPort() {
return port;
}
public byte[] getHash() {
return hash;
}
/**
* Computes the MD5 hash of hostname + ":" + port
*
* @param hostname ip or domain name
* @param port port number
* @return MD5 hash
*/
public static byte[] computeHash(String hostname, int port) {
final String strToHash = hostname + ":" + port;
try {
final MessageDigest alg = MessageDigest.getInstance("MD5");
alg.update(strToHash.getBytes(StandardCharsets.UTF_8));
return alg.digest();
} catch (NoSuchAlgorithmException e) {
System.exit(1);
return new byte[0];
}
}
@Override
public String toString() {
return hostname + ":" + port;
}
/**
* Check if this server and another server have the same hash.
* @param obj other server
* @return true if same hash, otherwise false
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ServerName)) {
return false;
}
final ServerName other = (ServerName) obj;
return Arrays.equals(other.hash, this.hash);
}
/**
* Check if another server has a higher/lower/same hash compared to this server.
* @param o other server
*/
@Override
public int compareTo(ServerName o) {
for (int i = 0; i < MD5_HASH_LENGTH; i++) {
final int result = Integer.compare(this.hash[i], o.hash[i]);
if (result != 0) {
return result;
}
}
return 0;
}
@Override
public int hashCode() {
return Arrays.hashCode(hash);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment