/*
Copyright 2011-2013 Frederic Langlet
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may obtain a copy of the License at

                http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kanzi.io;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

// An implementation of BlockListener to display block information (verbose option
// of the BlockCompressor/BlockDecompressor)
public class InfoPrinter implements BlockListener
{
   public enum Type { ENCODING, DECODING };
   
   private final PrintStream ps;
   private final AtomicInteger blockId;
   private final Map<Integer, BlockInfo> map;
   private final BlockEvent.Type[] thresholds;
   private final Type type;
   
   
   public InfoPrinter(Type type, PrintStream ps) 
   {
      if (ps == null)
         throw new NullPointerException("Invalid null print stream parameter");
      
      this.ps = ps;
      this.blockId = new AtomicInteger();
      this.type = type;
      this.map = new HashMap<Integer, BlockInfo>();
      this.thresholds = (type == Type.ENCODING) ? 
              new BlockEvent.Type[]
              { 
                 BlockEvent.Type.BEFORE_TRANSFORM,
                 BlockEvent.Type.AFTER_TRANSFORM,
                 BlockEvent.Type.AFTER_ENTROPY
              } :
              new BlockEvent.Type[]
              { 
                 BlockEvent.Type.AFTER_ENTROPY,
                 BlockEvent.Type.BEFORE_TRANSFORM,
                 BlockEvent.Type.AFTER_TRANSFORM
              };
   }
   
   
   @Override
   public void processEvent(BlockEvent evt) 
   {
      int currentBlockId = evt.getId();

      if (evt.getType() == this.thresholds[0])
      {
         // Register initial block size
         BlockInfo bi = new BlockInfo();
         bi.stage0Size = evt.getSize();
         this.map.put(currentBlockId, bi);
         bi.time = System.nanoTime();
      }
      else if (evt.getType() == this.thresholds[1])
      {
         // Register block size after stage 1
         BlockInfo bi = this.map.get(currentBlockId);
         
         if (bi != null)
            bi.stage1Size = evt.getSize();
      }
      else if (evt.getType() == this.thresholds[2])
      {
         // Need a synchronization point to ensure that the data
         // output happens in the proper order (increasing block ID)
         while (this.blockId.get() != currentBlockId-1)
         {    
            // Backoff improves performance in heavy contention scenarios
            LockSupport.parkNanos(1);
         }        
         
         // Get block size after stage 2
         int stage2Size = evt.getSize();
         BlockInfo bi = this.map.remove(currentBlockId);
         
         if (bi == null)
            return;
             
         long duration_ms = (System.nanoTime() - bi.time) / 1000000L; 
         
         // Display block info
         String msg = String.format("Block %d: %d => %d => %d", currentBlockId, 
                 bi.stage0Size, bi.stage1Size, stage2Size);

         // Add percentage for encoding
         if (this.type == Type.ENCODING)
            msg += String.format(" (%d%%)", (stage2Size*100/bi.stage0Size));
         
         // Optionally add hash
         if (evt.getHash() != null) 
            msg += ("  [" + Integer.toHexString(evt.getHash()) + "]");

         //msg += String.format(" [%d ms]", duration_ms);
         this.ps.println(msg);
         this.blockId.getAndSet(currentBlockId);
      }
   }
   
   
   static class BlockInfo
   {
      long time;
      int stage0Size;
      int stage1Size;
   }
   
}

@@ -655,20 +655,16 @@
 va.util.
-Hash
 Map;%0D%0Aim
@@ -670,32 +670,57 @@
 mport java.util.
+concurrent.ConcurrentHash
 Map;%0D%0Aimport jav
@@ -1532,16 +1532,26 @@
 p = new 
+Concurrent
 HashMap%3C
@@ -3363,24 +3363,26 @@
  %0D%0A         
+//
 long duratio

Use concurrent hashmap instead of regular hashmap to avoid potential deadlocks