forked from KSemenenko/TimePeriodLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeBlock.cs
More file actions
462 lines (407 loc) · 13 KB
/
TimeBlock.cs
File metadata and controls
462 lines (407 loc) · 13 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// -- FILE ------------------------------------------------------------------
// name : TimeBlock.cs
// project : Itenso Time Period
// created : Jani Giannoudis - 2011.02.18
// language : C# 4.0
// environment: .NET 2.0
// copyright : (c) 2011-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
namespace Itenso.TimePeriod
{
// ------------------------------------------------------------------------
public class TimeBlock : ITimeBlock
{
// ----------------------------------------------------------------------
public static readonly TimeBlock Anytime = new TimeBlock( true );
// ----------------------------------------------------------------------
public TimeBlock() :
this( TimeSpec.MinPeriodDate, TimeSpec.MaxPeriodDate )
{
} // TimeBlock
// ----------------------------------------------------------------------
internal TimeBlock( bool isReadOnly = false ) :
this( TimeSpec.MinPeriodDate, TimeSpec.MaxPeriodDate, isReadOnly )
{
} // TimeBlock
// ----------------------------------------------------------------------
public TimeBlock( DateTime moment, bool isReadOnly = false ) :
this( moment, TimeSpec.MinPeriodDuration, isReadOnly )
{
} // TimeBlock
// ----------------------------------------------------------------------
public TimeBlock( DateTime start, DateTime end, bool isReadOnly = false )
{
if ( start <= end )
{
this.start = start;
this.end = end;
}
else
{
this.end = start;
this.start = end;
}
duration = this.end - this.start;
this.isReadOnly = isReadOnly;
} // TimeBlock
// ----------------------------------------------------------------------
public TimeBlock( DateTime start, TimeSpan duration, bool isReadOnly = false )
{
if ( duration < TimeSpec.MinPeriodDuration )
{
throw new ArgumentOutOfRangeException( "duration" );
}
this.start = start;
this.duration = duration;
end = start.Add( duration );
this.isReadOnly = isReadOnly;
} // TimeBlock
// ----------------------------------------------------------------------
public TimeBlock( TimeSpan duration, DateTime end, bool isReadOnly = false )
{
if ( duration < TimeSpec.MinPeriodDuration )
{
throw new ArgumentOutOfRangeException( "duration" );
}
this.end = end;
this.duration = duration;
start = end.Subtract( duration );
this.isReadOnly = isReadOnly;
} // TimeBlock
// ----------------------------------------------------------------------
public TimeBlock( ITimePeriod copy )
{
if ( copy == null )
{
throw new ArgumentNullException( "copy" );
}
start = copy.Start;
end = copy.End;
duration = copy.Duration;
isReadOnly = copy.IsReadOnly;
} // TimeBlock
// ----------------------------------------------------------------------
protected TimeBlock( ITimePeriod copy, bool isReadOnly )
{
if ( copy == null )
{
throw new ArgumentNullException( "copy" );
}
start = copy.Start;
end = copy.End;
duration = copy.Duration;
this.isReadOnly = isReadOnly;
} // TimeBlock
// ----------------------------------------------------------------------
public bool IsReadOnly
{
get { return isReadOnly; }
} // IsReadOnly
// ----------------------------------------------------------------------
public bool IsAnytime
{
get { return !HasStart && !HasEnd; }
} // IsAnytime
// ----------------------------------------------------------------------
public bool IsMoment
{
get { return start.Equals( end ); }
} // IsMoment
// ----------------------------------------------------------------------
public bool HasStart
{
get { return start != TimeSpec.MinPeriodDate; }
} // HasStart
// ----------------------------------------------------------------------
public DateTime Start
{
get { return start; }
set
{
CheckModification();
start = value;
end = start.Add( duration );
}
} // Start
// ----------------------------------------------------------------------
public bool HasEnd
{
get { return end != TimeSpec.MaxPeriodDate; }
} // HasEnd
// ----------------------------------------------------------------------
public DateTime End
{
get { return end; }
set
{
CheckModification();
end = value;
start = end.Subtract( duration );
}
} // End
// ----------------------------------------------------------------------
public TimeSpan Duration
{
get { return duration; }
set { DurationFromStart( value ); }
} // Duration
// ----------------------------------------------------------------------
public string DurationDescription
{
get { return TimeFormatter.Instance.GetDuration( Duration, DurationFormatType.Detailed ); }
} // DurationDescription
// ----------------------------------------------------------------------
public virtual TimeSpan GetDuration( IDurationProvider provider )
{
if ( provider == null )
{
throw new ArgumentNullException( "provider" );
}
return provider.GetDuration( Start, End );
} // GetDuration
// ----------------------------------------------------------------------
public virtual void Setup( DateTime newStart, DateTime newEnd )
{
CheckModification();
if ( newStart <= newEnd )
{
start = newStart;
end = newEnd;
}
else
{
end = newStart;
start = newEnd;
}
duration = end - start;
} // Setup
// ----------------------------------------------------------------------
public virtual void Setup( DateTime newStart, TimeSpan newDuration )
{
CheckModification();
if ( newDuration < TimeSpec.MinPeriodDuration )
{
throw new ArgumentOutOfRangeException( "newDuration" );
}
start = newStart;
duration = newDuration;
end = start.Add( duration );
} // Setup
// ----------------------------------------------------------------------
public ITimeBlock Copy()
{
return Copy( TimeSpan.Zero );
} // Copy
// ----------------------------------------------------------------------
public virtual ITimeBlock Copy( TimeSpan offset )
{
return new TimeBlock( start.Add( offset ), end.Add( offset ), IsReadOnly );
} // Copy
// ----------------------------------------------------------------------
public virtual void Move( TimeSpan offset )
{
CheckModification();
if ( offset == TimeSpan.Zero )
{
return;
}
start = start.Add( offset );
end = end.Add( offset );
} // Move
// ----------------------------------------------------------------------
public ITimeBlock GetPreviousPeriod()
{
return GetPreviousPeriod( TimeSpan.Zero );
} // GetPreviousPeriod
// ----------------------------------------------------------------------
public virtual ITimeBlock GetPreviousPeriod( TimeSpan offset )
{
return new TimeBlock( Duration, Start.Add( offset ), IsReadOnly );
} // GetPreviousPeriod
// ----------------------------------------------------------------------
public ITimeBlock GetNextPeriod()
{
return GetNextPeriod( TimeSpan.Zero );
} // GetNextPeriod
// ----------------------------------------------------------------------
public virtual ITimeBlock GetNextPeriod( TimeSpan offset )
{
return new TimeBlock( End.Add( offset ), Duration, IsReadOnly );
} // GetNextPeriod
// ----------------------------------------------------------------------
public virtual void DurationFromStart( TimeSpan newDuration )
{
if ( newDuration < TimeSpec.MinPeriodDuration )
{
throw new ArgumentOutOfRangeException( "newDuration" );
}
CheckModification();
duration = newDuration;
end = start.Add( newDuration );
} // DurationFromStart
// ----------------------------------------------------------------------
public virtual void DurationFromEnd( TimeSpan newDuration )
{
if ( newDuration < TimeSpec.MinPeriodDuration )
{
throw new ArgumentOutOfRangeException( "newDuration" );
}
CheckModification();
duration = newDuration;
start = end.Subtract( newDuration );
} // DurationFromEnd
// ----------------------------------------------------------------------
public virtual bool IsSamePeriod( ITimePeriod test )
{
if ( test == null )
{
throw new ArgumentNullException( "test" );
}
return start == test.Start && end == test.End;
} // IsSamePeriod
// ----------------------------------------------------------------------
public virtual bool HasInside( DateTime test )
{
return TimePeriodCalc.HasInside( this, test );
} // HasInside
// ----------------------------------------------------------------------
public virtual bool HasInside( ITimePeriod test )
{
if ( test == null )
{
throw new ArgumentNullException( "test" );
}
return TimePeriodCalc.HasInside( this, test );
} // HasInside
// ----------------------------------------------------------------------
public virtual bool IntersectsWith( ITimePeriod test )
{
if ( test == null )
{
throw new ArgumentNullException( "test" );
}
return TimePeriodCalc.IntersectsWith( this, test );
} // IntersectsWith
// ----------------------------------------------------------------------
public virtual ITimeBlock GetIntersection( ITimePeriod period )
{
if ( period == null )
{
throw new ArgumentNullException( "period" );
}
if ( !IntersectsWith( period ) )
{
return null;
}
DateTime periodStart = period.Start;
DateTime periodEnd = period.End;
return new TimeBlock(
periodStart > start ? periodStart : start,
periodEnd < end ? periodEnd : end,
IsReadOnly );
} // GetIntersection
// ----------------------------------------------------------------------
public virtual bool OverlapsWith( ITimePeriod test )
{
if ( test == null )
{
throw new ArgumentNullException( "test" );
}
return TimePeriodCalc.OverlapsWith( this, test );
} // OverlapsWith
// ----------------------------------------------------------------------
public virtual PeriodRelation GetRelation( ITimePeriod test )
{
if ( test == null )
{
throw new ArgumentNullException( "test" );
}
return TimePeriodCalc.GetRelation( this, test );
} // GetRelation
// ----------------------------------------------------------------------
public virtual int CompareTo( ITimePeriod other, ITimePeriodComparer comparer )
{
if ( other == null )
{
throw new ArgumentNullException( "other" );
}
if ( comparer == null )
{
throw new ArgumentNullException( "comparer" );
}
return comparer.Compare( this, other );
} // CompareTo
// ----------------------------------------------------------------------
public virtual void Reset()
{
CheckModification();
start = TimeSpec.MinPeriodDate;
duration = TimeSpec.MaxPeriodDuration;
end = TimeSpec.MaxPeriodDate;
} // Reset
// ----------------------------------------------------------------------
public string GetDescription( ITimeFormatter formatter = null )
{
return Format( formatter ?? TimeFormatter.Instance );
} // GetDescription
// ----------------------------------------------------------------------
protected virtual string Format( ITimeFormatter formatter )
{
return formatter.GetPeriod( start, end, duration );
} // Format
// ----------------------------------------------------------------------
public override string ToString()
{
return GetDescription();
} // ToString
// ----------------------------------------------------------------------
public sealed override bool Equals( object obj )
{
if ( obj == this )
{
return true;
}
if ( obj == null || GetType() != obj.GetType() )
{
return false;
}
return IsEqual( obj );
} // Equals
// ----------------------------------------------------------------------
protected virtual bool IsEqual( object obj )
{
return HasSameData( obj as TimeBlock );
} // IsEqual
// ----------------------------------------------------------------------
private bool HasSameData( TimeBlock comp )
{
return start == comp.start && end == comp.end && isReadOnly == comp.isReadOnly;
} // HasSameData
// ----------------------------------------------------------------------
public sealed override int GetHashCode()
{
return HashTool.AddHashCode( GetType().GetHashCode(), ComputeHashCode() );
} // GetHashCode
// ----------------------------------------------------------------------
protected virtual int ComputeHashCode()
{
return HashTool.ComputeHashCode( isReadOnly, start, end, duration );
} // ComputeHashCode
// ----------------------------------------------------------------------
protected void CheckModification()
{
if ( IsReadOnly )
{
throw new NotSupportedException( "TimeBlock is read-only" );
}
} // CheckModification
// ----------------------------------------------------------------------
// members
private readonly bool isReadOnly;
private DateTime start;
private TimeSpan duration;
private DateTime end; // cache
} // class TimeBlock
} // namespace Itenso.TimePeriod
// -- EOF -------------------------------------------------------------------