using System; using System.Collections.Generic; using Pithos.Core.Agents; namespace Pithos.Core { public class LocalFileComparer:EqualityComparer { public override bool Equals(CloudAction x, CloudAction y) { if (x.Action != y.Action) return false; if (x.LocalFile != null && y.LocalFile != null && !x.LocalFile.FullName.Equals(y.LocalFile.FullName)) return false; if (x.CloudFile != null && y.CloudFile != null ) { if (x.CloudFile.X_Object_Hash == null & y.CloudFile.X_Object_Hash != null) return false; if (x.CloudFile.X_Object_Hash != null & y.CloudFile.X_Object_Hash == null) return false; if (x.CloudFile.X_Object_Hash == null & y.CloudFile.X_Object_Hash == null) return (x.CloudFile.Name == y.CloudFile.Name); if (!x.CloudFile.X_Object_Hash.Equals(y.CloudFile.X_Object_Hash)) return false; //All directories have the same hash. Compare them using their names instead if (x.CloudFile.Content_Type == y.CloudFile.Content_Type && x.CloudFile.IsDirectory) { return (x.CloudFile.Name == y.CloudFile.Name); } } if (x.CloudFile == null ^ y.CloudFile == null || x.LocalFile == null ^ y.LocalFile == null) return false; return true; } public override int GetHashCode(CloudAction obj) { if (obj == null) return 0; var hash1 = (obj.LocalFile == null) ? Int32.MaxValue : obj.LocalFile.FullName.GetHashCode(); var hash2 = Int32.MaxValue; if (obj.CloudFile != null) { //All directories have the same hash code. Use their name's hash code instead hash2 = obj.CloudFile.IsDirectory ? obj.CloudFile.Name.GetHashCode() : (obj.CloudFile.X_Object_Hash!=null) ? obj.CloudFile.X_Object_Hash.GetHashCode() : obj.CloudFile.Name.GetHashCode(); } var hash3 = obj.Action.GetHashCode(); return hash1 ^ hash2 & hash3; } } }