PageRenderTime 102ms CodeModel.GetById 99ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Otis/Functions/AvgFunction.cs

http://otis-lib.googlecode.com/
C# | 45 lines | 36 code | 8 blank | 1 comment | 0 complexity | 31e5da03318b34857e118dfdebc9d408 MD5 | raw file
 1using System;
 2using System.CodeDom;
 3using System.Collections.Generic;
 4using System.Text;
 5using Otis.CodeGen;
 6
 7namespace Otis.Functions
 8{
 9	public class AvgFunction : SimpleFunctionBase
10	{
11		public override IEnumerable<CodeStatement> GetInitializationStatements(AggregateFunctionContext context)
12		{
13			List<CodeStatement> statements = new List<CodeStatement>(base.GetInitializationStatements(context));
14
15			CodeStatement st = new CodeVariableDeclarationStatement(typeof(int),
16				GetCounterObjectName(context),
17				new CodeSnippetExpression("0"));
18
19			statements.Add(st);
20
21			return statements;
22		}
23
24		protected override string GetResultExpression()
25		{
26			return string.Format("{0}/{1}", Context.FunctionObjectName, GetCounterObjectName(Context));
27		}
28
29		private static string GetCounterObjectName(AggregateFunctionContext context)
30		{
31			return context.FunctionObjectName + "_Cnt";
32		}
33
34		protected override string GetFormatForExecutedExpression(AggregateFunctionContext context)
35		{
36			// todo: test
37			return string.Format("FN_OBJ = FN_OBJ + CURR_ITEM; {0}++;", GetCounterObjectName(context));
38		}
39
40		protected override Type GetDefaultTargetType()
41		{
42			return typeof(double);
43		}
44	}
45}