11using System ;
2+ using Newtonsoft . Json ;
3+ using Octopus . TinyTypes ;
24
35namespace Octopus . Client . Model
46{
57 public class RetentionPeriod : IEquatable < RetentionPeriod >
68 {
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 )
1111 {
12- this . quantityToKeep = quantityToKeep ;
13- this . unit = unit ;
12+ Strategy = strategy ?? SelectStrategyBasedOnSettings ( quantityToKeep , unit ) ;
13+ QuantityToKeep = quantityToKeep ;
14+ Unit = unit ;
1415 }
1516
16- public RetentionUnit Unit
17- {
18- get { return unit ; }
19- }
17+ public RetentionPeriod ( int quantityToKeep , RetentionUnit unit ) : this ( null , quantityToKeep , unit )
18+ { }
2019
21- public int QuantityToKeep
22- {
23- get { return quantityToKeep ; }
24- }
20+ public RetentionPeriodStrategy Strategy { get ; protected set ; }
2521
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 ) ;
29+ public static RetentionPeriod KeepForever ( ) => new ( RetentionPeriodStrategy . Forever , 0 , RetentionUnit . Items ) ;
3030
31- public static RetentionPeriod KeepForever ( )
31+ RetentionPeriodStrategy SelectStrategyBasedOnSettings ( int quantityToKeep , RetentionUnit unit )
3232 {
33- return new RetentionPeriod ( 0 , RetentionUnit . Items ) ;
33+ return quantityToKeep == 0 && unit == RetentionUnit . Items
34+ ? Strategy = RetentionPeriodStrategy . Default
35+ : Strategy = RetentionPeriodStrategy . Count ;
3436 }
3537
3638 public bool Equals ( RetentionPeriod other )
3739 {
3840 if ( ReferenceEquals ( null , other ) ) return false ;
3941 if ( ReferenceEquals ( this , other ) ) return true ;
40- return quantityToKeep == other . quantityToKeep && unit . Equals ( other . unit ) ;
42+ return Strategy . Equals ( other . Strategy ) && QuantityToKeep == other . QuantityToKeep && Unit . Equals ( other . Unit ) ;
4143 }
4244
4345 public override bool Equals ( object obj )
@@ -52,23 +54,25 @@ public override int GetHashCode()
5254 {
5355 unchecked
5456 {
55- return ( quantityToKeep * 397 ) ^ unit . GetHashCode ( ) ;
57+ var hashCode = Strategy . GetHashCode ( ) ;
58+ hashCode = ( hashCode * 397 ) ^ ( int ) Unit ;
59+ hashCode = ( hashCode * 397 ) ^ QuantityToKeep ;
60+ return hashCode ;
5661 }
5762 }
5863
59- public static bool operator == ( RetentionPeriod left , RetentionPeriod right )
60- {
61- return Equals ( left , right ) ;
62- }
64+ public static bool operator == ( RetentionPeriod left , RetentionPeriod right ) => Equals ( left , right ) ;
6365
64- public static bool operator != ( RetentionPeriod left , RetentionPeriod right )
65- {
66- return ! Equals ( left , right ) ;
67- }
66+ public static bool operator != ( RetentionPeriod left , RetentionPeriod right ) => ! Equals ( left , right ) ;
6867
69- public override string ToString ( )
70- {
71- return ShouldKeepForever ? "Forever" : "Last " + quantityToKeep + " " + ( unit == RetentionUnit . Days ? "day" + ( quantityToKeep == 1 ? "" : "s" ) : "item" + ( quantityToKeep == 1 ? "" : "s" ) ) ;
72- }
68+ public override string ToString ( ) => ShouldKeepForever ? "Forever" : "Last " + QuantityToKeep + " " + ( Unit == RetentionUnit . Days ? "day" + ( QuantityToKeep == 1 ? "" : "s" ) : "item" + ( QuantityToKeep == 1 ? "" : "s" ) ) ;
69+
70+ }
71+
72+ public class RetentionPeriodStrategy ( string value ) : CaseInsensitiveStringTinyType ( value )
73+ {
74+ public static readonly RetentionPeriodStrategy Default = new ( "Default" ) ;
75+ public static readonly RetentionPeriodStrategy Count = new ( "Count" ) ;
76+ public static readonly RetentionPeriodStrategy Forever = new ( "Forever" ) ;
7377 }
7478}
0 commit comments