
// ExpiringMapTester.java
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* A PassiveExpiringMap decorates a Map to delete 
   entries once their expiration time has been 
   reached. 

  Usage:
    > compile ExpiringMapTester.java
    > run ExpiringMapTester

   Also see:
     LRUMapTester.java
     OrderedMapTester.java
     TransformedMapTester
     SplitMapTester
     PredicateMapTester.java
   and
     MapUtilsTester.java

*/

import java.util.*;

import org.apache.commons.collections4.*;
import org.apache.commons.collections4.map.*;


public class ExpiringMapTester 
{
  public static void main(String[] args) 
  {
    Map<String, Integer> map = new HashMap<>(); 

    PassiveExpiringMap<String, Integer> emap = 
              new PassiveExpiringMap<>(2000, map); 

    // add three elements, the last after 1 sec delay
    emap.put("andrew", 17);
    emap.put("jim", 20);
    pause(1000);
    emap.put("jane", 18);
    System.out.println("Keys: " + emap.keySet() + "; Map: " + emap);

    // wait for values to disappear
    pause(1000);
    System.out.println("Keys: " + emap.keySet() + "; Map: " + emap);

    pause(1000);
    System.out.println("Keys: " + emap.keySet() + "; Map: " + emap);
  }  // end of main()



  public static void pause(int ms)
  {
    System.out.println("Pausing " + ms + "ms...");
    try {
      Thread.sleep(ms);
    }
    catch(InterruptedException ex) { }
  }  // end of pause()

}  // end of ExpiringMapTester class
