5 // Moonlight List (moonlight-list@lists.ximian.com)
7 // Copyright 2007, 2009 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 namespace System
.Windows
.Media
.Animation
{
30 public struct RepeatBehavior
: IFormattable
{
32 // note: Count is default for the (default) empty ctor, so it needs to have a 0 value
33 enum RepeatBehaviorKind
{
39 private RepeatBehaviorKind kind
;
42 private TimeSpan duration
;
44 private RepeatBehavior (RepeatBehaviorKind kind
, double count
, TimeSpan duration
)
48 this.duration
= duration
;
52 public RepeatBehavior (double count
)
54 if ((count
< 0) || Double
.IsNaN (count
) || Double
.IsInfinity (count
))
55 throw new ArgumentOutOfRangeException ("count");
57 kind
= RepeatBehaviorKind
.Count
;
59 duration
= TimeSpan
.Zero
;
63 public RepeatBehavior (TimeSpan duration
)
65 if (duration
.Ticks
< 0)
66 throw new ArgumentOutOfRangeException ("duration");
68 kind
= RepeatBehaviorKind
.TimeSpan
;
69 this.duration
= duration
;
76 if (kind
== RepeatBehaviorKind
.Count
)
78 throw new InvalidOperationException ("This RepeatBehavior does not contain a Count");
82 public TimeSpan Duration
{
84 if (kind
== RepeatBehaviorKind
.TimeSpan
)
86 throw new InvalidOperationException ("This RepeatBehavior does not contain a Duration");
90 public static RepeatBehavior Forever
{
91 get { return new RepeatBehavior (RepeatBehaviorKind.Forever, 0, TimeSpan.Zero); }
95 public bool HasCount
{
96 get { return kind == RepeatBehaviorKind.Count; }
99 public bool HasDuration
{
100 get { return kind == RepeatBehaviorKind.TimeSpan; }
103 public override bool Equals (object value)
105 if (!(value is RepeatBehavior
))
108 return Equals (this, (RepeatBehavior
) value);
111 public bool Equals (RepeatBehavior repeatBehavior
)
113 return Equals (this, repeatBehavior
);
116 public static bool Equals (RepeatBehavior repeatBehavior1
, RepeatBehavior repeatBehavior2
)
118 if (repeatBehavior1
.kind
!= repeatBehavior2
.kind
)
121 switch (repeatBehavior1
.kind
) {
122 case RepeatBehaviorKind
.Count
:
123 return repeatBehavior1
.count
== repeatBehavior2
.count
;
124 case RepeatBehaviorKind
.TimeSpan
:
125 return repeatBehavior1
.duration
== repeatBehavior2
.duration
;
126 case RepeatBehaviorKind
.Forever
:
133 public static bool operator == (RepeatBehavior repeatBehavior1
, RepeatBehavior repeatBehavior2
)
135 return Equals (repeatBehavior1
, repeatBehavior2
);
138 public static bool operator != (RepeatBehavior repeatBehavior1
, RepeatBehavior repeatBehavior2
)
140 return !Equals (repeatBehavior1
, repeatBehavior2
);
143 public override int GetHashCode ()
146 case RepeatBehaviorKind
.Count
:
147 return count
.GetHashCode ();
148 case RepeatBehaviorKind
.TimeSpan
:
149 return duration
.GetHashCode ();
150 case RepeatBehaviorKind
.Forever
:
151 return RepeatBehaviorKind
.Forever
.GetHashCode ();
153 return base.GetHashCode ();
157 public override string ToString ()
160 case RepeatBehaviorKind
.Forever
:
162 case RepeatBehaviorKind
.Count
:
163 return String
.Format ("{0}x", count
);
164 case RepeatBehaviorKind
.TimeSpan
:
165 return duration
.ToString ();
167 return base.ToString ();
171 public string ToString (IFormatProvider formatProvider
)
173 return (this as IFormattable
).ToString (null, formatProvider
);
176 string System
.IFormattable
.ToString (string format
, IFormatProvider formatProvider
)
178 if (kind
!= RepeatBehaviorKind
.Count
)
181 if (String
.IsNullOrEmpty (format
))
184 if (formatProvider
!= null) {
185 ICustomFormatter cp
= (ICustomFormatter
) formatProvider
.GetFormat (typeof (ICustomFormatter
));
187 return String
.Format ("{0}x", cp
.Format (format
, count
, formatProvider
));
191 return String
.Format ("{0}x", count
.ToString (format
, formatProvider
));