Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RangeParameter.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Script;
4
6{
10 public abstract class RangeParameter<T> : Parameter
11 where T : struct, IComparable<T>
12 {
13 private T? @value;
14 private T? min = null;
15 private T? max = null;
16 private bool minIncluded = true;
17 private bool maxIncluded = true;
18
22 public T? Value
23 {
24 get => this.@value;
25 set
26 {
27 this.@value = value;
28 this.ProtectedValue = null;
29 }
30 }
31
35 public T? Min
36 {
37 get => this.min;
38 set => this.min = value;
39 }
40
44 public T? Max
45 {
46 get => this.max;
47 set => this.max = value;
48 }
49
53 public bool MinIncluded
54 {
55 get => this.minIncluded;
56 set => this.minIncluded = value;
57 }
58
62 public bool MaxIncluded
63 {
64 get => this.maxIncluded;
65 set => this.maxIncluded = value;
66 }
67
71 public override object ObjectValue => this.@value;
72
77 public override void Populate(Variables Variables)
78 {
79 Variables[this.Name] = this.@value;
80 }
81
88 public override Task<bool> IsParameterValid(Variables Variables, ContractsClient Client)
89 {
90 int Diff;
91
92 if (!(this.Value.HasValue))
93 {
94 this.ErrorReason = ParameterErrorReason.LacksValue;
95 this.ErrorText = null;
96
97 return Task.FromResult(false);
98 }
99
100 IComparable<T> Compare = this.Value.Value;
101
102 if (this.Min.HasValue)
103 {
104 Diff = Compare.CompareTo(this.Min.Value);
105
106 if (Diff < 0 || (Diff == 0 && !this.MinIncluded))
107 {
108 this.ErrorReason = ParameterErrorReason.BelowMin;
109 this.ErrorText = null;
110
111 return Task.FromResult(false);
112 }
113 }
114
115 if (this.Max.HasValue)
116 {
117 Diff = Compare.CompareTo(this.Max.Value);
118
119 if (Diff > 0 || (Diff == 0 && !this.MaxIncluded))
120 {
121 this.ErrorReason = ParameterErrorReason.AboveMax;
122 this.ErrorText = null;
123
124 return Task.FromResult(false);
125 }
126 }
127
128 return base.IsParameterValid(Variables, Client);
129 }
130
131 }
132}
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Abstract base class for contractual parameters
Definition: Parameter.cs:17
override void Populate(Variables Variables)
Populates a variable collection with the value of the parameter.
bool MinIncluded
If the optional minimum value is included in the allowed range.
bool MaxIncluded
If the optional maximum value is included in the allowed range.
override Task< bool > IsParameterValid(Variables Variables, ContractsClient Client)
Checks if the parameter value is valid.
override object ObjectValue
Parameter value.
Collection of variables.
Definition: Variables.cs:25
ParameterErrorReason
Enumeration of different parameter validation reasons.