| 1 | #Region "Microsoft.VisualBasic::e9e751fb0c36ed5fa7c7e67d5d93ff8b, Microsoft.VisualBasic.Core\Language\Value\Value.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Class Value |
| 35 | ' |
| 36 | ' Properties: HasValue, Value |
| 37 | ' |
| 38 | ' Constructor: (+2 Overloads) Sub New |
| 39 | ' Function: Equals, GetUnderlyingType, (+2 Overloads) GetValueOrDefault, IsNothing, ToString |
| 40 | ' Operators: -, (+3 Overloads) +, <=, <>, = |
| 41 | ' >= |
| 42 | ' Interface IValueOf |
| 43 | ' |
| 44 | ' Properties: Value |
| 45 | ' |
| 46 | ' |
| 47 | ' |
| 48 | ' |
| 49 | ' /********************************************************************************/ |
| 50 | |
| 51 | #End Region |
| 52 | |
| 53 | Imports System.Runtime.CompilerServices |
| 54 | Imports Microsoft.VisualBasic.Scripting |
| 55 | |
| 56 | Namespace Language |
| 57 | |
| 58 | ''' <summary> |
| 59 | ''' You can applying this data type into a dictionary object to makes the mathematics calculation more easily. |
| 60 | ''' </summary> |
| 61 | ''' <typeparam name="T"></typeparam> |
| 62 | Public Class Value(Of T) ': Implements ValueType |
| 63 | Implements IValueOf |
| 64 | |
| 65 | ''' <summary> |
| 66 | ''' This object have a <see cref="IValueOf.value"/> property for stores its data |
| 67 | ''' </summary> |
| 68 | Public Interface IValueOf |
| 69 | |
| 70 | ''' <summary> |
| 71 | ''' value property for this object stores its data |
| 72 | ''' </summary> |
| 73 | ''' <returns></returns> |
| 74 | Property Value As T |
| 75 | End Interface |
| 76 | |
| 77 | ''' <summary> |
| 78 | ''' Gets a value indicating whether the current System.Nullable`1 object has a valid |
| 79 | ''' value of its underlying type. |
| 80 | ''' </summary> |
| 81 | ''' <returns>true if the current System.Nullable`1 object has a value; false if the current |
| 82 | ''' System.Nullable`1 object has no value.</returns> |
| 83 | Public ReadOnly Property HasValue As Boolean |
| 84 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 85 | Get |
| 86 | Return Not Value Is Nothing |
| 87 | End Get |
| 88 | End Property |
| 89 | |
| 90 | ''' <summary> |
| 91 | ''' Retrieves the value of the current System.Nullable`1 object, or the object's |
| 92 | ''' default value. |
| 93 | ''' </summary> |
| 94 | ''' <returns>The value of the System.Nullable`1.Value property if the System.Nullable`1.HasValue |
| 95 | ''' property is true; otherwise, the default value of the current System.Nullable`1 |
| 96 | ''' object. The type of the default value is the type argument of the current System.Nullable`1 |
| 97 | ''' object, and the value of the default value consists solely of binary zeroes.</returns> |
| 98 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 99 | Public Function GetValueOrDefault() As T |
| 100 | Return GetValueOrDefault(Nothing) |
| 101 | End Function |
| 102 | |
| 103 | ''' <summary> |
| 104 | ''' Retrieves the value of the current System.Nullable`1 object, or the specified |
| 105 | ''' default value. |
| 106 | ''' </summary> |
| 107 | ''' <param name="defaultValue">A value to return if the System.Nullable`1.HasValue property is false.</param> |
| 108 | ''' <returns>The value of the System.Nullable`1.Value property if the System.Nullable`1.HasValue |
| 109 | ''' property is true; otherwise, the defaultValue parameter.</returns> |
| 110 | Public Function GetValueOrDefault(defaultValue As T) As T |
| 111 | If Value Is Nothing Then |
| 112 | Return defaultValue |
| 113 | Else |
| 114 | Return Value |
| 115 | End If |
| 116 | End Function |
| 117 | |
| 118 | ''' <summary> |
| 119 | ''' Indicates whether the current System.Nullable`1 object is equal to a specified |
| 120 | ''' object. |
| 121 | ''' </summary> |
| 122 | ''' <param name="other">An object.</param> |
| 123 | ''' <returns>true if the other parameter is equal to the current System.Nullable`1 object; |
| 124 | ''' otherwise, false. This table describes how equality is defined for the compared |
| 125 | ''' values: Return ValueDescriptiontrueThe System.Nullable`1.HasValue property is |
| 126 | ''' false, and the other parameter is null. That is, two null values are equal by |
| 127 | ''' definition.-or-The System.Nullable`1.HasValue property is true, and the value |
| 128 | ''' returned by the System.Nullable`1.Value property is equal to the other parameter.falseThe |
| 129 | ''' System.Nullable`1.HasValue property for the current System.Nullable`1 structure |
| 130 | ''' is true, and the other parameter is null.-or-The System.Nullable`1.HasValue property |
| 131 | ''' for the current System.Nullable`1 structure is false, and the other parameter |
| 132 | ''' is not null.-or-The System.Nullable`1.HasValue property for the current System.Nullable`1 |
| 133 | ''' structure is true, and the value returned by the System.Nullable`1.Value property |
| 134 | ''' is not equal to the other parameter.</returns> |
| 135 | Public Overrides Function Equals(other As Object) As Boolean |
| 136 | If other Is Nothing Then |
| 137 | Return False |
| 138 | ElseIf Not other.GetType Is GetType(T) Then |
| 139 | Return False |
| 140 | Else |
| 141 | Return Value.Equals(other) |
| 142 | End If |
| 143 | End Function |
| 144 | |
| 145 | ''' <summary> |
| 146 | ''' The object value with a specific type define. |
| 147 | ''' </summary> |
| 148 | ''' <returns></returns> |
| 149 | Public Overridable Property Value As T Implements IValueOf.Value |
| 150 | |
| 151 | ''' <summary> |
| 152 | ''' Creates an reference value object with the specific object value |
| 153 | ''' </summary> |
| 154 | ''' <param name="value"></param> |
| 155 | Sub New(value As T) |
| 156 | Me.Value = value |
| 157 | End Sub |
| 158 | |
| 159 | ''' <summary> |
| 160 | ''' Value is Nothing |
| 161 | ''' </summary> |
| 162 | Sub New() |
| 163 | Call MyBase.New |
| 164 | Value = Nothing |
| 165 | End Sub |
| 166 | |
| 167 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 168 | Public Function GetUnderlyingType() As Type |
| 169 | Return GetType(T) |
| 170 | End Function |
| 171 | |
| 172 | ''' <summary> |
| 173 | ''' Is the value is nothing. |
| 174 | ''' </summary> |
| 175 | ''' <returns></returns> |
| 176 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 177 | Public Function IsNothing() As Boolean |
| 178 | Return Value Is Nothing |
| 179 | End Function |
| 180 | |
| 181 | ''' <summary> |
| 182 | ''' Display <see cref="value"/> ``ToString()`` function value. |
| 183 | ''' </summary> |
| 184 | ''' <returns></returns> |
| 185 | Public Overrides Function ToString() As String |
| 186 | Return InputHandler.ToString(Value) |
| 187 | End Function |
| 188 | |
| 189 | Public Overloads Shared Operator +(list As Generic.List(Of Value(Of T)), x As Value(Of T)) As Generic.List(Of Value(Of T)) |
| 190 | Call list.Add(x) |
| 191 | Return list |
| 192 | End Operator |
| 193 | |
| 194 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 195 | Public Overloads Shared Operator +(x As Value(Of T), list As IEnumerable(Of T)) As List(Of T) |
| 196 | Return (+x).Join(list) |
| 197 | End Operator |
| 198 | |
| 199 | Public Overloads Shared Operator -(list As Generic.List(Of Value(Of T)), x As Value(Of T)) As Generic.List(Of Value(Of T)) |
| 200 | Call list.Remove(x) |
| 201 | Return list |
| 202 | End Operator |
| 203 | |
| 204 | Public Shared Operator <=(value As Value(Of T), o As T) As T |
| 205 | value.Value = o |
| 206 | Return o |
| 207 | End Operator |
| 208 | |
| 209 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 210 | Public Shared Narrowing Operator CType(x As Value(Of T)) As T |
| 211 | Return x.Value |
| 212 | End Operator |
| 213 | |
| 214 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 215 | Public Shared Widening Operator CType(x As T) As Value(Of T) |
| 216 | Return New Value(Of T)(x) |
| 217 | End Operator |
| 218 | |
| 219 | ''' <summary> |
| 220 | ''' Gets the <see cref="Value"/> property value. |
| 221 | ''' </summary> |
| 222 | ''' <param name="x"></param> |
| 223 | ''' <returns></returns> |
| 224 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 225 | Public Shared Operator +(x As Value(Of T)) As T |
| 226 | Return x.Value |
| 227 | End Operator |
| 228 | |
| 229 | ''' <summary> |
| 230 | ''' Inline value assignment: ``Dim s As String = Value(Of String) = var`` |
| 231 | ''' </summary> |
| 232 | ''' <param name="value"></param> |
| 233 | ''' <param name="o"></param> |
| 234 | ''' <returns></returns> |
| 235 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 236 | Public Shared Operator =(value As Value(Of T), o As T) As T |
| 237 | value.Value = o |
| 238 | Return o |
| 239 | End Operator |
| 240 | |
| 241 | Public Shared Operator <>(value As Value(Of T), o As T) As T |
| 242 | Throw New NotSupportedException |
| 243 | End Operator |
| 244 | |
| 245 | Public Shared Operator >=(value As Value(Of T), o As T) As T |
| 246 | Throw New NotSupportedException |
| 247 | End Operator |
| 248 | |
| 249 | 'Public Shared Operator &(o As Value(Of T)) As T |
| 250 | ' Return o.value |
| 251 | 'End Operator |
| 252 | End Class |
| 253 | End Namespace |