2019-07-29 02:05:05 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::7215eacef6da5d9f4c3c795ae8d23869, Microsoft.VisualBasic.Core\Language\Value\DefaultValue\Default.vb"
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2018-11-17 20:51:14 +08:00
|
|
|
|
' Author:
|
|
|
|
|
|
'
|
|
|
|
|
|
' asuka (amethyst.asuka@gcmodeller.org)
|
|
|
|
|
|
' xie (genetics@smrucc.org)
|
|
|
|
|
|
' xieguigang (xie.guigang@live.com)
|
|
|
|
|
|
'
|
|
|
|
|
|
' Copyright (c) 2018 GPL3 Licensed
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' GNU GENERAL PUBLIC LICENSE (GPL3)
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
' it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
' the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
' (at your option) any later version.
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
' but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
' GNU General Public License for more details.
|
|
|
|
|
|
'
|
|
|
|
|
|
' You should have received a copy of the GNU General Public License
|
|
|
|
|
|
' along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-11-17 17:52:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-17 20:51:14 +08:00
|
|
|
|
' /********************************************************************************/
|
2018-11-17 17:52:38 +08:00
|
|
|
|
|
2018-11-17 20:51:14 +08:00
|
|
|
|
' Summaries:
|
2018-11-17 17:52:38 +08:00
|
|
|
|
|
2018-11-17 20:51:14 +08:00
|
|
|
|
' Delegate Function
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' Delegate Function
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
2019-05-24 18:33:50 +08:00
|
|
|
|
' Interface IDefault
|
2018-11-17 20:51:14 +08:00
|
|
|
|
'
|
|
|
|
|
|
' Properties: DefaultValue
|
|
|
|
|
|
'
|
|
|
|
|
|
' Interface IsEmpty
|
|
|
|
|
|
'
|
|
|
|
|
|
' Properties: IsEmpty
|
|
|
|
|
|
'
|
2019-05-24 18:33:50 +08:00
|
|
|
|
' Structure [Default]
|
2018-11-17 20:51:14 +08:00
|
|
|
|
'
|
|
|
|
|
|
' Properties: DefaultValue, IsEmpty
|
|
|
|
|
|
'
|
|
|
|
|
|
' Constructor: (+2 Overloads) Sub New
|
|
|
|
|
|
' Function: (+2 Overloads) [When], getDefault, GetNumericAssert, ToString
|
|
|
|
|
|
' Operators: (+2 Overloads) +, (+6 Overloads) Or
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
|
|
|
|
|
Imports System.Linq.Expressions
|
|
|
|
|
|
Imports System.Runtime.CompilerServices
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Language.Perl
|
|
|
|
|
|
|
|
|
|
|
|
Namespace Language.Default
|
|
|
|
|
|
|
|
|
|
|
|
Public Delegate Function Assert(Of T)(obj As T) As Boolean
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' + Test of A eqauls to B?
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
|
|
|
|
|
''' <param name="x"></param>
|
|
|
|
|
|
''' <param name="y"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
Public Delegate Function BinaryAssert(Of T)(x As T, y As T) As Boolean
|
|
|
|
|
|
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Interface IDefault(Of T)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
ReadOnly Property DefaultValue As T
|
|
|
|
|
|
End Interface
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Apply on the structure type that assert the object is null or not.
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
Public Interface IsEmpty
|
|
|
|
|
|
ReadOnly Property IsEmpty As Boolean
|
|
|
|
|
|
End Interface
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' The default value
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
2019-06-06 21:16:58 +08:00
|
|
|
|
Public Structure [Default](Of T)
|
|
|
|
|
|
Implements IDefault(Of T)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Implements IsEmpty
|
|
|
|
|
|
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public ReadOnly Property DefaultValue As T Implements IDefault(Of T).DefaultValue
|
2018-08-02 20:14:48 +08:00
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
|
|
|
|
|
Get
|
2019-02-19 18:25:51 +08:00
|
|
|
|
If Not constructor Is Nothing Then
|
|
|
|
|
|
Return constructor()
|
|
|
|
|
|
ElseIf Not lazy Is Nothing Then
|
2018-08-02 20:14:48 +08:00
|
|
|
|
' using lazy loading, if the default value takes time to creates.
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Return lazy.Value()
|
|
|
|
|
|
Else
|
|
|
|
|
|
Return value
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End If
|
|
|
|
|
|
End Get
|
|
|
|
|
|
End Property
|
|
|
|
|
|
|
|
|
|
|
|
Public ReadOnly Property IsEmpty As Boolean Implements IsEmpty.IsEmpty
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
|
|
|
|
|
Get
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Return lazy Is Nothing AndAlso constructor Is Nothing AndAlso True = assert(value)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Get
|
|
|
|
|
|
End Property
|
|
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
#Region "Default Value/Generator"
|
2018-08-02 20:14:48 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' The default value for <see cref="DefaultValue"/>
|
|
|
|
|
|
''' </summary>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Dim value As T
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 假若生成目标值的时间比较久,可以将其申明为Lambda表达式,这样子可以进行惰性加载
|
|
|
|
|
|
''' </summary>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Dim lazy As Lazy(Of T)
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 与<see cref="lazy"/>不同的是,这个会一直产生新的数据
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
Dim constructor As Func(Of T)
|
|
|
|
|
|
#End Region
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
2019-03-25 18:15:46 +08:00
|
|
|
|
''' asset that if target value is null? If this function returns true when
|
|
|
|
|
|
''' test on the object, means object value is missing or null, then default
|
|
|
|
|
|
''' value <see cref="DefaultValue"/> will be returns.
|
2018-08-02 20:14:48 +08:00
|
|
|
|
''' </summary>
|
|
|
|
|
|
Dim assert As Assert(Of Object)
|
|
|
|
|
|
|
2018-11-17 17:52:38 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 这个判断函数优化了对数字类型的判断
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="n"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
''' <remarks>
|
|
|
|
|
|
''' 在VB之中,数值类型在未赋值的状态下默认值为零,意味着此时该数值的值为空
|
|
|
|
|
|
''' 但是不清楚这样子判断是否会出现bug?
|
|
|
|
|
|
''' </remarks>
|
|
|
|
|
|
Public Shared Function GetNumericAssert(n As Object) As Boolean
|
|
|
|
|
|
If n Is Nothing Then
|
|
|
|
|
|
' 可空类型的数值类型
|
|
|
|
|
|
Return True
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
Select Case n.GetType
|
|
|
|
|
|
Case GetType(Integer), GetType(Long), GetType(ULong), GetType(UInteger), GetType(Short), GetType(UShort)
|
|
|
|
|
|
Return CInt(n) = 0 OrElse CDbl(n).IsNaNImaginary
|
|
|
|
|
|
Case GetType(Double), GetType(Single), GetType(Decimal)
|
|
|
|
|
|
Return CDbl(n) = 0.0 OrElse CDbl(n).IsNaNImaginary
|
|
|
|
|
|
Case Else
|
|
|
|
|
|
#If DEBUG Then
|
|
|
|
|
|
Call n.GetType.FullName.Warning
|
|
|
|
|
|
#End If
|
|
|
|
|
|
Return ExceptionHandle.Default(obj:=n)
|
|
|
|
|
|
End Select
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Sub New(value As T, Optional assert As Assert(Of Object) = Nothing)
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Me.value = value
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Me.assert = assert Or defaultAssert
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
2019-05-31 19:14:18 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
'''
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="populator"></param>
|
|
|
|
|
|
''' <param name="assert"></param>
|
|
|
|
|
|
''' <param name="isLazy">
|
|
|
|
|
|
''' + 如果这个参数为true,则表示表达式为lazy加载,只会执行一次
|
|
|
|
|
|
''' + 反之当这个参数为false的时候,则表达式会不断的产生新的值
|
|
|
|
|
|
''' </param>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Sub New(populator As Func(Of T), Optional assert As Assert(Of Object) = Nothing, Optional isLazy As Boolean = True)
|
|
|
|
|
|
If isLazy Then
|
|
|
|
|
|
Me.lazy = populator.AsLazy
|
|
|
|
|
|
Else
|
|
|
|
|
|
Me.constructor = populator
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Me.assert = assert Or defaultAssert
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Function [When](expression As Boolean) As [Default](Of T)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
assert = Function(null) expression
|
|
|
|
|
|
Return Me
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Function [When](assert As Assert(Of T)) As [Default](Of T)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Me.assert = Function(o) assert(DirectCast(o, T))
|
|
|
|
|
|
Return Me
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
Public Overrides Function ToString() As String
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Return $"default({value})"
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Add handler
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="[default]"></param>
|
|
|
|
|
|
''' <param name="assert"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
'''
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Operator +([default] As [Default](Of T), assert As Assert(Of Object)) As [Default](Of T)
|
|
|
|
|
|
Return New [Default](Of T) With {
|
2018-08-02 20:14:48 +08:00
|
|
|
|
.assert = assert,
|
2019-02-19 18:25:51 +08:00
|
|
|
|
.value = [default].value
|
2018-08-02 20:14:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
End Operator
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Operator +([default] As [Default](Of T), assert As Expression(Of Func(Of Boolean))) As [Default](Of T)
|
|
|
|
|
|
Return New [Default](Of T) With {
|
2018-08-02 20:14:48 +08:00
|
|
|
|
.assert = Function(null) (assert.Compile())(),
|
2019-02-19 18:25:51 +08:00
|
|
|
|
.value = [default].value
|
2018-08-02 20:14:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
End Operator
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' if <see cref="assert"/> is true, then will using default <see cref="value"/>,
|
|
|
|
|
|
''' otherwise, return the source <paramref name="obj"/>.
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="obj"></param>
|
|
|
|
|
|
''' <param name="[default]"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
'''
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Operator Or(obj As T, [default] As [Default](Of T)) As T
|
2018-10-05 23:38:48 +08:00
|
|
|
|
Return getDefault(obj, [default].DefaultValue, If([default].assert, ExceptionHandle.defaultHandler))
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Operator
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
|
|
|
|
|
Private Shared Function getDefault(value As T, [default] As T, assert As Assert(Of Object))
|
|
|
|
|
|
Return If(assert(value), [default], value)
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Operator Or([default] As [Default](Of T), obj As T) As T
|
2018-10-05 23:38:48 +08:00
|
|
|
|
Return getDefault([default].DefaultValue, obj, If([default].assert, ExceptionHandle.defaultHandler))
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Operator
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 这个操作符允许链式计算默认值:
|
|
|
|
|
|
'''
|
|
|
|
|
|
''' A OR B OR C OR x OR y OR z
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="x"></param>
|
|
|
|
|
|
''' <param name="y"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Operator Or(x As [Default](Of T), y As [Default](Of T)) As T
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Return x.DefaultValue Or y
|
|
|
|
|
|
End Operator
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Widening Operator CType(obj As T) As [Default](Of T)
|
|
|
|
|
|
Return New [Default](Of T) With {
|
2019-02-19 18:25:51 +08:00
|
|
|
|
.value = obj,
|
2018-10-05 23:38:48 +08:00
|
|
|
|
.assert = AddressOf ExceptionHandle.Default
|
2018-08-02 20:14:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
End Operator
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Narrowing Operator CType([default] As [Default](Of T)) As T
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Return [default].DefaultValue
|
|
|
|
|
|
End Operator
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-05-22 19:02:42 +08:00
|
|
|
|
Public Shared Widening Operator CType(lazy As Func(Of T)) As [Default](Of T)
|
|
|
|
|
|
Return New [Default](Of T) With {
|
2019-02-19 18:25:51 +08:00
|
|
|
|
.lazy = lazy.AsLazy,
|
2018-10-05 23:38:48 +08:00
|
|
|
|
.assert = AddressOf ExceptionHandle.Default
|
2018-08-02 20:14:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
End Operator
|
|
|
|
|
|
End Structure
|
|
|
|
|
|
End Namespace
|