Package gem.util

Source Code of gem.util.Progress

package gem.util;

import gem.util.Kronometre;

/**
* @author Ozgun Babur
*         Date: May 5, 2008
*         Time: 3:08:06 PM
*/
public class Progress
{
  /**
   * Length of the bar.
   */
  private int length;

  /**
   * Total count. If this is counted, then the job is complete.
   */
  private int totalTicks;
  private double total;

  private int counted;

  private Kronometre kron;

  public Progress(int totalTicks)
  {
    this(totalTicks, DEFAULT_LENGTH);
  }

  public Progress(int totalTicks, int length)
  {
    this.length = length;
    this.totalTicks = totalTicks;
    this.total = totalTicks;
    this.kron = new Kronometre();
    kron.start();

    System.out.print("\n|");
    for (int i = 0; i < length; i++) System.out.print(" ");
    System.out.print("|\n ");
  }

  public void tick()
  {
    counted++;

    if (counted == totalTicks)
    {
      kron.stop();
      System.out.print("  ");
      kron.print();
      return;
    }

    int p = (int) (Math.ceil((counted / total) * length) -
          Math.ceil(((counted - 1) / total) * length));

    if (p == 1) System.out.print(DOT);
    else if (p > 1) for (int i = 0; i < p; i++) System.out.print(DOT);
  }

  private static final String DOT = "#";

  public static void main(String[] args)
  {
    for (int i = 0; i < 500; i++)
    {
      System.out.println(i + "\t" + (char)i);
    }

    Progress p = new Progress(100, 54);

    for (int i = 0; i < 54; i++) p.tick();
  }

  public static final int DEFAULT_LENGTH = 100;
}

TOP

Related Classes of gem.util.Progress

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.