Source

NBody / NBody.DomainModel / Services / AnalyticsServiceBase.cs

Ade Miller b47cfa5 






Ade Miller 6e65c6c 


Ade Miller b47cfa5 





























// Copyright (c) Ade Miller.  All Rights Reserved.
// This code released under the terms of the
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
namespace NBody.DomainModel.Services
{
	/// <summary>
	/// Base class template from which <see cref="IAnalyticsService"/> implementations may inherit. 
	/// </summary>
	public abstract class AnalyticsServiceBase : IAnalyticsService
	{
		private WeakReference _latestUniverse;
		private AnalyticsResult _latestResults = new AnalyticsResult();

		public AnalyticsResult InitialResult { get; private set; }

		protected AnalyticsServiceBase()
		{
			InitialResult = new AnalyticsResult();
		}

		public AnalyticsResult Analyze(IUniverse universe)
		{
			if (universe.Bodies.Length == 0)
				return new AnalyticsResult(universe.Time, universe.StepCount);
			if ((universe.Time == _latestResults.Time) && 
				(_latestUniverse != null) && (_latestUniverse.Target == universe))
				return _latestResults;

			_latestResults = GenerateResult(universe.Bodies, universe.Time, universe.StepCount);
			_latestUniverse = new WeakReference(universe);

			if (universe.Time == 0.0)
				InitialResult = _latestResults;
			return _latestResults;
		}

		protected abstract AnalyticsResult GenerateResult(Body[] bodies, double time, int stepCount);
	}
}