// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
namespace EpicGames.Perforce
{
///
/// Wrapper for a list of filespecs. Allows implicit conversion from string (a single entry) or list.
///
public readonly struct FileSpecList : IEquatable
{
///
/// Empty filespec list
///
public static FileSpecList Empty { get; } = new FileSpecList(new List());
///
/// Matches any files in the depot
///
public static FileSpecList Any { get; } = new FileSpecList(new List { "//..." });
///
/// The list of filespecs
///
public IReadOnlyList List { get; }
///
/// Private constructor. Use implicit conversion operators below instead.
///
/// List of filespecs
private FileSpecList(IReadOnlyList fileSpecList)
{
List = fileSpecList;
}
///
public override bool Equals(object? obj) => obj is FileSpecList other && Equals(other);
///
public override int GetHashCode()
{
HashCode code = new HashCode();
foreach (string item in List)
{
code.Add(item);
}
return code.ToHashCode();
}
///
public bool Equals(FileSpecList other) => List.SequenceEqual(other.List, StringComparer.Ordinal);
///
/// Test two filespecs for equality
///
public static bool operator ==(FileSpecList a, FileSpecList b) => a.Equals(b);
///
/// Test two filespecs for inequality
///
public static bool operator !=(FileSpecList a, FileSpecList b) => !a.Equals(b);
///
/// Implicit conversion operator from a list of filespecs
///
/// The list to construct from
public static implicit operator FileSpecList(List list)
{
return new FileSpecList(list);
}
///
/// Implicit conversion operator from an array of filespecs
///
/// The array to construct from
public static implicit operator FileSpecList(string[] array)
{
return new FileSpecList(array);
}
///
/// Implicit conversion operator from a single filespec
///
/// The single filespec to construct from
public static implicit operator FileSpecList(string fileSpec)
{
return new FileSpecList(new string[] { fileSpec });
}
///
public override string ToString()
{
if (List.Count == 0)
{
return "(none)";
}
else
{
return String.Join(", ", List);
}
}
}
}