Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2085 path equality overrides #2089

Merged
merged 1 commit into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/kOS.Safe/Persistence/GlobalPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,34 @@ public override int GetHashCode()

public override bool Equals(object other)
{
Console.WriteLine("eraseme: GlobalPath.Equals(): does " + this.ToString() + " == " + other.ToString() + " ?");
GlobalPath otherPath = other as GlobalPath;

if (otherPath == null)
if (ReferenceEquals(otherPath, null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
{
return false;
}
bool result = VolumeId.Equals(otherPath.VolumeId) && Segments.SequenceEqual(otherPath.Segments);
Console.WriteLine("eraseme: Result = " + result);
return result;
}

public static bool operator ==(GlobalPath left, GlobalPath right)
{
if (ReferenceEquals(left,null) || ReferenceEquals(right,null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
return ReferenceEquals(left, null) && ReferenceEquals(right, null); // ReferenceEquals prevents infinite recursion with overloaded == operator.
return left.Equals(right);
}

return VolumeId.Equals(otherPath.VolumeId) && Segments.SequenceEqual(otherPath.Segments);
public static bool operator !=(GlobalPath left, GlobalPath right)
{
return !(left == right);
}

public override string ToString()
{
return VolumeId + ":" + base.ToString();
}

}
}
33 changes: 33 additions & 0 deletions src/kOS.Safe/Persistence/PathValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ public override string ToString()
{
return Path.ToString();
}

public override bool Equals(object other)
{
Console.WriteLine("eraseme: PathValue.Equals(): this = " + this.ToString() + ", other = " + other.ToString());
PathValue pVal = other as PathValue;
if (!ReferenceEquals(pVal,null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
return Path == pVal.Path;
GlobalPath gVal = other as GlobalPath;
if (!ReferenceEquals(gVal,null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
return Path == gVal;

// fallback:
return base.Equals(other);
}

public override int GetHashCode()
{
if (!ReferenceEquals(Path,null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
return Path.GetHashCode();
return base.GetHashCode();
}

public static bool operator ==(PathValue left, PathValue right)
{
if (ReferenceEquals(left,null) || ReferenceEquals(right,null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
return ReferenceEquals(left, null) && ReferenceEquals(right, null); // ReferenceEquals prevents infinite recursion with overloaded == operator.
return left.Equals(right);
}
public static bool operator !=(PathValue left, PathValue right)
{
return !(left == right);
}

}
}

15 changes: 14 additions & 1 deletion src/kOS.Safe/Persistence/VolumePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,27 @@ public override bool Equals(object other)
{
VolumePath otherPath = other as VolumePath;

if (otherPath == null)
if (ReferenceEquals(otherPath,null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
{
return false;
}

return Segments.SequenceEqual(otherPath.Segments);
}

public static bool operator ==(VolumePath left, VolumePath right)
{
if (ReferenceEquals(left,null) || ReferenceEquals(right,null)) // ReferenceEquals prevents infinite recursion with overloaded == operator.
return ReferenceEquals(left, null) && ReferenceEquals(right, null); // ReferenceEquals prevents infinite recursion with overloaded == operator.
return left.Equals(right);
}

public static bool operator !=(VolumePath left, VolumePath right)
{
return !(left == right);
}


public override string ToString()
{
return "/" + String.Join("/", Segments.ToArray());
Expand Down