forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeEnvelope.cs
84 lines (74 loc) · 1.61 KB
/
TimeEnvelope.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
public class TimeEnvelope
{
private int _count;
private int _time = 0;
private bool _throwOnOverflow;
public static void AddTime(IEnumerable<TimeEnvelope> envelopes, int value)
{
foreach(var envelope in envelopes)
{
envelope.AddTime(value);
}
}
public TimeEnvelope(int count, bool throwOnOverthrow = true)
{
_count = count;
_throwOnOverflow = throwOnOverthrow;
}
public int Count
{
get
{
return _count;
}
}
public int Time
{
get
{
return _time;
}
}
public int AddTime(int value)
{
_time += value;
if (_time == _count)
{
_time = 0;
}
else if (_throwOnOverflow && _time > _count)
{
throw new Exception ("TimeEnvelope count overflowed!");
}
return _time;
}
public bool IsFirstMultiple(int value)
{
if (_time == value)
{
return true;
}
return false;
}
public bool IsLastMultiple(int value)
{
if (_time - value == 0)
{
return true;
}
return false;
}
public bool IsMultiple(int value)
{
if (_time % value == 0)
{
return true;
}
return false;
}
}