New tests.
[mono.git] / mcs / class / System.Xaml / System.Xaml.Schema / XamlValueConverter.cs
1 //
2 // Copyright (C) 2010 Novell Inc. http://novell.com
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 using System;
24 using System.Collections.Generic;
25
26 namespace System.Xaml.Schema
27 {
28         public class XamlValueConverter<TConverterBase> : IEquatable<XamlValueConverter<TConverterBase>>
29                 where TConverterBase : class
30         {
31                 public XamlValueConverter (Type converterType, XamlType targetType)
32                         : this (converterType, targetType, null)
33                 {
34                 }
35
36                 public XamlValueConverter (Type converterType, XamlType targetType, string name)
37                 {
38                         if (converterType == null && targetType == null && name == null)
39                                 throw new ArgumentException ("Either of converterType, targetType or name must be non-null");
40                         ConverterType = converterType;
41                         TargetType = targetType;
42                         Name = name;
43                 }
44
45                 TConverterBase converter_instance;
46                 public TConverterBase ConverterInstance {
47                         get {
48                                 if (converter_instance == null)
49                                         converter_instance = CreateInstance ();
50                                 return converter_instance;
51                         }
52                 }
53                 public Type ConverterType { get; private set; }
54                 public string Name { get; private set; }
55                 public XamlType TargetType { get; private set; }
56
57                 
58                 public static bool operator == (XamlValueConverter<TConverterBase> left, XamlValueConverter<TConverterBase> right)
59                 {
60                         return IsNull (left) ? IsNull (right) : left.Equals (right);
61                 }
62
63                 static bool IsNull (XamlValueConverter<TConverterBase> a)
64                 {
65                         return Object.ReferenceEquals (a, null);
66                 }
67
68                 public static bool operator != (XamlValueConverter<TConverterBase> left, XamlValueConverter<TConverterBase> right)
69                 {
70                         return IsNull (left) ? !IsNull (right) : IsNull (right) || left.ConverterType != right.ConverterType || left.TargetType != right.TargetType || left.Name != right.Name;
71                 }
72                 
73                 public bool Equals (XamlValueConverter<TConverterBase> other)
74                 {
75                         return !IsNull (other) && ConverterType == other.ConverterType && TargetType == other.TargetType && Name == other.Name;
76                 }
77
78                 public override bool Equals (object obj)
79                 {
80                         var a = obj as XamlValueConverter<TConverterBase>;
81                         return Equals (a);
82                 }
83
84                 protected virtual TConverterBase CreateInstance ()
85                 {
86                         if (ConverterType == null)
87                                 return null;
88
89                         if (!typeof (TConverterBase).IsAssignableFrom (ConverterType))
90                                 throw new XamlSchemaException (String.Format ("ConverterType '{0}' is not derived from '{1}' type", ConverterType, typeof (TConverterBase)));
91
92                         return (TConverterBase) Activator.CreateInstance (ConverterType);
93                 }
94
95                 public override int GetHashCode ()
96                 {
97                         var ret = ConverterType != null ? ConverterType.GetHashCode () : 0;
98                         ret <<= 5;
99                         if (TargetType != null)
100                                 ret += TargetType.GetHashCode ();
101                         ret <<= 5;
102                         if (Name != null)
103                                 ret += Name.GetHashCode ();
104                         return ret;
105                 }
106
107                 public override string ToString ()
108                 {
109                         if (Name != null)
110                                 return Name;
111                         if (ConverterType != null && TargetType != null)
112                                 return String.Concat (ConverterType.Name, "(", TargetType.Name, ")");
113                         else if (ConverterType != null)
114                                 return ConverterType.Name;
115                         else
116                                 return TargetType.Name;
117                 }
118         }
119 }