Skip to content

Commit b7d9e1f

Browse files
committed
Fix bug with Time Equality comparison
1 parent a369b5d commit b7d9e1f

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/Nest/CommonOptions/TimeUnit/Time.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ public int CompareTo(Time other)
7979
public static bool operator >=(Time left, Time right) => left.CompareTo(right) > 0 || left.Equals(right);
8080

8181
public static bool operator ==(Time left, Time right) =>
82-
object.ReferenceEquals(left, null) ? object.ReferenceEquals(right, null) : left.Equals(right);
82+
ReferenceEquals(left, null) ? ReferenceEquals(right, null) : left.Equals(right);
8383

84-
public static bool operator !=(Time left, Time right) =>
85-
!object.ReferenceEquals(left, null) && !object.ReferenceEquals(right, null) && !left.Equals(right);
84+
public static bool operator !=(Time left, Time right) => !(left == right);
8685

8786
public TimeSpan ToTimeSpan() => TimeSpan.FromMilliseconds(this.Milliseconds);
8887

@@ -96,7 +95,7 @@ public bool Equals(Time other)
9695
{
9796
if (ReferenceEquals(null, other)) return false;
9897
if (ReferenceEquals(this, other)) return true;
99-
return (this.ApproximateMilliseconds == other.ApproximateMilliseconds);
98+
return this.ApproximateMilliseconds == other.ApproximateMilliseconds;
10099
}
101100

102101
public override bool Equals(object obj)

src/Nest/CommonOptions/TimeUnit/TimeJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ internal class TimeJsonConverter : JsonConverter
77
{
88
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
99
{
10-
var v = value as Time;
10+
var v = (Time)value;
1111
if (v.Factor.HasValue && v.Interval.HasValue)
1212
writer.WriteValue(v.ToString());
1313
else writer.WriteValue((long)v.Milliseconds);
@@ -16,7 +16,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
1616
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
1717
{
1818
if (reader.TokenType == JsonToken.String)
19-
return new Time(reader.Value as string);
19+
return new Time((string)reader.Value);
2020

2121
if (reader.TokenType == JsonToken.Integer || reader.TokenType == JsonToken.Float)
2222
{

src/Tests/CommonOptions/TimeUnit/TimeUnits.doc.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public void EqualityAndComparable()
9191
oneAndHalfYear.Should().BeGreaterThan(twoWeeks);
9292
(oneAndHalfYear > twoWeeks).Should().BeTrue();
9393
(oneAndHalfYear >= twoWeeks).Should().BeTrue();
94-
(twoDays >= new Time("2d")).Should().BeTrue();
94+
95+
(twoDays != null).Should().BeTrue();
96+
(twoDays >= new Time("2d")).Should().BeTrue();
9597

9698
twoDays.Should().BeLessThan(twoWeeks);
9799
(twoDays < twoWeeks).Should().BeTrue();
@@ -101,9 +103,10 @@ public void EqualityAndComparable()
101103
/**
102104
* And assert equality
103105
*/
104-
twoDays.Should().Be(new Time("2d"));
105-
(twoDays == new Time("2d")).Should().BeTrue();
106+
twoDays.Should().Be(new Time("2d"));
107+
(twoDays == new Time("2d")).Should().BeTrue();
106108
(twoDays != new Time("2.1d")).Should().BeTrue();
109+
107110
(new Time("2.1d") == new Time(TimeSpan.FromDays(2.1))).Should().BeTrue();
108111
(new Time("1") == new Time(1)).Should().BeTrue();
109112
(new Time("-1") == new Time(-1)).Should().BeTrue();

0 commit comments

Comments
 (0)