[17263] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 | using HeuristicLab.Data;
|
---|
| 8 |
|
---|
[17284] | 9 | namespace HeuristicLab.JsonInterface {
|
---|
[17281] | 10 | public class ValueTypeValueConverter<ValueType, T> : BaseConverter
|
---|
[17263] | 11 | where ValueType : ValueTypeValue<T>
|
---|
| 12 | where T : struct {
|
---|
[17266] | 13 |
|
---|
[17283] | 14 | public override void InjectData(IItem item, JsonItem data) =>
|
---|
[17263] | 15 | item.Cast<ValueType>().Value = CastValue<T>(data.Default);
|
---|
| 16 |
|
---|
[17283] | 17 | public override JsonItem ExtractData(IItem value) =>
|
---|
| 18 | new JsonItem() {
|
---|
[17266] | 19 | Default = value.Cast<ValueType>().Value,
|
---|
[17280] | 20 | Range = new object[] { GetMinValue(), GetMaxValue() }
|
---|
[17266] | 21 | };
|
---|
[17263] | 22 |
|
---|
[17266] | 23 | #region Helper
|
---|
[17263] | 24 | private object GetMaxValue() {
|
---|
| 25 | TypeCode typeCode = Type.GetTypeCode(typeof(T));
|
---|
[17266] | 26 |
|
---|
| 27 | if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
|
---|
| 28 | return 1.0d;
|
---|
| 29 |
|
---|
[17263] | 30 | switch (typeCode) {
|
---|
| 31 | case TypeCode.Int16: return Int16.MaxValue;
|
---|
| 32 | case TypeCode.Int32: return Int32.MaxValue;
|
---|
| 33 | case TypeCode.Int64: return Int64.MaxValue;
|
---|
| 34 | case TypeCode.UInt16: return UInt16.MaxValue;
|
---|
| 35 | case TypeCode.UInt32: return UInt32.MaxValue;
|
---|
| 36 | case TypeCode.UInt64: return UInt64.MaxValue;
|
---|
| 37 | case TypeCode.Single: return Single.MaxValue;
|
---|
| 38 | case TypeCode.Double: return Double.MaxValue;
|
---|
| 39 | case TypeCode.Decimal: return Decimal.MaxValue;
|
---|
| 40 | case TypeCode.Byte: return Byte.MaxValue;
|
---|
| 41 | case TypeCode.Boolean: return true;
|
---|
| 42 | default: return default(T);
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
[17280] | 45 |
|
---|
| 46 | private object GetMinValue() {
|
---|
| 47 | TypeCode typeCode = Type.GetTypeCode(typeof(T));
|
---|
| 48 |
|
---|
| 49 | if (typeof(ValueType).IsEqualTo(typeof(PercentValue)))
|
---|
| 50 | return 0.0d;
|
---|
| 51 |
|
---|
| 52 | switch (typeCode) {
|
---|
| 53 | case TypeCode.Int16: return Int16.MinValue;
|
---|
| 54 | case TypeCode.Int32: return Int32.MinValue;
|
---|
| 55 | case TypeCode.Int64: return Int64.MinValue;
|
---|
| 56 | case TypeCode.UInt16: return UInt16.MinValue;
|
---|
| 57 | case TypeCode.UInt32: return UInt32.MinValue;
|
---|
| 58 | case TypeCode.UInt64: return UInt64.MinValue;
|
---|
| 59 | case TypeCode.Single: return Single.MinValue;
|
---|
| 60 | case TypeCode.Double: return Double.MinValue;
|
---|
| 61 | case TypeCode.Decimal: return Decimal.MinValue;
|
---|
| 62 | case TypeCode.Byte: return Byte.MinValue;
|
---|
| 63 | case TypeCode.Boolean: return false;
|
---|
| 64 | default: return default(T);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
[17266] | 67 | #endregion
|
---|
[17263] | 68 | }
|
---|
| 69 | }
|
---|