Merge pull request #823 from DavidKarlas/master
[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.Globalization;
32 using System.ComponentModel;
33
34 namespace System.ComponentModel.DataAnnotations
35 {
36 #if NET_4_0
37         [AttributeUsage (AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
38 #else
39         [AttributeUsage (AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
40 #endif
41         public class RangeAttribute : ValidationAttribute
42         {
43                 Func <object, bool> comparer;
44                 TypeConverter cvt;
45
46                 public object Maximum { get; private set; }
47                 public object Minimum { get; private set; }
48                 public Type OperandType { get; private set; }
49
50                 IComparable MaximumComparable {
51                         get { return Maximum as IComparable; }
52                 }
53
54                 IComparable MinimumComparable {
55                         get { return Minimum as IComparable; }
56                 }
57                 
58                 RangeAttribute ()
59                         : base (GetDefaultErrorMessage)
60                 {
61                 }
62                 
63                 public RangeAttribute (double minimum, double maximum) : this ()
64                 {
65                         Minimum = minimum;
66                         Maximum = maximum;
67                         OperandType = typeof (double);
68                 }
69
70                 public RangeAttribute (int minimum, int maximum) : this ()
71                 {
72                         Minimum = minimum;
73                         Maximum = maximum;
74                         OperandType = typeof (int);
75                 }
76
77                 public RangeAttribute (Type type, string minimum, string maximum) : this ()
78                 {
79 #if !NET_4_0
80                         if (type == null)
81                                 throw new ArgumentNullException ("type");
82 #endif
83                         OperandType = type;
84                         Minimum = minimum;
85                         Maximum = maximum;
86 #if !NET_4_0
87                         comparer = SetupComparer ();
88 #endif
89                 }
90
91                 static string GetDefaultErrorMessage ()
92                 {
93                         return "The field {0} must be between {1} and {2}.";
94                 }
95                 
96                 public override string FormatErrorMessage (string name)
97                 {
98                         if (comparer == null)
99                                 comparer = SetupComparer ();
100
101                         return String.Format (ErrorMessageString, name, Minimum, Maximum);
102                 }
103
104                 // LAMESPEC: does not throw ValidationException when value is out of range
105                 public override bool IsValid (object value)
106                 {
107                         if (comparer == null)
108                                 comparer = SetupComparer ();
109                         
110                         if (value == null)
111                                 return true;
112
113                         string s = value as string;
114                         if (s != null && s.Length == 0)
115                                 return true;
116                         
117                         try {
118                                 if (comparer != null)
119                                         return comparer (value);
120
121                                 return false;
122                         } catch (FormatException) {
123                                 return false;
124                         } catch (InvalidCastException) {
125                                 return false;
126                         }
127                 }
128
129                 Func <object, bool> SetupComparer ()
130                 {
131                         Type ot = OperandType;
132
133                         object min = Minimum, max = Maximum;
134 #if NET_4_0
135                         if (min == null || max == null)
136                                 throw new InvalidOperationException ("The minimum and maximum values must be set.");
137 #endif
138                         if (min is int)
139                                 return new Func <object, bool> (CompareInt);
140
141                         if (min is double)
142                                 return new Func <object, bool> (CompareDouble);
143                         
144                         if (ot == null)
145                                 throw new InvalidOperationException ("The OperandType must be set when strings are used for minimum and maximum values.");
146                         
147                         if (!typeof(IComparable).IsAssignableFrom (ot)) {
148 #if NET_4_0
149                                 string message = String.Format ("The type {0} must implement System.IComparable", ot.FullName);
150                                 throw new InvalidOperationException (message);
151 #else
152                                 throw new ArgumentException ("object");
153 #endif
154                         }
155                         
156                         string smin = min as string, smax = max as string;
157                         cvt = TypeDescriptor.GetConverter (ot);
158                         Minimum = cvt.ConvertFromString (smin);
159                         Maximum = cvt.ConvertFromString (smax);
160
161                         return new Func <object, bool> (CompareArbitrary);
162                 }
163
164                 bool CompareInt (object value)
165                 {
166                         int cv = Convert.ToInt32 (value);
167
168                         return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
169                 }
170
171                 bool CompareDouble (object value)
172                 {
173                         double cv = Convert.ToDouble (value);
174                         
175                         return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
176                 }
177
178                 bool CompareArbitrary (object value)
179                 {
180                         object cv;
181                         if (value != null && value.GetType () == OperandType)
182                                 cv = value;
183                         else if (cvt != null)
184                                 cv = cvt.ConvertFrom (value);
185                         else
186                                 cv = null;
187                         
188                         return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
189                 }
190         }
191 }