5a13de8979080ca283b954d4f8ec32ab2387f7a8
[pithos-ms-client] / trunk / Pithos.Core / LocalFileComparer.cs
1 using System;
2 using System.Collections.Generic;
3 using Pithos.Core.Agents;
4
5 namespace Pithos.Core
6 {
7     public class LocalFileComparer:EqualityComparer<CloudAction>
8     {
9         public override bool Equals(CloudAction x, CloudAction y)
10         {
11             if (x.Action != y.Action)
12                 return false;
13             if (x.LocalFile != null && y.LocalFile != null && !x.LocalFile.FullName.Equals(y.LocalFile.FullName))
14                 return false;
15             if (x.CloudFile != null && y.CloudFile != null )
16             {
17                 if (x.CloudFile.Hash == null & y.CloudFile.Hash != null)
18                     return false;
19                 if (x.CloudFile.Hash != null & y.CloudFile.Hash == null)
20                     return false;
21                 if (x.CloudFile.Hash == null & y.CloudFile.Hash == null)
22                     return (x.CloudFile.Name == y.CloudFile.Name);
23                     
24                 if (!x.CloudFile.Hash.Equals(y.CloudFile.Hash))
25                     return false;
26                 //All directories have the same hash. Compare them using their names instead
27                 if (x.CloudFile.Content_Type == y.CloudFile.Content_Type && x.CloudFile.Content_Type == "application/directory")
28                 {
29                     return (x.CloudFile.Name == y.CloudFile.Name);
30                 }
31             }
32             if (x.CloudFile == null ^ y.CloudFile == null ||
33                 x.LocalFile == null ^ y.LocalFile == null)
34                 return false;
35             return true;
36         }
37
38         public override int GetHashCode(CloudAction obj)
39         {
40             if (obj == null)
41                 return 0;
42             var hash1 = (obj.LocalFile == null) ? Int32.MaxValue : obj.LocalFile.FullName.GetHashCode();
43             var hash2 = Int32.MaxValue;
44             if (obj.CloudFile != null)
45             {
46                 //All directories have the same hash code. Use their name's hash code instead
47                 hash2 = obj.CloudFile.Content_Type == "application/directory" 
48                             ? obj.CloudFile.Name.GetHashCode() 
49                             : (obj.CloudFile.Hash ?? obj.CloudFile.Name ?? "").GetHashCode();
50             }
51             var hash3 = obj.Action.GetHashCode();
52             return hash1 ^ hash2 & hash3;
53         }
54     }
55 }