Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / Common / Range.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18
19 namespace Tizen.Multimedia
20 {
21     /// <summary>
22     /// Represents a range(min, max) value.
23     /// </summary>
24     public struct Range
25     {
26         /// <summary>
27         /// Initializes a new instance of the range with the specified values.
28         /// </summary>
29         /// <param name="min">Minimum value of the range.</param>
30         /// <param name="max">Maximum value of the range.</param>
31         /// <exception cref="ArgumentException"><paramref name="max"/> is less than <paramref name="min"/>.</exception>
32         public Range(int min, int max)
33         {
34             if (min > max)
35             {
36                 throw new ArgumentException($"min can't be greater than max.");
37             }
38             Min = min;
39             Max = max;
40         }
41
42         /// <summary>
43         /// Gets or sets the minimum value of the range.
44         /// </summary>
45         public int Min
46         {
47             get;
48             set;
49         }
50
51         /// <summary>
52         /// Gets or sets the maximum value of the range.
53         /// </summary>
54         public int Max
55         {
56             get;
57             set;
58         }
59
60         /// <summary>
61         /// Gets the length of the range.
62         /// </summary>
63         public int Length => Max - Min;
64
65         /// <summary>
66         /// Determines if the specified value is within the range.
67         /// </summary>
68         /// <param name="value">The value to check.</param>
69         /// <returns>true if the value is within the range; otherwise false.</returns>
70         public bool IsInside(int value)
71         {
72             return Min <= value && value <= Max;
73         }
74
75         /// <summary>
76         /// Returns a string that represents the current object.
77         /// </summary>
78         /// <returns>A string that represents the current object.</returns>
79         public override string ToString() => $"Min={Min.ToString()}, Max={Max.ToString()}";
80
81         /// <summary>
82         /// Gets the hash code for this instance of <see cref="Range"/>.
83         /// </summary>
84         /// <returns>The hash code for this instance of <see cref="Range"/>.</returns>
85         public override int GetHashCode()
86         {
87             return new { Min, Max }.GetHashCode();
88         }
89
90         /// <summary>
91         /// Compares an object to an instance of <see cref="Range"/> for equality.
92         /// </summary>
93         /// <param name="obj">A <see cref="Object"/> to compare.</param>
94         /// <returns>true if the two ranges are equal; otherwise, false.</returns>
95         public override bool Equals(object obj)
96         {
97             return obj is Range && this == (Range)obj;
98         }
99
100         /// <summary>
101         /// Compares two instances of <see cref="Range"/> for equality.
102         /// </summary>
103         /// <param name="range1">A <see cref="Range"/> to compare.</param>
104         /// <param name="range2">A <see cref="Range"/> to compare.</param>
105         /// <returns>true if the two instances of <see cref="Range"/> are equal; otherwise false.</returns>
106         public static bool operator ==(Range range1, Range range2)
107         {
108             return range1.Min == range2.Min && range1.Max == range2.Max;
109         }
110
111         /// <summary>
112         /// Compares two instances of <see cref="Range"/> for inequality.
113         /// </summary>
114         /// <param name="range1">A <see cref="Range"/> to compare.</param>
115         /// <param name="range2">A <see cref="Range"/> to compare.</param>
116         /// <returns>true if the two instances of <see cref="Range"/> are not equal; otherwise false.</returns>
117         public static bool operator !=(Range range1, Range range2)
118         {
119             return !(range1 == range2);
120         }
121     }
122 }
123