1 #Region "Microsoft.VisualBasic::04f1c568f523034097893e43b84b73af, Microsoft.VisualBasic.Core\ComponentModel\DataSource\Property\NamedValue(Of T).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     '     Structure NamedValue
35     
36     '         Properties: Description, IsEmpty, Name, Value, ValueType
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         Function: FixValue, ToString
40     '         Operators: (+2 Overloads) +, <>, =
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 Imports System.Runtime.CompilerServices
48 Imports System.Web.Script.Serialization
49 Imports System.Xml.Serialization
50 Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
51 Imports Microsoft.VisualBasic.Language.Default
52 Imports Microsoft.VisualBasic.Serialization.JSON
53
54 Namespace ComponentModel.DataSourceModel
55
56     ''' <summary>
57     ''' The value object have a name string.(一个具有自己的名称的变量值)
58     ''' </summary>
59     ''' <typeparam name="T"></typeparam>
60     Public Structure NamedValue(Of T) : Implements INamedValue
61         Implements IKeyValuePairObject(Of String, T)
62         Implements IsEmpty
63
64         ''' <summary>
65         ''' Identifier tag data. you can using this property value as a dictionary key.
66         ''' </summary>
67         ''' <returns></returns>
68         <XmlAttribute>
69         Public Property Name As String Implements INamedValue.Key, IKeyValuePairObject(Of String, T).Key
70
71         ''' <summary>
72         ''' Object value
73         ''' </summary>
74         ''' <returns></returns>
75         <XmlElement> Public Property Value As T Implements IKeyValuePairObject(Of String, T).Value
76
77         ''' <summary>
78         ''' Additional description text about this variable <see cref="Value"/>
79         ''' </summary>
80         ''' <returns></returns>
81         <XmlAttribute>
82         Public Property Description As String
83
84         ''' <summary>
85         ''' Does this object have value?
86         ''' </summary>
87         ''' <returns></returns>
88         <XmlIgnore, ScriptIgnore, DataIgnored>
89         Public ReadOnly Property IsEmpty As Boolean Implements IsEmpty.IsEmpty
90             <MethodImpl(MethodImplOptions.AggressiveInlining)>
91             Get
92                 Return String.IsNullOrEmpty(Name) AndAlso Value Is Nothing
93             End Get
94         End Property
95
96         ''' <summary>
97         ''' Returns object gettype
98         ''' </summary>
99         ''' <returns></returns>
100         Public ReadOnly Property ValueType As Type
101             <MethodImpl(MethodImplOptions.AggressiveInlining)>
102             Get
103                 ' 假若类型参数T是基类型的话,则直接使用GetType(T)只能够得到基类型的信息,无法得到当前的实现类型的信息
104                 ' 故而要在这里使用对象自身的GetType方法来获取真正的类型信息
105                 Return Value.GetType
106             End Get
107         End Property
108
109         ''' <summary>
110         ''' Creates a object bind with a specific <see cref="Name"/>.
111         ''' </summary>
112         ''' <param name="name"></param>
113         ''' <param name="value"></param>
114         Sub New(name$, value As T, Optional describ As String = Nothing)
115             Me.Name = name
116             Me.Value = value
117             Me.Description = describ
118         End Sub
119
120         ''' <summary>
121         ''' View object.
122         ''' </summary>
123         ''' <returns></returns>
124         Public Overrides Function ToString() As String
125             Try
126                 ' 2018-4-9 在这里将显示修改为类型的ToString方法,这样子就可以由
127                 ' 用户来通过重写ToString方法来自定义显示,而非强制使用GetJson方法
128                 ' 将全部的对象都显示出来,对于属性很多的对象GetJson方法显示的效果不是太好
129                 If Description.StringEmpty Then
130                     Return $"{Name} --> {Value.ToString}"
131                 Else
132                     Return $"{Name} --> {Value.ToString} ({Description})"
133                 End If
134             Catch ex As Exception
135                 Return Name
136             End Try
137         End Function
138
139         Public Function FixValue(h As Func(Of T, T)) As NamedValue(Of T)
140             Return New NamedValue(Of T)(Name, h(Value))
141         End Function
142
143         Public Shared Operator +(obj As NamedValue(Of T)) As T
144             Return obj.Value
145         End Operator
146
147         ''' <summary>
148         ''' 这个函数会将<paramref name="value"/>插入为<paramref name="table"/>的第一个元素
149         ''' </summary>
150         ''' <param name="value"></param>
151         ''' <param name="table"></param>
152         ''' <returns></returns>
153         Public Shared Operator +(value As NamedValue(Of T), table As Dictionary(Of String, T)) As Dictionary(Of String, T)
154             Dim newTable As New Dictionary(Of String, T) From {
155                 {value.Name, value.Value}
156             }
157
158             For Each map As KeyValuePair(Of String, T) In table
159                 Call newTable.Add(map.Key, map.Value)
160             Next
161
162             Return newTable
163         End Operator
164
165         <MethodImpl(MethodImplOptions.AggressiveInlining)>
166         Public Shared Operator =(tuple As NamedValue(Of T), compares As T) As Boolean
167             If tuple.Value Is Nothing Then
168                 If compares Is Nothing Then
169                     Return True
170                 Else
171                     Return False
172                 End If
173             Else
174                 If compares Is Nothing Then
175                     Return False
176                 Else
177                     Return tuple.Value.Equals(compares)
178                 End If
179             End If
180         End Operator
181
182         ''' <summary>
183         ''' Convert from tuple
184         ''' </summary>
185         ''' <param name="tuple"></param>
186         ''' <returns></returns>
187         ''' 
188         <MethodImpl(MethodImplOptions.AggressiveInlining)>
189         Public Shared Widening Operator CType(tuple As (name$, value As T)) As NamedValue(Of T)
190             Return New NamedValue(Of T)(tuple.name, tuple.value)
191         End Operator
192
193         <MethodImpl(MethodImplOptions.AggressiveInlining)>
194         Public Shared Widening Operator CType(tuple As (name$, value As T, describ$)) As NamedValue(Of T)
195             Return New NamedValue(Of T)(tuple.name, tuple.value, tuple.describ)
196         End Operator
197
198         <MethodImpl(MethodImplOptions.AggressiveInlining)>
199         Public Shared Operator <>(tuple As NamedValue(Of T), compares As T) As Boolean
200             Return Not tuple = compares
201         End Operator
202     End Structure
203 End Namespace