package org.jlib.core.collection;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class CachingMapTest {

    @Test
    public void performance() {
        final Map<String, String> hashMap = new HashMap<>();
        hashMap.put("ja", "nein");
        hashMap.put("gut", "schlecht");
        hashMap.put("München", "Berlin");

        final Map<String, String> delegatingMap = new DelegatingMap<String, String>(hashMap) {
            // intentilonally empty
        };

        final Map<String, String> cacheMap = new CachingMap<>(hashMap);

        for (int i = 0; i < 20; i++) {
            System.out.println("HashMap:       " + measure(hashMap));
            System.out.println("DelegatingMap: " + measure(delegatingMap));
            System.out.println("CacheMap:      " + measure(cacheMap));
        }
    }

    private long measure(final Map<String, String> map) {
        final int MAX = 100;
        final String MUC = "ja";

        long start = System.nanoTime();

        for (int i = 0; i < MAX; i++)
            if (map.containsKey(MUC))
                map.get(MUC);

        long end = System.nanoTime();

        return end - start;
    }
}

@@ -77,16 +77,64 @@
 l.Map;%0A%0A
+import com.google.common.collect.ForwardingMap;%0A
 import o
@@ -148,16 +148,16 @@
 t.Test;%0A
-
 %0Apublic 
@@ -435,23 +435,23 @@
 String%3E 
-delegat
+forward
 ingMap =
@@ -455,23 +455,23 @@
 p = new 
-Delegat
+Forward
 ingMap%3CS
@@ -489,19 +489,13 @@
 ng%3E(
-hashMap
 ) %7B%0A
+%0A
     
@@ -506,31 +506,118 @@
     
-// intentilonally empty
+@Override%0A            protected Map%3CString, String%3E delegate() %7B%0A                return hashMap;%0A            %7D
 %0A   
@@ -866,23 +866,23 @@
 measure(
-delegat
+forward
 ingMap))

Test modified according to refactorings