1
1
using System ;
2
+ using Newtonsoft . Json ;
3
+ using Octopus . TinyTypes ;
2
4
3
5
namespace Octopus . Client . Model
4
6
{
5
7
public class RetentionPeriod : IEquatable < RetentionPeriod >
6
8
{
7
- readonly int quantityToKeep ;
8
- readonly RetentionUnit unit ;
9
-
10
- public RetentionPeriod ( int quantityToKeep , RetentionUnit unit )
9
+ [ JsonConstructor ]
10
+ public RetentionPeriod ( RetentionPeriodStrategy strategy , int quantityToKeep , RetentionUnit unit )
11
11
{
12
- this . quantityToKeep = quantityToKeep ;
13
- this . unit = unit ;
12
+ Strategy = strategy ?? SelectStrategyBasedOnSettings ( quantityToKeep , unit ) ;
13
+ QuantityToKeep = quantityToKeep ;
14
+ Unit = unit ;
14
15
}
15
16
16
- public RetentionUnit Unit
17
- {
18
- get { return unit ; }
19
- }
17
+ public RetentionPeriod ( int quantityToKeep , RetentionUnit unit ) : this ( null , quantityToKeep , unit )
18
+ { }
20
19
21
- public int QuantityToKeep
22
- {
23
- get { return quantityToKeep ; }
24
- }
20
+ public RetentionPeriodStrategy Strategy { get ; protected set ; }
25
21
26
- public bool ShouldKeepForever
27
- {
28
- get { return QuantityToKeep == 0 ; }
29
- }
22
+ public RetentionUnit Unit { get ; protected set ; }
23
+
24
+ public int QuantityToKeep { get ; protected set ; }
25
+
26
+ public bool ShouldKeepForever => QuantityToKeep == 0 ;
27
+
28
+ public static RetentionPeriod Default ( ) => new ( RetentionPeriodStrategy . Default , 0 , RetentionUnit . Items ) ;
30
29
31
- public static RetentionPeriod KeepForever ( )
30
+ RetentionPeriodStrategy SelectStrategyBasedOnSettings ( int quantityToKeep , RetentionUnit unit )
32
31
{
33
- return new RetentionPeriod ( 0 , RetentionUnit . Items ) ;
32
+ return quantityToKeep == 0 && unit == RetentionUnit . Items
33
+ ? Strategy = RetentionPeriodStrategy . Default
34
+ : Strategy = RetentionPeriodStrategy . Count ;
34
35
}
35
36
36
37
public bool Equals ( RetentionPeriod other )
37
38
{
38
39
if ( ReferenceEquals ( null , other ) ) return false ;
39
40
if ( ReferenceEquals ( this , other ) ) return true ;
40
- return quantityToKeep == other . quantityToKeep && unit . Equals ( other . unit ) ;
41
+ return Strategy . Equals ( other . Strategy ) && QuantityToKeep == other . QuantityToKeep && Unit . Equals ( other . Unit ) ;
41
42
}
42
43
43
44
public override bool Equals ( object obj )
@@ -52,23 +53,24 @@ public override int GetHashCode()
52
53
{
53
54
unchecked
54
55
{
55
- return ( quantityToKeep * 397 ) ^ unit . GetHashCode ( ) ;
56
+ var hashCode = Strategy . GetHashCode ( ) ;
57
+ hashCode = ( hashCode * 397 ) ^ ( int ) Unit ;
58
+ hashCode = ( hashCode * 397 ) ^ QuantityToKeep ;
59
+ return hashCode ;
56
60
}
57
61
}
58
62
59
- public static bool operator == ( RetentionPeriod left , RetentionPeriod right )
60
- {
61
- return Equals ( left , right ) ;
62
- }
63
+ public static bool operator == ( RetentionPeriod left , RetentionPeriod right ) => Equals ( left , right ) ;
63
64
64
- public static bool operator != ( RetentionPeriod left , RetentionPeriod right )
65
- {
66
- return ! Equals ( left , right ) ;
67
- }
65
+ public static bool operator != ( RetentionPeriod left , RetentionPeriod right ) => ! Equals ( left , right ) ;
68
66
69
- public override string ToString ( )
70
- {
71
- return ShouldKeepForever ? "Forever" : "Last " + quantityToKeep + " " + ( unit == RetentionUnit . Days ? "day" + ( quantityToKeep == 1 ? "" : "s" ) : "item" + ( quantityToKeep == 1 ? "" : "s" ) ) ;
72
- }
67
+ public override string ToString ( ) => ShouldKeepForever ? "Forever" : "Last " + QuantityToKeep + " " + ( Unit == RetentionUnit . Days ? "day" + ( QuantityToKeep == 1 ? "" : "s" ) : "item" + ( QuantityToKeep == 1 ? "" : "s" ) ) ;
68
+
69
+ }
70
+
71
+ public class RetentionPeriodStrategy ( string value ) : CaseInsensitiveStringTinyType ( value )
72
+ {
73
+ public static readonly RetentionPeriodStrategy Default = new ( "Default" ) ;
74
+ public static readonly RetentionPeriodStrategy Count = new ( "Count" ) ;
73
75
}
74
76
}
0 commit comments