Subversion Repository Public Repository

WOX2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections;
using System.Reflection;

/// <header>
/// <wox>
///     <version>1.0</version>
///     <author>Carlos R. Jaimez Gonzalez</author>
///     <author>Simon M. Lucas</author>
///     <site>http://woxserializer.sourceforge.net/</site>
/// </wox>
/// <wox>
///     <version>1.5</version>
///     <author>Steven M Lewis</author>
/// </wox>
/// <wox>
///     <version>2.0</version>
///     <author>George A Smith</author>
///     <svn>http://woxserializer.sourceforge.net/</svn>
///     <note>XML form for vs 2 is more compact and therefor incompatible with vs 1</note>
/// </wox>
/// </header>

namespace wox.serial
{
    public class ComparisonUtilities
    {
        public static bool Equivalent( Object o1, Object o2 )
        {
            if ( o1 == o2 )
            {
                return true;
            }
            if ( o1 == null || o2 == null )
            {
                return false; // both null covered in first test
            }
            Type type = o1.GetType();
            if ( type != o2.GetType() )
            {
                return false;
            }
            if ( (o1 is ValueType) || (type == typeof (DateTime)) || type.IsEnum )
            {
                return o1.Equals( o2 );
            }
            // Array special case
            if ( type.IsArray )
            {
                return EquivalentArrayValues( (Array) o1, (Array) o2 );
            }
            // List special case
            if ( type.GetInterface( "IList`1" ) != null )
            {
                return EquivalentListValues( (IList) o1, (IList) o2 );
            }
            // Dictionary special case
            if ( type.GetInterface( "IDictionary`2" ) != null )
            {
                return EquivalentMapValues( (IDictionary) o1, (IDictionary) o2 );
            }

            return EquivalentFieldValues( o1, o2 );
        }

        public static bool EquivalentFieldValues( Object o1, Object o2 )
        {
            Type type = o1.GetType();
            FieldInfo[] fields = type.GetFields();
            for ( int i = 0; i < fields.Length; i++ )
            {
                FieldInfo info = fields[i];
                if ( !EquivalentFieldValue( info, o1, o2 ) )
                {
                    return false;
                }
            }
            return true;
        }

        public static bool EquivalentArrayValues( Array o1, Array o2 )
        {
            if ( o1 == o2 )
            {
                return true;
            }
            if ( o1 == null || o2 == null )
            {
                return false; // both null covered in first test
            }
            if ( o1.Length != o2.Length )
            {
                return false;
            }
            for ( int i = 0; i < o1.Length; i++ )
            {
                if ( !Equivalent( o1.GetValue( i ), o2.GetValue( i ) ) )
                {
                    return false;
                }
            }
            return true;
        }

        public static bool EquivalentListValues( IList o1, IList o2 )
        {
            if ( o1 == o2 )
            {
                return true;
            }
            if ( o1 == null || o2 == null )
            {
                return false; // both null covered in first test
            }
            if ( o1.Count != o2.Count )
            {
                return false;
            }
            for ( int i = 0; i < o1.Count; i++ )
            {
                if ( !Equivalent( o1[i], o2[i] ) )
                {
                    return false;
                }
            }
            return true;
        }

        public static bool EquivalentMapValues( IDictionary o1, IDictionary o2 )
        {
            if ( o1 == o2 )
            {
                return true;
            }
            if ( o1 == null || o2 == null )
            {
                return false; // both null covered in first test
            }
            if ( o1.Count != o2.Count )
            {
                return false;
            }
            foreach ( Object key in o1.Keys )
            {
                if ( !o2.Contains( key ) )
                {
                    return false;
                }
                object item1 = o1[key];
                object item2 = o2[key];
                if ( !Equivalent( item1, item2 ) )
                {
                    return false;
                }
            }
            return true;
        }

        protected static bool EquivalentFieldValue( FieldInfo info, Object o1, Object o2 )
        {
            if ( info.IsNotSerialized || info.IsStatic )
            {
                return true;
            }
            Object f1 = info.GetValue( o1 );
            Object f2 = info.GetValue( o2 );
            return Equivalent( f1, f2 );
        }
    }
}

Commits for WOX2/trunk/CSharp/src/wox/serial/ComparisonUtilities.cs

Diff revisions: vs.
Revision Author Commited Message
14 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 19:15:03 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000