package com.carrotsearch.hppc.examples;

import java.util.*;

import org.junit.Test;

import com.carrotsearch.hppc.IntIntOpenHashMap;
import com.carrotsearch.hppc.hash.IntMurmurHash;
import com.carrotsearch.junitbenchmarks.AbstractBenchmark;

public class ForEach
{
    public void accessAnonymousClassFieldInForEach()
    {
        // [[[start:foreach-counting]]]
        // Create a map.
        final IntIntOpenHashMap map = new IntIntOpenHashMap();
        map.put(1, 2);
        map.put(2, 5);
        map.put(3, 10);

        int count = map.forEach(new IntIntProcedure() {
           int count;
           public void apply(int key, int value)
           {
               if (value >= 5) count++;
           }
        }).count;
        System.out.println("There are " + count + " values >= 5");
        // [[[end:foreach-counting]]]
    }

    @Test
    public void characterCountingWithJcfSmarter()
    {
        final char [] CHARS = DATA;
        final Map<Integer, IntHolder> counts = new HashMap<Integer, IntHolder>();
        for (int i = 0; i < CHARS.length - 1; i++)
        {
            final int bigram = CHARS[i] << 16 | CHARS[i+1];
            final IntHolder currentCount = counts.get(bigram);
            if (currentCount == null)
            {
                counts.put(bigram, new IntHolder(1));
            }
            else
            {
                currentCount.value++;
            }
        }
    }
    
    @Test
    public void characterCountingWithJcfNaive()
    {
        final char [] CHARS = DATA;
        final Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
        for (int i = 0; i < CHARS.length - 1; i++)
        {
            final int bigram = CHARS[i] << 16 | CHARS[i+1];
            final Integer currentCount = counts.get(bigram);
            if (currentCount == null)
            {
                counts.put(bigram, 1);
            }
            else
            {
                counts.put(bigram, currentCount + 1);
            }
        }
    }

    private static final class IntHolder
    {
        int value;

        IntHolder(int initial)
        {
            value = initial;
        }
    }
}

@@ -38,53 +38,8 @@
 s;%0A%0A
-import java.util.*;%0A%0Aimport org.junit.Test;%0A%0A
 impo
@@ -115,85 +115,34 @@
 ppc.
-hash.IntMurmurHash;%0Aimport com.carrotsearch.junitbenchmarks.AbstractBenchmark
+procedures.IntIntProcedure
 ;%0A%0Ap
@@ -474,16 +474,24 @@
 cedure()
+%0A       
  %7B%0A     
@@ -707,16 +707,16 @@
 %3E= 5%22);%0A
+
         
@@ -754,1334 +754,7 @@
    %7D
-%0A%0A    @Test%0A    public void characterCountingWithJcfSmarter()%0A    %7B%0A        final char %5B%5D CHARS = DATA;%0A        final Map%3CInteger, IntHolder%3E counts = new HashMap%3CInteger, IntHolder%3E();%0A        for (int i = 0; i %3C CHARS.length - 1; i++)%0A        %7B%0A            final int bigram = CHARS%5Bi%5D %3C%3C 16 %7C CHARS%5Bi+1%5D;%0A            final IntHolder currentCount = counts.get(bigram);%0A            if (currentCount == null)%0A            %7B%0A                counts.put(bigram, new IntHolder(1));%0A            %7D%0A            else%0A            %7B%0A                currentCount.value++;%0A            %7D%0A        %7D%0A    %7D%0A    %0A    @Test%0A    public void characterCountingWithJcfNaive()%0A    %7B%0A        final char %5B%5D CHARS = DATA;%0A        final Map%3CInteger, Integer%3E counts = new HashMap%3CInteger, Integer%3E();%0A        for (int i = 0; i %3C CHARS.length - 1; i++)%0A        %7B%0A            final int bigram = CHARS%5Bi%5D %3C%3C 16 %7C CHARS%5Bi+1%5D;%0A            final Integer currentCount = counts.get(bigram);%0A            if (currentCount == null)%0A            %7B%0A                counts.put(bigram, 1);%0A            %7D%0A            else%0A            %7B%0A                counts.put(bigram, currentCount + 1);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private static final class IntHolder%0A    %7B%0A        int value;%0A%0A        IntHolder(int initial)%0A        %7B%0A            value = initial;%0A        %7D%0A    %7D
 %0A%7D%0A

Corrected example.