All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mcs.jmeasurement.DefaultMonitor Maven / Gradle / Ivy

There is a newer version: 1.1.226
Show newest version
/*
 * MCS Media Computer Software Copyright (c) 2005 by MCS
 * -------------------------------------- Created on 23.04.2005 by w.klaas
 * 
 * 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 de.mcs.jmeasurement;

import java.text.SimpleDateFormat;
import java.util.Date;

import de.mcs.utils.StringFormat;

/**
 * This is the default implementation of the monitor class. Here all measurement
 * will be done. This monitor is not thread safe. Threadsafty will be
 * implemented thru the monitorFactory and the MeasurePoint class
 * implementation.
 * 
 * @author w.klaas
 */
public class DefaultMonitor implements Monitor {

  /** time when the monitor starts measurement. */
  private long startTime;

  /** time when the monitor starts measurement. */
  private long pauseTime;

  /** measurement is actually running. */
  private boolean running;

  /** measurement is actually paused. */
  private boolean paused;

  /** time elapsed since last measuremtent. */
  private long accrued;

  /** the parent MEasurePoint (for call back functions). */
  private MeasurePoint point;

  /** id of this monitor. */
  private String monitorId;

  /** this monitor has recorded an exception. */
  private boolean isException;

  /** the text of the exception. */
  private String exceptionText;

  /**
   * This is the default constructor.
   */
  public DefaultMonitor() {
    reset();
    point = null;
    monitorId = "";
  }

  /**
   * This is the default constructor.
   * 
   * @param aPoint
   *            the measure point (for callback functions)
   */
  public DefaultMonitor(final MeasurePoint aPoint) {
    this();
    point = aPoint;
  }

  /**
   * This is the default constructor.
   * 
   * @param aPoint
   *            the measure point (for callback functions)
   * @param aMonitorId
   *            id of this monitor
   */
  public DefaultMonitor(final MeasurePoint aPoint, final String aMonitorId) {
    this(aPoint);
    monitorId = aMonitorId;
  }

  /**
   * This is the default constructor.
   * 
   * @param aMonitorId
   *            id of this monitor
   */
  public DefaultMonitor(final String aMonitorId) {
    this();
    monitorId = aMonitorId;
  }

  /**
   * starting the measurement of this monitor.
   * 
   * @return boolean true if the monitor could be startet,
   *         otherwise false
   * @see de.mcs.jmeasurement.Monitor#start()
   */
  public final boolean start() {
    startTime = System.currentTimeMillis();
    running = true;
    if (null != point) {
      point.activateMonitor(this);
    }
    return true;
  }

  /**
   * pausing the measurement.
   * 
   * @return boolean true if the monitor could be paused,
   *         otherwise false
   * @see de.mcs.jmeasurement.Monitor#pause()
   */
  public final boolean pause() {
    if (running) {
      pauseTime = System.currentTimeMillis();
      paused = true;
      return true;
    } else {
      return false;
    }
  }

  /**
   * resume the measurement.
   * 
   * @return boolean true if the monitor could be resumed,
   *         otherwise false
   * @see de.mcs.jmeasurement.Monitor#resume()
   */
  public final boolean resume() {
    if (running) {
      accrued += pauseTime - startTime;
      startTime = System.currentTimeMillis();
      paused = false;
      return true;
    } else {
      return false;
    }
  }

  /**
   * @see de.mcs.jmeasurement.Monitor#increase(long)
   * @param msec
   *            time to increase
   */
  public final void increase(final long msec) {
    accrued += msec;
  }

  /**
   * @see de.mcs.jmeasurement.Monitor#decrease(long)
   * @param msec
   *            time to decrease
   */
  public final void decrease(final long msec) {
    accrued -= msec;
  }

  /**
   * stopping the measurment.
   * 
   * @return boolean true if the monitor could be stopped,
   *         otherwise false
   * @see de.mcs.jmeasurement.Monitor#stop()
   */
  public final boolean stop() {
    if (running) {
      accrued = accrued + timeElapsed();
      running = false;
      if (null != point) {
        point.processMonitor(this);
      }
      return true;
    } else {
      return false;
    }
  }

  /**
   * @return long the time elapsed since last call to start() or resume().
   */
  private long timeElapsed() {
    return System.currentTimeMillis() - startTime;
  }

  /**
   * @return long getting the measured time.
   * @see de.mcs.jmeasurement.Monitor#getAccrued()
   */
  public final long getAccrued() {
    return accrued;
  }

  /**
   * @see de.mcs.jmeasurement.Monitor#reset()
   */
  public final void reset() {
    accrued = 0;
    running = false;
    startTime = 0;
    isException = false;
  }

  /**
   * @return boolean the monitor is actual running
   * @see de.mcs.jmeasurement.Monitor#isRunning()
   */
  public final boolean isRunning() {
    return running;
  }

  /**
   * @return boolean the monitor is actual paused
   * @see de.mcs.jmeasurement.Monitor#isPaused()
   */
  public final boolean isPaused() {
    return paused;
  }

  /**
   * This methode will be called if the garbage collector will be remove this
   * object. So we can use the callback to add a death object if the monitor
   * is still runnning.
   * 
   * @throws Throwable
   *             if something goes wrong
   * @see java.lang.Object#finalize()
   */
  protected final void finalize() throws Throwable {
    super.finalize();
    if (running) {
      point.deathMonitor(this);
    }
  }

  /**
   * @return the id of this monitor
   * @see de.mcs.jmeasurement.Monitor#getMonitoId()
   */
  public final String getMonitoId() {
    return monitorId;
  }

  /**
   * @return the striong representation of this monitor.
   * @see java.lang.Object#toString()
   */
  public final String toString() {
    return getMonitoId() + ":" + getAccrued();
  }

  /**
   * @return if this monitor has get an exception.
   * @see de.mcs.jmeasurement.Monitor#hasException()
   * @since 0.64
   */
  public final boolean hasException() {
    return isException;
  }

  /**
   * @return the exception text.
   * @see de.mcs.jmeasurement.Monitor#getException()
   * @since 0.64
   */
  public final String getException() {
    return exceptionText;
  }

  /**
   * @param text
   *            the text to set for the exception.
   * @see de.mcs.jmeasurement.Monitor#setException(String)
   * @since 0.64
   */
  public final void setException(final String text) {
    isException = true;
    exceptionText = text;
    stop();
  }

  /**
   * @param throwable
   *            the exception to set.
   * @see de.mcs.jmeasurement.Monitor#setException(String)
   * @since 0.64
   */
  public final void setException(final Throwable throwable) {
    isException = true;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    switch (MeasureFactory.getExceptionHandling()) {
    case JMConfig.EXCEPTION_COUNT:
      exceptionText = "";
      break;
    case JMConfig.EXCEPTION_NAME:
      exceptionText = simpleDateFormat.format(new Date()) + ':' + throwable.toString();
      break;
    case JMConfig.EXCEPTION_TRACE:
      exceptionText = simpleDateFormat.format(new Date()) + ':' + StringFormat.getStackTrace(throwable);
      break;

    default:
      exceptionText = "";
      break;
    }
    ;
    stop();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy