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

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

There is a newer version: 1.1.226
Show newest version
/*
 * MCS Media Computer Software Copyright (c) 2006 by MCS
 * -------------------------------------- Created on 20.03.2006 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.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import de.mcs.jmeasurement.MeasurePoint.PRIORITY;
import de.mcs.jmeasurement.exception.RendererMustNotBeNullException;
import de.mcs.jmeasurement.renderer.MeasureDataRenderer;
import de.mcs.jmeasurement.renderer.MeasurementReporter;

/**
 * This class contains all data for a snapshot.
 * @author w.klaas
 */
public class SnapShot {

  /** id of this snapshot. */
  private static int count = 0;

  /** free memory of this snapshot. */
  private static final String KEY_FREEMEMORY = "freememory";

  /** max memory of this snapshot. */
  private static final String KEY_MAXMEMORY = "maxmemory";

  /** total memory of this snapshot. */
  private static final String KEY_TOTALMEMORY = "totalmemory";

  /** name of this snapshot. */
  private String name;

  /** id of this snapshot. */
  private int id;

  /** date of this snapshot. */
  private Date date;

  /** all measure points of this snapshot. */
  private HashMap measurePoints;

  /** properties for this snapshot. */
  private Properties properties;

  /**
   * Constructor to create a snapshot with the desired name.
   * @param snapshotname
   *            name of this snapshot.
   */
  public SnapShot(final String snapshotname) {
    this.name = snapshotname;
    properties = new Properties();
    measurePoints = new HashMap();
    initFields();
    id = count++;
  }

  /**
   * init fields with values.
   */
  private void initFields() {
    this.date = new Date();
    properties.setProperty(KEY_FREEMEMORY, Long.toString(Runtime.getRuntime().freeMemory()));
    properties.setProperty(KEY_MAXMEMORY, Long.toString(Runtime.getRuntime().maxMemory()));
    properties.setProperty(KEY_TOTALMEMORY, Long.toString(Runtime.getRuntime().totalMemory()));
  }

  /**
   * @return name of the snapshot.
   */
  public final String getName() {
    return name;
  }

  /**
   * @return date of the snapshot.
   */
  public final Date getDate() {
    return (Date) date.clone();
  }

  /**
   * @return free memory of this snapshot.
   */
  public final long getFreeMemory() {
    String mem = properties.getProperty(KEY_FREEMEMORY);
    if (mem == null) {
      return 0L;
    } else {
      return Long.parseLong(mem);
    }
  }

  /**
   * @return max memory of this snapshot.
   */
  public final long getMaxMemory() {
    String mem = properties.getProperty(KEY_MAXMEMORY);
    if (mem == null) {
      return 0L;
    } else {
      return Long.parseLong(mem);
    }
  }

  /**
   * @return total memory of this snapshot.
   */
  public final long getTotalMemory() {
    String mem = properties.getProperty(KEY_TOTALMEMORY);
    if (mem == null) {
      return 0L;
    } else {
      return Long.parseLong(mem);
    }
  }

  /**
   * Now cloning all measurepoints for this snapshot.
   * @param orgMeasurePoints
   *            map with all measure points
   */
  public final void cloneMeasurePoints(final Map orgMeasurePoints) {
    this.measurePoints = new HashMap(orgMeasurePoints.size());
    for (String key : orgMeasurePoints.keySet()) {
      MeasurePoint point = orgMeasurePoints.get(key);
      MeasurePoint addPoint = (MeasurePoint) point.clone();
      measurePoints.put(key, addPoint);
    }
  }

  /**
   * getting a report for this snapshot.
   * @param pointname
   *            regular expression to match the point names (or null for all
   *            points)
   * @param renderer
   *            the renderer to use.
   * @param priority
   *            the priority of points, which has to be added to the report.
   * @return String the report
   * @throws RendererMustNotBeNullException
   *             if something goes wrong
   * @throws IOException 
   */
  public final String getReport(final String pointname, final MeasureDataRenderer renderer, final PRIORITY priority)
      throws RendererMustNotBeNullException {
    StringWriter writer = new StringWriter();

    try {
      getReport(pointname, renderer, priority, writer);
    } catch (IOException e) {
      // Should never occure
    }

    return writer.toString();
  }

  /**
   * getting a report for this snapshot.
   * @param pointname
   *            regular expression to match the point names (or null for all
   *            points)
   * @param renderer
   *            the renderer to use.
   * @param priority
   *            the priority of points, which has to be added to the report.
   * @param output
   *            the writer to report to.
   * @throws RendererMustNotBeNullException
   *             if something goes wrong
   * @throws IOException
   *             if something goes wrong with the writer IO operation.
   */
  public final void getReport(final String pointname, final MeasureDataRenderer renderer, final PRIORITY priority,
      final Writer output) throws RendererMustNotBeNullException, IOException {
    MeasurePoint[] points = getMeasurePoints(pointname);

    MeasurementReporter reporter = new MeasurementReporter(MeasureFactory.getConfig(), points, null);
    reporter.setPriority(MeasureFactory.getPriority());

    reporter.getSnapshotReport(this, renderer, output);
  }

  /**
   * getting an array of measurement points. The pointname is a regular
   * expression of the desired points.
   * @param pointName
   *            regular expression of the desired point names (or null for all
   *            points)
   * @return MeasurePoint[] the desired MeasurePoints
   */
  public final MeasurePoint[] getMeasurePoints(final String pointName) {
    String regexPointName = pointName;
    if (null == regexPointName) {
      regexPointName = ".*";
    }

    Pattern compile = Pattern.compile(regexPointName);

    Set keySet = measurePoints.keySet();
    ArrayList activePointNames = keySet.stream().filter(p -> compile.matcher(p).matches()).sequential()
        .collect(Collectors.toCollection(ArrayList::new));

    activePointNames.sort(Comparator.naturalOrder());

    List points = new ArrayList<>();

    activePointNames.forEach(name -> points.add(measurePoints.get(name)));

    return (MeasurePoint[]) points.toArray(new MeasurePoint[0]);
  }

  /**
   * getting the properties ofthis snapshot.
   * @return Properties
   */
  public final Properties getProperties() {
    return properties;
  }

  /**
   * Adding a new measurepoint to the point registration.
   * @param measurePoint
   *            measure point to add
   */
  public final void register(final MeasurePoint measurePoint) {
    measurePoints.put(measurePoint.getName(), measurePoint);
  }

  /**
   * @return the id
   */
  public int getId() {
    return id;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy