×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Gerrit Viljoen
Added: May 8, 2014 12:21 PM
Views: 16
Logs the progress (like 10%, 25%) of a process but only for certain stages (like every 10%).

The example logs every 10%.
  1.     private int lastMarker = 0;
  2.  
  3.     private void log(long index, long size) {
  4.         final int step = 10;
  5.         final long ratio = (index * 100) / size;
  6.         for (int lowMarker = 0; lowMarker <= 100; lowMarker += step) {
  7.             int highMarker = lowMarker + step;
  8.             if ((ratio >= lastMarker) && (ratio >= lowMarker) && (ratio < highMarker)) {
  9.                 System.out.println(lowMarker + "%");
  10.                 lastMarker = highMarker;
  11.             }
  12.         }
  13.     }