Skip to content

Commit

Permalink
Implement IEquatable<> and IComparable<> for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Jul 9, 2024
1 parent 27d9a6b commit d82eebc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
27 changes: 26 additions & 1 deletion src/Spectre.IO/DirectoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Spectre.IO;
/// <summary>
/// Represents a directory path.
/// </summary>
public sealed class DirectoryPath : Path
public sealed class DirectoryPath : Path, IEquatable<DirectoryPath>, IComparable<DirectoryPath>
{
/// <summary>
/// Gets a value indicating whether or not the current
Expand Down Expand Up @@ -240,4 +240,29 @@ public FilePath GetRelativePath(FilePath to)

return GetRelativePath(to.GetDirectory()).GetFilePath(to.GetFilename());
}

/// <inheritdoc />
public int CompareTo(DirectoryPath? other)
{
return PathComparer.Default.Compare(this, other);
}

/// <inheritdoc />
public bool Equals(DirectoryPath? other)
{
return PathComparer.Default.Equals(this, other);
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj)
|| (obj is DirectoryPath other && Equals(other));
}

/// <inheritdoc />
public override int GetHashCode()
{
return PathComparer.Default.GetHashCode(this);
}
}
27 changes: 26 additions & 1 deletion src/Spectre.IO/FilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Spectre.IO;
/// <summary>
/// Represents a file path.
/// </summary>
public sealed class FilePath : Path
public sealed class FilePath : Path, IEquatable<FilePath>, IComparable<FilePath>
{
/// <summary>
/// Gets a value indicating whether this path has a file extension.
Expand Down Expand Up @@ -240,4 +240,29 @@ public FilePath GetRelativePath(FilePath to)
{
return GetDirectory().GetRelativePath(to);
}

/// <inheritdoc />
public int CompareTo(FilePath? other)
{
return PathComparer.Default.Compare(this, other);
}

/// <inheritdoc />
public bool Equals(FilePath? other)
{
return PathComparer.Default.Equals(this, other);
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj)
|| (obj is FilePath other && Equals(other));
}

/// <inheritdoc />
public override int GetHashCode()
{
return PathComparer.Default.GetHashCode(this);
}
}
41 changes: 40 additions & 1 deletion src/Spectre.IO/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Spectre.IO;
/// Provides properties and instance methods for working with paths.
/// This class must be inherited.
/// </summary>
public abstract class Path
public abstract class Path : IEquatable<Path>, IComparable<Path>
{
private readonly string[] _segments;

Expand Down Expand Up @@ -135,4 +135,43 @@ public override string ToString()
{
return FullPath;
}

/// <inheritdoc />
public int CompareTo(Path? other)
{
return PathComparer.Default.Compare(this, other);
}

/// <inheritdoc />
public bool Equals(Path? other)
{
return PathComparer.Default.Equals(this, other);
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return Equals((Path)obj);
}

/// <inheritdoc />
public override int GetHashCode()
{
return PathComparer.Default.GetHashCode(this);
}
}
12 changes: 11 additions & 1 deletion src/Spectre.IO/PathComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int Compare(Path? x, Path? y)
}

// This might look strange,
// but for some reason, null reference tracking
// but for some reason, nullable reference tracking
// does not work correctly otherwise.
if (x == null || y == null)
{
Expand All @@ -67,6 +67,11 @@ public int Compare(Path? x, Path? y)
return 1;
}

if (x.GetType() != y.GetType())
{
return -1;
}

if (x.Segments.Count != y.Segments.Count)
{
return x.Segments.Count
Expand Down Expand Up @@ -102,6 +107,11 @@ public bool Equals(Path? x, Path? y)
return false;
}

if (x.GetType() != y.GetType())
{
return false;
}

if (x.Segments.Count != y.Segments.Count)
{
return false;
Expand Down

0 comments on commit d82eebc

Please sign in to comment.