<commit_before>package org.geepawhill.contentment.timing;

import org.geepawhill.contentment.model.Step;
import org.geepawhill.contentment.model.Timing;

public class TimingBuilder
{

	public static final String RELATIVES_BUT_NO_TOTAL = "Included relative timing with 0d parent.";
	public static final String ABSOLUTE_OVERRUN = "Absolutes are bigger than allocated.";
	private double accumulatedAbsolute;
	private double accumulatedRelative;
	
	public double build(double total, Step... steps)
	{
		Timing[] timings = new Timing[steps.length];
		int dest = 0;
		for(Step step: steps)
		{
			timings[dest++]=step.timing();
		}
		return build(total,timings);
	}

	public double build(double total, Timing...timings)
	{
		gatherTotals(timings);
		if(total==0d) return noTotalSupplied();
		double distribute = amountLeftToDistribute(total);
		return distributeRelatives(distribute, timings);
	}

	private double distributeRelatives(double distribute, Timing... timings)
	{
		double afterDistribution = accumulatedAbsolute;
		for(Timing timing : timings)
		{
			double ratio = timing.getRatio();
			if(ratio!=0d)
			{
				double ms = (ratio*distribute)/accumulatedRelative;
				afterDistribution+=ms;
				timing.setAbsolute( ms);
			}
		}
		return afterDistribution;
	}

	private double amountLeftToDistribute(double total)
	{
		double distribute = total - accumulatedAbsolute;
		if(distribute<0d) throw new RuntimeException(ABSOLUTE_OVERRUN);
		return distribute;
	}

	private double noTotalSupplied()
	{
		if(accumulatedRelative>0d)
		{
			throw new RuntimeException(RELATIVES_BUT_NO_TOTAL);
		}
		return accumulatedAbsolute;
	}

	private void gatherTotals(Timing... timings)
	{
		accumulatedAbsolute = 0d;
		accumulatedRelative = 0d;
		for(Timing timing : timings)
		{
			if(timing.getRatio()==0d)
			{
				accumulatedAbsolute+=timing.getAbsolute();
			}
			else
			{
				accumulatedRelative+=timing.getRatio();
			}
		}
	}

}
<commit_msg>Minor cleanup.<commit_after>package org.geepawhill.contentment.timing;

import org.geepawhill.contentment.model.Step;
import org.geepawhill.contentment.model.Timing;

public class TimingBuilder
{

	public static final String RELATIVES_BUT_NO_TOTAL = "Included relative timing with 0d parent.";
	public static final String ABSOLUTE_OVERRUN = "Absolutes are bigger than allocated.";
	private double accumulatedAbsolute;
	private double accumulatedRelative;

	public double build(double total, Step... steps)
	{
		Timing[] timings = new Timing[steps.length];
		int dest = 0;
		for (Step step : steps)
		{
			timings[dest++] = step.timing();
		}
		return build(total, timings);
	}

	public double build(double total, Timing... timings)
	{
		gatherTotals(timings);
		if (total == 0d) return noTotalSupplied();
		double distribute = amountLeftToDistribute(total);
		return distributeRelatives(distribute, timings);
	}

	private double distributeRelatives(double distribute, Timing... timings)
	{
		double afterDistribution = accumulatedAbsolute;
		for (Timing timing : timings)
		{
			double ratio = timing.getRatio();
			if (ratio != 0d)
			{
				double ms = (ratio * distribute) / accumulatedRelative;
				afterDistribution += ms;
				timing.setAbsolute(ms);
			}
		}
		return afterDistribution;
	}

	private double amountLeftToDistribute(double total)
	{
		double distribute = total - accumulatedAbsolute;
		if (distribute < 0d) throw new RuntimeException(ABSOLUTE_OVERRUN);
		return distribute;
	}

	private double noTotalSupplied()
	{
		if (accumulatedRelative > 0d) { throw new RuntimeException(RELATIVES_BUT_NO_TOTAL); }
		return accumulatedAbsolute;
	}
	
	private void gatherAbsolutes()
	{
		accumulatedAbsolute = 0d;
		for (Timing timing : timings)
		{
			if (timing.getRatio() == 0d)
			{
				accumulatedAbsolute += timing.getAbsolute();
			}
		}
	}

	private void gatherTotals(Timing... timings)
	{
		accumulatedAbsolute = 0d;
		accumulatedRelative = 0d;
		for (Timing timing : timings)
		{
			if (timing.getRatio() == 0d)
			{
				accumulatedAbsolute += timing.getAbsolute();
			}
			else
			{
				accumulatedRelative += timing.getRatio();
			}
		}
	}

}

{'fraction_non_alphanumeric': 0.10322108345534407, 'fraction_numerical': 0.004880429477794046, 'mean_word_length': 4.2483994878361075, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2892664', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1305, 'n_words': 299}