<commit_before>package sofia.graphics.internal;

import java.util.HashMap;
import sofia.graphics.Shape;
import sofia.graphics.ShapeView;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

// -------------------------------------------------------------------------
/**
 * Manages animations for all shapes system-wide.
 *
 * @author  <NAME>
 * @version 2011.12.04
 */
public class ShapeAnimationManager extends Thread
{
    private ShapeView view;
    private boolean running;
    private Object animatorToken = new Object();

    private ConcurrentLinkedQueue<Shape.Animator<?>> animators =
        new ConcurrentLinkedQueue<Shape.Animator<?>>();
    private HashMap<Shape, Shape.Animator<?>> currentAnimators =
        new HashMap<Shape, Shape.Animator<?>>();


    // ----------------------------------------------------------
    /**
     * Not intended for public use.
     */
    public ShapeAnimationManager(ShapeView view)
    {
        this.view = view;
        running = true;
    }


    // ----------------------------------------------------------
    public synchronized void setRunning(boolean value)
    {
        running = value;
    }


    // ----------------------------------------------------------
    public synchronized boolean isRunning()
    {
        return running;
    }


    // ----------------------------------------------------------
    public void enqueue(Shape.Animator<?> animator)
    {
        Shape shape = animator.getShape();
        if (currentAnimators.containsKey(shape))
        {
            currentAnimators.get(shape).stop();
        }

        currentAnimators.put(shape, animator);
        animators.offer(animator);

        synchronized (animatorToken)
        {
            animatorToken.notify();
        }
    }


    // ----------------------------------------------------------
    public void stop(Shape shape)
    {
    	Shape.Animator<?> animator = currentAnimators.get(shape);

        if (animator != null)
        {
            animator.stop();
        }
    }


    // ----------------------------------------------------------
    @Override
    public void run()
    {
        view.internalSetAutoRepaintForThread(false);

        while (running)
        {
            waitForSignal();

            while (!animators.isEmpty())
            {
                long start = System.currentTimeMillis();

                Iterator<Shape.Animator<?>> it = animators.iterator();

                while (it.hasNext())
                {
                	Shape.Animator<?> animator = it.next();
                    Shape shape = animator.getShape();

                    boolean ended =
                        animator.advanceTo(start);

                    if (ended)
                    {
                        it.remove();

                        if (currentAnimators.get(shape) == animator)
                        {
                            currentAnimators.remove(shape);
                        }
                    }
                }

                view.repaint();

                long end = System.currentTimeMillis();
                long length = end - start;

                long FRAME_TIME = 20;

                if (length < FRAME_TIME)
                {
                    try
                    {
                        Thread.sleep(FRAME_TIME - length);
                    }
                    catch (InterruptedException e)
                    {
                    	// Do nothing.
                    }
                }

                end = System.currentTimeMillis();
            }
        }
    }


    // ----------------------------------------------------------
    private void waitForSignal()
    {
        synchronized (animatorToken)
        {
            while (animators.isEmpty())
            {
                try
                {
                    animatorToken.wait();
                }
                catch (InterruptedException e)
                {
                    // Do nothing.
                }
            }
        }
    }
}<commit_msg>Added a "sofia.graphics.testingMode" system property that can be used to make all animations execute instantaneously, for automated grading purposes.<commit_after>package sofia.graphics.internal;

import java.util.HashMap;
import sofia.graphics.Shape;
import sofia.graphics.ShapeView;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

// -------------------------------------------------------------------------
/**
 * Manages animations for all shapes system-wide.
 *
 * @author  <NAME>
 * @version 2011.12.04
 */
public class ShapeAnimationManager
{
	public static final String TESTING_MODE_PROPERTY =
			"sofia.graphics.testingMode";

    private ShapeView view;
    private boolean running;
    private Object animatorToken = new Object();

    private ConcurrentLinkedQueue<Shape.Animator<?>> animators =
        new ConcurrentLinkedQueue<Shape.Animator<?>>();
    private HashMap<Shape, Shape.Animator<?>> currentAnimators =
        new HashMap<Shape, Shape.Animator<?>>();


    // ----------------------------------------------------------
    /**
     * Not intended for public use.
     */
    public ShapeAnimationManager(ShapeView view)
    {
        this.view = view;
        running = true;
    }


    // ----------------------------------------------------------
    public synchronized void setRunning(boolean value)
    {
        running = value;
    }


    // ----------------------------------------------------------
    public synchronized boolean isRunning()
    {
        return running;
    }


    // ----------------------------------------------------------
    public void enqueue(Shape.Animator<?> animator)
    {
    	if (isTestingMode())
    	{
    		// Just run the animator instantaneously.
    		
    		long endTime = System.currentTimeMillis()
    				+ animator.getDelay() + animator.getDuration();
    		animator.advanceTo(endTime);
    	}
    	else
    	{
	        Shape shape = animator.getShape();
	        if (currentAnimators.containsKey(shape))
	        {
	            currentAnimators.get(shape).stop();
	        }
	
	        currentAnimators.put(shape, animator);
	        animators.offer(animator);
	
	        synchronized (animatorToken)
	        {
	            animatorToken.notify();
	        }
    	}
    }


    // ----------------------------------------------------------
    public void stop(Shape shape)
    {
    	Shape.Animator<?> animator = currentAnimators.get(shape);

        if (animator != null)
        {
            animator.stop();
        }
    }


    // ----------------------------------------------------------
    public void start()
    {
    	if (!isTestingMode())
    	{
    		new ProductionThread().start();
    	}
    }


    // ----------------------------------------------------------
    private boolean isTestingMode()
    {
    	String testingProp = System.getProperty(
    			TESTING_MODE_PROPERTY, "false");
    	
    	return Boolean.parseBoolean(testingProp);    	
    }


    // ----------------------------------------------------------
    private class ProductionThread extends Thread
    {
        // ----------------------------------------------------------
	    @Override
	    public void run()
	    {
	        while (running)
	        {
	            waitForSignal();
	            view.internalSetAutoRepaintForThread(false);
	
	            while (!animators.isEmpty())
	            {
	                long start = System.currentTimeMillis();
	
	                Iterator<Shape.Animator<?>> it = animators.iterator();
	
	                while (it.hasNext())
	                {
	                	Shape.Animator<?> animator = it.next();
	                    Shape shape = animator.getShape();
	
	                    boolean ended =
	                        animator.advanceTo(start);
	
	                    if (ended)
	                    {
	                        it.remove();
	
	                        if (currentAnimators.get(shape) == animator)
	                        {
	                            currentAnimators.remove(shape);
	                        }
	                    }
	                }
	
	                view.repaint();
	
	                long end = System.currentTimeMillis();
	                long length = end - start;
	
	                long FRAME_TIME = 20;
	
	                if (length < FRAME_TIME)
	                {
	                    try
	                    {
	                        Thread.sleep(FRAME_TIME - length);
	                    }
	                    catch (InterruptedException e)
	                    {
	                    	// Do nothing.
	                    }
	                }
	
	                end = System.currentTimeMillis();
	            }
	
	            view.internalSetAutoRepaintForThread(true);
	        }
	    }
    }


    // ----------------------------------------------------------
    private void waitForSignal()
    {
        synchronized (animatorToken)
        {
            while (animators.isEmpty())
            {
                try
                {
                    animatorToken.wait();
                }
                catch (InterruptedException e)
                {
                    // Do nothing.
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.20367796428953278, 'fraction_numerical': 0.0021383513311237037, 'mean_word_length': 0.9610062893081761, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, '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': '6537009', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2067, 'n_words': 407}