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