<commit_before>package pl.pwr.hiervis.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.lang.reflect.Array;

import basic_hierarchy.interfaces.Instance;
import basic_hierarchy.interfaces.Node;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.activity.ActivityManager;
import prefuse.util.ColorLib;
import prefuse.util.GraphicsLib;
import prefuse.util.display.DisplayLib;


public class Utils
{
	private Utils()
	{
		// Static class -- disallow instantiation.
		throw new RuntimeException( "Attempted to instantiate a static class: " + getClass().getName() );
	}

	public static void waitUntilActivitiesAreFinished()
	{
		while ( ActivityManager.activityCount() > 0 ) {
			try {
				// Poll every 100ms
				Thread.sleep( 100 );
			}
			catch ( InterruptedException e ) {
				e.printStackTrace();
			}
		}
	}

	public static Point2D getDisplayCenter( Display display )
	{
		Rectangle2D contentRect = display.getVisibleRect();

		return new Point2D.Double(
			contentRect.getCenterX(),
			contentRect.getCenterY()
		);
	}

	public static void resetDisplayZoom( Display display, int margin, int duration )
	{
		Visualization vis = display.getVisualization();
		Rectangle2D bounds = vis.getBounds( Visualization.ALL_ITEMS );
		GraphicsLib.expand( bounds, margin + (int)( 1 / display.getScale() ) );
		DisplayLib.fitViewToBounds( display, bounds, duration );
	}

	public static BufferedImage getDisplaySnapshot( Display dis )
	{
		BufferedImage img = null;
		try {
			// get an image to draw into
			Dimension d = new Dimension( dis.getWidth(), dis.getHeight() );
			if ( !GraphicsEnvironment.isHeadless() ) {
				try {
					img = (BufferedImage)dis.createImage( dis.getWidth(), dis.getHeight() );
				}
				catch ( Exception e ) {
					img = null;
				}
			}

			if ( img == null ) {
				img = new BufferedImage(
					dis.getWidth(),
					dis.getHeight(),
					BufferedImage.TYPE_INT_RGB
				);
			}
			Graphics2D g = (Graphics2D)img.getGraphics();

			// set up the display, render, then revert to normal settings
			Point2D p = new Point2D.Double( 0, 0 );
			dis.zoom( p, 1.0 ); // also takes care of damage report
			boolean q = dis.isHighQuality();
			dis.setHighQuality( true );
			dis.paintDisplay( g, d );
			dis.setHighQuality( q );
			dis.zoom( p, 1.0 ); // also takes care of damage report
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}

		return img;
	}

	public static int rgba( Color c )
	{
		return ColorLib.rgba( c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha() );
	}

	/**
	 * Calculates the smallest rectangle containing all points within
	 * the specified group.
	 * 
	 * @param group
	 *            the group for which the extents are to be computed
	 * @param dimX
	 *            index of the data dimension imaged on the X axis
	 * @param dimY
	 *            index of the data dimension imaged on the Y axis
	 * @return the smallest bounding rectangle
	 */<commit_msg>Added Utils.timed(), for measuring execution time of methods.<commit_after>package pl.pwr.hiervis.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.lang.reflect.Array;

import org.apache.commons.lang3.time.StopWatch;
import org.apache.logging.log4j.Logger;

import basic_hierarchy.interfaces.Instance;
import basic_hierarchy.interfaces.Node;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.activity.ActivityManager;
import prefuse.util.ColorLib;
import prefuse.util.GraphicsLib;
import prefuse.util.display.DisplayLib;


public class Utils
{
	private Utils()
	{
		// Static class -- disallow instantiation.
		throw new RuntimeException( "Attempted to instantiate a static class: " + getClass().getName() );
	}

	public static void waitUntilActivitiesAreFinished()
	{
		while ( ActivityManager.activityCount() > 0 ) {
			try {
				// Poll every 100ms
				Thread.sleep( 100 );
			}
			catch ( InterruptedException e ) {
				e.printStackTrace();
			}
		}
	}

	public static Point2D getDisplayCenter( Display display )
	{
		Rectangle2D contentRect = display.getVisibleRect();

		return new Point2D.Double(
			contentRect.getCenterX(),
			contentRect.getCenterY()
		);
	}

	public static void resetDisplayZoom( Display display, int margin, int duration )
	{
		Visualization vis = display.getVisualization();
		Rectangle2D bounds = vis.getBounds( Visualization.ALL_ITEMS );
		GraphicsLib.expand( bounds, margin + (int)( 1 / display.getScale() ) );
		DisplayLib.fitViewToBounds( display, bounds, duration );
	}

	public static BufferedImage getDisplaySnapshot( Display dis )
	{
		BufferedImage img = null;
		try {
			// get an image to draw into
			Dimension d = new Dimension( dis.getWidth(), dis.getHeight() );
			if ( !GraphicsEnvironment.isHeadless() ) {
				try {
					img = (BufferedImage)dis.createImage( dis.getWidth(), dis.getHeight() );
				}
				catch ( Exception e ) {
					img = null;
				}
			}

			if ( img == null ) {
				img = new BufferedImage(
					dis.getWidth(),
					dis.getHeight(),
					BufferedImage.TYPE_INT_RGB
				);
			}
			Graphics2D g = (Graphics2D)img.getGraphics();

			// set up the display, render, then revert to normal settings
			Point2D p = new Point2D.Double( 0, 0 );
			dis.zoom( p, 1.0 ); // also takes care of damage report
			boolean q = dis.isHighQuality();
			dis.setHighQuality( true );
			dis.paintDisplay( g, d );
			dis.setHighQuality( q );
			dis.zoom( p, 1.0 ); // also takes care of damage report
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}

		return img;
	}

	/**
	 * Executes the specified runnable in a timed block, and prints the measured
	 * time to the specified logger, with the specified prefix.
	 * 
	 * @param log
	 *            the logger to print the result to
	 * @param prefix
	 *            the prefix in the printed message
	 * @param f
	 *            the runnable to time
	 */
	public static void timed( Logger log, String prefix, Runnable f )
	{
		StopWatch sw = new StopWatch();

		sw.start();
		f.run();
		sw.stop();

		log.trace(
			String.format(
				"%s: %sms (%sns)",
				prefix, sw.getTime(), sw.getNanoTime()
			)
		);
	}

	public static int rgba( Color c )
	{
		return ColorLib.rgba( c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha() );
	}

	/**
	 * Calculates the smallest rectangle containing all points within
	 * the specified group.
	 * 
	 * @param group
	 *            the group for which the extents are to be computed
	 * @param dimX
	 *            index of the data dimension imaged on the X axis
	 * @param dimY
	 *            index of the data dimension imaged on the Y axis
	 * @return the smallest bounding rectangle
	 */
{'fraction_non_alphanumeric': 0.10934795152914022, 'fraction_numerical': 0.0075014425851125215, 'mean_word_length': 3.219720024345709, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5697000', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2144, 'n_words': 622}