System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / SwitchCase.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Apache License, Version 2.0, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Apache License, Version 2.0.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15
16 using System;
17 using System.Collections.Generic;
18 using System.Collections.ObjectModel;
19 using System.Diagnostics;
20 using System.Dynamic.Utils;
21
22 #if CLR2
23 namespace Microsoft.Scripting.Ast {
24 #else
25 namespace System.Linq.Expressions {
26 #endif
27     /// <summary>
28     /// Represents one case of a <see cref="SwitchExpression"/>.
29     /// </summary>
30 #if !SILVERLIGHT
31     [DebuggerTypeProxy(typeof(Expression.SwitchCaseProxy))]
32 #endif
33     public sealed class SwitchCase {
34         private readonly ReadOnlyCollection<Expression> _testValues;
35         private readonly Expression _body;
36
37         internal SwitchCase(Expression body, ReadOnlyCollection<Expression> testValues) {
38             _body = body;
39             _testValues = testValues;
40         }
41
42         /// <summary>
43         /// Gets the values of this case. This case is selected for execution when the <see cref="SwitchExpression.SwitchValue"/> matches any of these values.
44         /// </summary>
45         public ReadOnlyCollection<Expression> TestValues {
46             get { return _testValues; }
47         }
48
49         /// <summary>
50         /// Gets the body of this case.
51         /// </summary>
52         public Expression Body {
53             get { return _body; }
54         }
55
56         /// <summary>
57         /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>. 
58         /// </summary>
59         /// <returns>A <see cref="String"/> that represents the current <see cref="Object"/>. </returns>
60         public override string ToString() {
61             return ExpressionStringBuilder.SwitchCaseToString(this);
62         }
63
64         /// <summary>
65         /// Creates a new expression that is like this one, but using the
66         /// supplied children. If all of the children are the same, it will
67         /// return this expression.
68         /// </summary>
69         /// <param name="testValues">The <see cref="TestValues" /> property of the result.</param>
70         /// <param name="body">The <see cref="Body" /> property of the result.</param>
71         /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
72         public SwitchCase Update(IEnumerable<Expression> testValues, Expression body) {
73             if (testValues == TestValues && body == Body) {
74                 return this;
75             }
76             return Expression.SwitchCase(body, testValues);
77         }
78     }
79
80     public partial class Expression {
81         /// <summary>
82         /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
83         /// </summary>
84         /// <param name="body">The body of the case.</param>
85         /// <param name="testValues">The test values of the case.</param>
86         /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
87         public static SwitchCase SwitchCase(Expression body, params Expression[] testValues) {
88             return SwitchCase(body, (IEnumerable<Expression>)testValues);
89         }
90
91         /// <summary>
92         /// Creates a <see cref="T:SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
93         /// </summary>
94         /// <param name="body">The body of the case.</param>
95         /// <param name="testValues">The test values of the case.</param>
96         /// <returns>The created <see cref="T:SwitchCase">SwitchCase</see>.</returns>
97         public static SwitchCase SwitchCase(Expression body, IEnumerable<Expression> testValues) {
98             RequiresCanRead(body, "body");
99             
100             var values = testValues.ToReadOnly();
101             RequiresCanRead(values, "testValues");
102             ContractUtils.RequiresNotEmpty(values, "testValues");
103
104             return new SwitchCase(body, values);
105         }
106     }
107 }