forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeEnvelope.cs
72 lines (61 loc) · 1.37 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
internal 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 throwOnOverflow = true)
{
_count = count;
_throwOnOverflow = throwOnOverflow;
}
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)
{
return _time == value;
}
public bool IsLastMultiple(int value)
{
return _count - value == _time;
}
public bool IsMultiple(int value)
{
return _time % value == 0;
}
}