Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / LocalFileComparer.cs @ 2b0ec5b8

History | View | Annotate | Download (2.2 kB)

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