Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / System.ComponentModel.DataAnnotations / RangeAttribute.cs
1 //
2 // RangeAttribute.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.ComponentModel;
32
33 namespace System.ComponentModel.DataAnnotations
34 {
35 #if NET_4_0
36         [AttributeUsage (AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
37 #else
38         [AttributeUsage (AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
39 #endif
40         public class RangeAttribute : ValidationAttribute
41         {
42                 Func <object, bool> comparer;
43                 TypeConverter cvt;
44
45                 public object Maximum { get; private set; }
46                 public object Minimum { get; private set; }
47                 public Type OperandType { get; private set; }
48
49                 IComparable MaximumComparable {
50                         get { return Maximum as IComparable; }
51                 }
52
53                 IComparable MinimumComparable {
54                         get { return Minimum as IComparable; }
55                 }
56                 
57                 RangeAttribute ()
58                         : base (GetDefaultErrorMessage)
59                 {
60                 }
61                 
62                 public RangeAttribute (double minimum, double maximum) : this ()
63                 {
64                         Minimum = minimum;
65                         Maximum = maximum;
66                         OperandType = typeof (double);
67                 }
68
69                 public RangeAttribute (int minimum, int maximum) : this ()
70                 {
71                         Minimum = minimum;
72                         Maximum = maximum;
73                         OperandType = typeof (int);
74                 }
75
76                 public RangeAttribute (Type type, string minimum, string maximum) : this ()
77                 {
78 #if !NET_4_0
79                         if (type == null)
80                                 throw new ArgumentNullException ("type");
81 #endif
82                         OperandType = type;
83                         Minimum = minimum;
84                         Maximum = maximum;
85 #if !NET_4_0
86                         comparer = SetupComparer ();
87 #endif
88                 }
89
90                 static string GetDefaultErrorMessage ()
91                 {
92                         return "The field {0} must be between {1} and {2}.";
93                 }
94                 
95                 public override string FormatErrorMessage (string name)
96                 {
97                         if (comparer == null)
98                                 comparer = SetupComparer ();
99
100                         return String.Format (ErrorMessageString, name, Minimum, Maximum);
101                 }
102
103                 // LAMESPEC: does not throw ValidationException when value is out of range
104                 public override bool IsValid (object value)
105                 {
106                         if (comparer == null)
107                                 comparer = SetupComparer ();
108                         
109                         if (value == null)
110                                 return true;
111
112                         string s = value as string;
113                         if (s != null && s.Length == 0)
114                                 return true;
115                         
116                         try {
117                                 if (comparer != null)
118                                         return comparer (value);
119
120                                 return false;
121                         } catch (FormatException) {
122                                 return false;
123                         } catch (InvalidCastException) {
124                                 return false;
125                         }
126                 }
127
128                 Func <object, bool> SetupComparer ()
129                 {
130                         Type ot = OperandType;
131
132                         object min = Minimum, max = Maximum;
133 #if NET_4_0
134                         if (min == null || max == null)
135                                 throw new InvalidOperationException ("The minimum and maximum values must be set.");
136 #endif
137                         if (min is int)
138                                 return new Func <object, bool> (CompareInt);
139
140                         if (min is double)
141                                 return new Func <object, bool> (CompareDouble);
142                         
143                         if (ot == null)
144                                 throw new InvalidOperationException ("The OperandType must be set when strings are used for minimum and maximum values.");
145                         
146                         if (!typeof(IComparable).IsAssignableFrom (ot)) {
147 #if NET_4_0
148                                 string message = String.Format ("The type {0} must implement System.IComparable", ot.FullName);
149                                 throw new InvalidOperationException (message);
150 #else
151                                 throw new ArgumentException ("object");
152 #endif
153                         }
154                         
155                         string smin = min as string, smax = max as string;
156                         cvt = TypeDescriptor.GetConverter (ot);
157                         Minimum = cvt.ConvertFromString (smin);
158                         Maximum = cvt.ConvertFromString (smax);
159
160                         return new Func <object, bool> (CompareArbitrary);
161                 }
162
163                 bool CompareInt (object value)
164                 {
165                         int cv = Convert.ToInt32 (value);
166
167                         return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
168                 }
169
170                 bool CompareDouble (object value)
171                 {
172                         double cv = Convert.ToDouble (value);
173                         
174                         return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
175                 }
176
177                 bool CompareArbitrary (object value)
178                 {
179                         object cv;
180                         if (value != null && value.GetType () == OperandType)
181                                 cv = value;
182                         else if (cvt != null)
183                                 cv = cvt.ConvertFrom (value);
184                         else
185                                 cv = null;
186                         
187                         return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
188                 }
189         }
190 }