1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace InABox.WPF
- {
- public abstract class AbstractCalculatorFunction<TValue, TType> where TType : struct, IComparable
- {
-
- protected abstract TValue Calculate(TValue current, TValue next, TType type) ;
- public TValue Calculate(TValue[] value, TType type)
- {
- if (!value.Any())
- return default;
- var result = value.First();
- result = value.Skip(1).Aggregate(result, (current, next) => Calculate(current, next, type));
- return result;
- }
-
- }
-
- public abstract class AbstractCalculator<TValue,TFunction, TType> : AbstractMultiConverter<TValue,TValue>
- where TFunction : AbstractCalculatorFunction<TValue,TType>, new()
- where TType : struct, IComparable
- {
- public TValue Default { get; private set; }
-
- public TType Type { get; set; }
-
- public TFunction Function { get; private set; }
- protected AbstractCalculator()
- {
- Default = default;
- Function = new TFunction();
- }
-
- protected override TValue Convert(IEnumerable<TValue> value, object parameter = null)
- {
- var enumerable = value as TValue[] ?? value?.ToArray() ?? new TValue[] { };
- if (Function == null || !enumerable.Any())
- return Default;
- var result = Function.Calculate(enumerable, Type);
- return result;
- }
- }
- }
|