Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RangeInfo.cs
1using System;
4using System.Text;
6
8{
12 public class RangeInfo : IEnumerable<Filter>
13 {
14 private readonly string fieldName;
15 private LinkedList<Filter> filters = null;
16 private object min;
17 private object max;
18 private object point;
19 private bool minInclusive;
20 private bool maxInclusive;
21 private bool isRange;
22 private bool isPoint;
23
28 public RangeInfo(string FieldName)
29 {
30 this.fieldName = FieldName;
31 this.filters = new LinkedList<Filter>();
32 }
33
39 public RangeInfo(string FieldName, LinkedList<Filter> Filters)
40 {
41 this.fieldName = FieldName;
42 this.filters = Filters;
43 }
44
48 public string FieldName => this.fieldName;
49
53 public object Min => this.min;
54
58 public object Max => this.max;
59
63 public object Point => this.point;
64
68 public bool MinInclusive => this.minInclusive;
69
73 public bool MaxInclusive => this.maxInclusive;
74
78 public bool IsRange => this.isRange;
79
83 public bool IsPoint => this.isPoint;
84
88 public bool IsOpenEndedRange
89 {
90 get { return this.IsRange && (this.min is null || this.max is null); }
91 }
92
96 public bool HasMin
97 {
98 get { return (this.isRange && !(this.min is null)) || (this.isPoint && !(this.point is null)); }
99 }
100
104 public bool HasMax
105 {
106 get { return (this.isRange && !(this.max is null)) || (this.isPoint && !(this.point is null)); }
107 }
108
112 public bool HasFilters => !(this.filters is null);
113
119 {
120 if (this.filters is null)
121 this.filters = new LinkedList<Filter>();
122
123 this.filters.AddLast(Filter);
124 }
125
131 public bool SetPoint(object Value)
132 {
133 int? i;
134
135 if (this.isRange)
136 {
137 if (!(this.min is null))
138 {
139 i = Comparison.Compare(this.min, Value);
140
141 if (!i.HasValue || i.Value > 0 || (i.Value == 0 && !this.minInclusive))
142 return false;
143
144 this.min = null;
145 }
146
147 if (!(this.max is null))
148 {
149 i = Comparison.Compare(this.max, Value);
150
151 if (!i.HasValue || i.Value < 0 || (i.Value == 0 && !this.maxInclusive))
152 return false;
153
154 this.max = null;
155 }
156
157 this.point = Value;
158 this.isRange = false;
159 this.isPoint = true;
160 return true;
161 }
162 else if (this.isPoint)
163 {
164 i = Comparison.Compare(this.point, Value);
165 return (i.HasValue && i.Value == 0);
166 }
167 else
168 {
169 this.isPoint = true;
170 this.point = Value;
171 return true;
172 }
173 }
174
182 public bool SetMin(object Value, bool Inclusive, out bool Smaller)
183 {
184 int? i;
185
186 Smaller = false;
187
188 if (this.isRange)
189 {
190 if (!(this.min is null))
191 {
192 i = Comparison.Compare(this.min, Value);
193
194 if (!i.HasValue)
195 return false;
196
197 if (i.Value < 0)
198 {
199 Smaller = true;
200
201 if (!this.minInclusive && Inclusive)
202 {
203 if (Comparison.Increment(ref this.min))
204 {
205 i = Comparison.Compare(this.min, Value);
206 if (i.HasValue && i.Value == 0)
207 Smaller = false;
208 }
209 }
210
211 this.min = Value;
212 this.minInclusive = Inclusive;
213 }
214 else if (i.Value == 0)
215 {
216 if (!Inclusive && this.minInclusive)
217 {
218 this.minInclusive = false;
219 Smaller = true;
220 }
221 }
222 }
223 else
224 {
225 this.min = Value;
226 this.minInclusive = Inclusive;
227 Smaller = true;
228 }
229
230 if (!(this.max is null))
231 {
232 i = Comparison.Compare(this.max, this.min);
233 if (!i.HasValue || i.Value < 0)
234 return false;
235 else if (i.Value == 0)
236 {
237 if (!(this.minInclusive && this.maxInclusive))
238 return false;
239
240 this.point = Value;
241 this.isPoint = true;
242 this.isRange = false;
243 this.min = null;
244 this.max = null;
245 }
246 }
247
248 return true;
249 }
250 else if (this.isPoint)
251 {
252 i = Comparison.Compare(this.point, Value);
253
254 if (Inclusive)
255 return i.HasValue && i.Value >= 0;
256 else
257 return i.HasValue && i.Value > 0;
258 }
259 else
260 {
261 this.min = Value;
262 this.minInclusive = Inclusive;
263 this.isRange = true;
264 Smaller = true;
265 return true;
266 }
267 }
268
276 public bool SetMax(object Value, bool Inclusive, out bool Smaller)
277 {
278 int? i;
279
280 Smaller = false;
281
282 if (this.isRange)
283 {
284 if (!(this.max is null))
285 {
286 i = Comparison.Compare(this.max, Value);
287
288 if (!i.HasValue)
289 return false;
290
291 if (i.Value > 0)
292 {
293 Smaller = true;
294
295 if (!this.maxInclusive && Inclusive)
296 {
297 if (Comparison.Decrement(ref this.max))
298 {
299 i = Comparison.Compare(this.max, Value);
300 if (i.HasValue && i.Value == 0)
301 Smaller = false;
302 }
303 }
304
305 this.max = Value;
306 this.maxInclusive = Inclusive;
307 }
308 else if (i.Value == 0)
309 {
310 if (!Inclusive && this.maxInclusive)
311 {
312 this.maxInclusive = false;
313 Smaller = true;
314 }
315 }
316 }
317 else
318 {
319 this.max = Value;
320 this.maxInclusive = Inclusive;
321 Smaller = true;
322 }
323
324 if (!(this.min is null))
325 {
326 i = Comparison.Compare(this.max, this.min);
327 if (!i.HasValue || i.Value < 0)
328 return false;
329 else if (i.Value == 0)
330 {
331 if (!(this.minInclusive && this.maxInclusive))
332 return false;
333
334 this.point = Value;
335 this.isPoint = true;
336 this.isRange = false;
337 this.min = null;
338 this.max = null;
339 }
340 }
341
342 return true;
343 }
344 else if (this.isPoint)
345 {
346 i = Comparison.Compare(this.point, Value);
347
348 return !(!i.HasValue || i.Value > 0 || (i.Value == 0 && !Inclusive));
349 }
350 else
351 {
352 this.max = Value;
353 this.maxInclusive = Inclusive;
354 this.isRange = true;
355 Smaller = true;
356 return true;
357 }
358 }
359
365 {
366 RangeInfo Result = new RangeInfo(this.fieldName, this.filters);
367 this.CopyTo(Result);
368 return Result;
369 }
370
375 public void CopyTo(RangeInfo Destination)
376 {
377 Destination.min = this.min;
378 Destination.max = this.max;
379 Destination.point = this.point;
380 Destination.minInclusive = this.minInclusive;
381 Destination.maxInclusive = this.maxInclusive;
382 Destination.isRange = this.isRange;
383 Destination.isPoint = this.isPoint;
384 }
385
390 public IEnumerator<Filter> GetEnumerator()
391 {
392 if (this.filters is null)
393 return (IEnumerator<Filter>)Array.Empty<Filter>().GetEnumerator();
394 else
395 return this.filters.GetEnumerator();
396 }
397
402 IEnumerator IEnumerable.GetEnumerator()
403 {
404 return this.GetEnumerator();
405 }
406
408 public override string ToString()
409 {
410 StringBuilder sb = new StringBuilder();
411
412 if (this.isPoint)
413 {
414 sb.Append(this.fieldName);
415 sb.Append('=');
416 sb.Append(this.point?.ToString());
417 }
418 else if (this.isRange)
419 {
420 sb.Append(this.min?.ToString());
421
422 if (this.minInclusive)
423 sb.Append('≤');
424 else
425 sb.Append('<');
426
427 sb.Append(this.fieldName);
428
429 if (this.maxInclusive)
430 sb.Append('≤');
431 else
432 sb.Append('<');
433
434 sb.Append(this.max?.ToString());
435 }
436 else
437 sb.Append(this.fieldName);
438
439 return sb.ToString();
440 }
441 }
442}
Static class that performs comparisons of property values.
Definition: Comparison.cs:11
static bool Decrement(ref object Value)
Decrements Value to the largest value smaller than Value .
Definition: Comparison.cs:720
static ? int Compare(object Value1, object Value2)
Compares two values. The values can be of different, but compatible types.
Definition: Comparison.cs:231
static bool Increment(ref object Value)
Increments Value to the smallest value greater than Value .
Definition: Comparison.cs:262
Contains information about a range in a search operation.
Definition: RangeInfo.cs:13
bool HasMin
If the range has a minimum endpoint.
Definition: RangeInfo.cs:97
RangeInfo(string FieldName)
Contains information about a range in a search operation.
Definition: RangeInfo.cs:28
bool HasFilters
If range has associated filters.
Definition: RangeInfo.cs:112
object Max
Maximum endpoint, if a range.
Definition: RangeInfo.cs:58
bool MinInclusive
If the minimum endpoint is included in the range.
Definition: RangeInfo.cs:68
bool SetMin(object Value, bool Inclusive, out bool Smaller)
Sets minimum endpoint of range.
Definition: RangeInfo.cs:182
bool SetMax(object Value, bool Inclusive, out bool Smaller)
Sets maximum endpoint of range.
Definition: RangeInfo.cs:276
IEnumerator< Filter > GetEnumerator()
Gets an enumerator of associated fields.
Definition: RangeInfo.cs:390
object Min
Minimum endpoint, if a range.
Definition: RangeInfo.cs:53
void CopyTo(RangeInfo Destination)
Copies the range information to another range object.
Definition: RangeInfo.cs:375
object Point
Point value, if not a range.
Definition: RangeInfo.cs:63
bool IsRange
If the object specifies a range (true) or a single point (false).
Definition: RangeInfo.cs:78
bool IsOpenEndedRange
If the range is open-ended.
Definition: RangeInfo.cs:89
RangeInfo Copy()
Creates a copy of the range information.
Definition: RangeInfo.cs:364
bool IsPoint
If the object specifies a point (true) or a range (false).
Definition: RangeInfo.cs:83
void AddFilterReference(Filter Filter)
Adds a reference to a filter.
Definition: RangeInfo.cs:118
bool HasMax
If the range has a maximum endpoint.
Definition: RangeInfo.cs:105
bool SetPoint(object Value)
Range consists of a single point value.
Definition: RangeInfo.cs:131
bool MaxInclusive
If the maximum endpoint is included in the range.
Definition: RangeInfo.cs:73
RangeInfo(string FieldName, LinkedList< Filter > Filters)
Contains information about a range in a search operation.
Definition: RangeInfo.cs:39
Base class for all filter classes.
Definition: Filter.cs:15