Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs @ 17417

Last change on this file since 17417 was 17410, checked in by dpiringe, 5 years ago

#3026:

  • deleted JsonItemArrayControl and JsonItemDefaultControl
  • redesigned architecture for JsonItem: now there are different types of JsonItem (IntJsonItem, BoolJsonItem, ...) -> for better type safety and expandability
  • fixed bug in BaseConverter for GetMinValue and GetMaxValue for IntValue, but ignored for other value types (DoubleValue, DateTimeValue, ...) because the redesign of JsonItem-Architecture can make these two methods obsolet soon
  • fixed bug in JsonItemConverter to prevent null pointer exceptions
  • refactored value and range converters -> removed complicated generic ValueTypeValueConverter and ValueRangeConverter and implemented the necessary methods directly in concrete classes (improves readability and removes the need of reflection)
  • redesigned view handling in OptimizerIntegration -> dynamically seaches for JsonItemVMBase implementations, which are connected with a view
    • this enables better scaling with more user controls
  • JsonItemVMBase implements MVVM architecture
File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using Newtonsoft.Json.Linq;
9
10namespace HeuristicLab.JsonInterface {
11  public abstract class BaseConverter : IJsonItemConverter
12  {
13    public abstract int Priority { get; }
14    public abstract Type ConvertableType { get; }
15
16    public abstract void Inject(IItem item, IJsonItem data, IJsonItemConverter root);
17    public abstract IJsonItem Extract(IItem value, IJsonItemConverter root);
18
19    #region Helper
20    protected ValueType CastValue<ValueType>(object obj) {
21      if (obj is JToken)
22        return ((JToken)obj).ToObject<ValueType>();
23      else if (obj is IConvertible)
24        return (ValueType)Convert.ChangeType(obj, typeof(ValueType));
25      else return (ValueType)obj;
26    }
27
28    protected IItem Instantiate(Type type, params object[] args) =>
29      (IItem)Activator.CreateInstance(type,args);
30
31    protected T Instantiate<T>(params object[] args) => (T)Instantiate(typeof(T), args);
32
33    protected object GetMaxValue(Type t) {
34      TypeCode typeCode = Type.GetTypeCode(t);
35
36
37      if (t.IsEqualTo(typeof(PercentValue)))
38        return 1.0d;
39
40      if(t == typeof(IntValue)) {
41        return int.MaxValue;
42      }
43
44      switch (typeCode) {
45        case TypeCode.Int16: return short.MaxValue;
46        case TypeCode.Int32: return int.MaxValue;
47        case TypeCode.Int64: return long.MaxValue;
48        case TypeCode.UInt16: return ushort.MaxValue;
49        case TypeCode.UInt32: return uint.MaxValue;
50        case TypeCode.UInt64: return ulong.MaxValue;
51        case TypeCode.Single: return float.MaxValue;
52        case TypeCode.Double: return double.MaxValue;
53        case TypeCode.Decimal: return decimal.MaxValue;
54        case TypeCode.Byte: return byte.MaxValue;
55        case TypeCode.Boolean: return true;
56        default: return GetDefaultValue(t);
57      }
58    }
59
60    protected object GetMinValue(Type t) {
61      TypeCode typeCode = Type.GetTypeCode(t);
62
63      if (t.IsEqualTo(typeof(PercentValue)))
64        return 0.0d;
65
66      if (t == typeof(IntValue)) {
67        return int.MinValue;
68      }
69
70      switch (typeCode) {
71        case TypeCode.Int16: return short.MinValue;
72        case TypeCode.Int32: return int.MinValue;
73        case TypeCode.Int64: return long.MinValue;
74        case TypeCode.UInt16: return ushort.MinValue;
75        case TypeCode.UInt32: return uint.MinValue;
76        case TypeCode.UInt64: return ulong.MinValue;
77        case TypeCode.Single: return float.MinValue;
78        case TypeCode.Double: return double.MinValue;
79        case TypeCode.Decimal: return decimal.MinValue;
80        case TypeCode.Byte: return byte.MinValue;
81        case TypeCode.Boolean: return false;
82        default: return GetDefaultValue(t);
83      }
84    }
85
86    protected object GetDefaultValue(Type t) => t.IsValueType ? Activator.CreateInstance(t) : null;
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.