Suggested Pages

Monday, January 23, 2017

WeakHashMap (How it works)

WeakHashMap Unit Test

This is a simple example of JUnit class that can be used to understand WeakHashMap behaviour. Runtime.getRuntime().gc() is unreliable but it can help to understand how a WeakHashMap works.

import java.util.WeakHashMap;

import org.junit.Assert;
import org.junit.Test;


public class WeakHashMapTest {

 
 @Test
 public void testServer1() {
  Server server=new Server();
  add(server);
  Runtime.getRuntime().gc();
  Monitor monitor = get(server);
  Assert.assertNotNull(monitor);
 }
 
 @Test
 public void testServer1NOCG() {
  Server server=new Server();
  add(server);
  Monitor monitor = get(server);
  Assert.assertNotNull(monitor);
 }
 
 @Test
 public void testServer2() {
  Server2 server=new Server2();
  add2(server);
  Runtime.getRuntime().gc();
  String monitor = get2(server);
  Assert.assertNull(monitor);
 }
 
 @Test
 public void testServer2NOGC() {
  Server2 server=new Server2();
  add2(server);
  String monitor = get2(server);
  Assert.assertNotNull(monitor);
 }

 @Test
 public void testServer3() {
  Server3 server=new Server3();
  add3(server);
  Runtime.getRuntime().gc();
  Monitor monitor = get3(server);
  Assert.assertNotNull(monitor);
 }
 
 @Test
 public void testServer3NOGC() {
  Server3 server=new Server3();
  add3(server);
  Monitor monitor = get3(server);
  Assert.assertNotNull(monitor);
 }


 private static Monitor get(Server server) {
  return server.get(new Monitor(new String("1")));
 }
 
 private static void add(Server server){
  String string = new String("1");
  Monitor monitor = new Monitor(string);
  server.put(monitor);
  monitor=null;
  string=null;
 }
 
 private static String get2(Server2 server) {
  return server.get(new Monitor(new String("2")));
 }
 
 private static void add2(Server2 server){
  String string = new String("2");
  Monitor monitor = new Monitor(string);
  server.put(monitor);
  monitor=null;
  string=null;
 }
 
 private static Monitor get3(Server3 server) {
  return server.get(new String("3"));
 }
 
 private static void add3(Server3 server){
  String string = new String("3");
  Monitor monitor = new Monitor(string);
  server.put(monitor);
  monitor=null;
  string=null;
 }
 
 private static class Server{
  private WeakHashMap<Monitor, Monitor>weakMap=new WeakHashMap<>();
  
  public void put(Monitor monitor){
   weakMap.put(monitor, monitor);
  }
  public Monitor get(Monitor id){
   return  weakMap.get(id);
  }
 }
 
 private static class Server2{
  private WeakHashMap<Monitor, String>weakMap=new WeakHashMap<>();
  
  public void put(Monitor monitor){
   weakMap.put(monitor, monitor.getId());
  }
  public String get(Monitor id){
   return  weakMap.get(id);
  }
 }
 
 private static class Server3{
  private WeakHashMap<String, Monitor>weakMap=new WeakHashMap<>();
  
  public void put(Monitor monitor){
   weakMap.put(monitor.getId(), monitor);
  }
  public Monitor get(String id){
   return  weakMap.get(id);
  }
 }
 
 
 private static class Monitor{
  
  public Monitor(String id) {
   super();
   this.id = id;
  }

  private String id;

  public String getId() {
   return id;
  }

  public void setId(String id) {
   this.id = id;
  }

  @Override
  public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((id == null) ? 0 : id.hashCode());
   return result;
  }

  @Override
  public boolean equals(Object obj) {
   if (this == obj)
    return true;
   if (obj == null)
    return false;
   if (getClass() != obj.getClass())
    return false;
   Monitor other = (Monitor) obj;
   if (id == null) {
    if (other.id != null)
     return false;
   } else if (!id.equals(other.id))
    return false;
   return true;
  }

  @Override
  public String toString() {
   return "Monitor [id=" + id + "]";
  }
  
 }
}


No comments :

Post a Comment

Suggested Pages