1 #Region "Microsoft.VisualBasic::7dea8c94f2219af62b1b9ba3eaa259df, Microsoft.VisualBasic.Core\Text\Xml\Models\ValueTuples\ValuePair.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 KeyValuePair
35     
36     '         Properties: Key, Value
37     
38     '         Constructor: (+3 OverloadsSub New
39     '         Function: CreateObject, Distinct, (+2 Overloads) Equals, ToDictionary, ToString
40     '         Interface IKeyValuePair
41     
42     
43     
44     
45     
46     
47     ' /********************************************************************************/
48
49 #End Region
50
51 Imports System.Xml.Serialization
52 Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
53
54 Namespace Text.Xml.Models
55
56     ''' <summary>
57     ''' An object for the text file format xml data storage.(用于存储与XML文件之中的字符串键值对对象)
58     ''' </summary>
59     ''' <remarks>
60     ''' + 2016-05-24 为了更好的构建GCModeller项目的数据文档的格式,本类型对象不再继承自<see cref="KeyValuePairObject(Of StringString)"/>类型
61     ''' </remarks>
62     ''' 
63     <XmlType("hashEntry")> Public Class KeyValuePair
64         Implements INamedValue
65         Implements IKeyValuePair
66
67         Public Sub New()
68
69         End Sub
70
71         Sub New(item As KeyValuePair(Of StringString))
72             Key = item.Key
73             Value = item.Value
74         End Sub
75
76         Sub New(Key As String, Value As String)
77             Me.Key = Key
78             Me.Value = Value
79         End Sub
80
81         ''' <summary>
82         ''' Defines a key/value pair that can be set or retrieved.(特化的<see cref="IKeyValuePairObject(Of StringString)"></see>字符串属性类型)
83         ''' </summary>
84         ''' <remarks></remarks>
85         Public Interface IKeyValuePair : Inherits IKeyValuePairObject(Of StringString)
86         End Interface
87
88 #Region "ComponentModel.Collection.Generic.KeyValuePairObject(Of StringString) property overrides"
89
90         ''' <summary>
91         ''' Gets the key in the key/value pair.
92         ''' </summary>
93         ''' <value></value>
94         ''' <returns></returns>
95         ''' <remarks>在这里可能用不了<see cref="XmlAttributeAttribute"></see>自定义属性,因为其基本类型之中的Key和Value可以是任意的类型的,Attribute格式无法序列化复杂的数据类型</remarks>
96         <XmlAttribute> Public Property Key As String Implements INamedValue.Key, IKeyValuePair.Key
97         <XmlAttribute> Public Property Value As String Implements IKeyValuePairObject(Of StringString).Value
98 #End Region
99
100         Public Overloads Shared Widening Operator CType(obj As KeyValuePair(Of StringString)) As KeyValuePair
101             Return New KeyValuePair With {
102                 .Key = obj.Key,
103                 .Value = obj.Value
104             }
105         End Operator
106
107         Public Overloads Shared Widening Operator CType(obj As String()) As KeyValuePair
108             Return New KeyValuePair With {
109                 .Key = obj.First,
110                 .Value = obj.ElementAtOrDefault(1)
111             }
112         End Operator
113
114         Public Overrides Function ToString() As String
115             Return String.Format("{0} -> {1}", Key, Value)
116         End Function
117
118         Public Overloads Shared Function CreateObject(key As String, value As StringAs KeyValuePair
119             Return New KeyValuePair With {
120                 .Key = key,
121                 .Value = value
122             }
123         End Function
124
125         Public Shared Function ToDictionary(list As IEnumerable(Of KeyValuePair)) As Dictionary(Of StringString)
126             Dim Dictionary As Dictionary(Of StringString) =
127                 list.ToDictionary(
128                     Function(obj) obj.Key,
129                     Function(obj) obj.Value)
130             Return Dictionary
131         End Function
132
133         Public Overrides Function Equals(obj As ObjectAs Boolean
134             If TypeOf obj Is KeyValuePair Then
135                 Dim KeyValuePair As KeyValuePair = DirectCast(obj, KeyValuePair)
136
137                 Return String.Equals(KeyValuePair.Key, Key) AndAlso
138                     String.Equals(KeyValuePair.Value, Value)
139             Else
140                 Return False
141             End If
142         End Function
143
144         ''' <summary>
145         ''' 
146         ''' </summary>
147         ''' <param name="obj"></param>
148         ''' <param name="strict">If strict is TRUE then the function of the string compares will case sensitive.</param>
149         ''' <returns></returns>
150         ''' <remarks></remarks>
151         Public Overloads Function Equals(obj As KeyValuePair, Optional strict As Boolean = TrueAs Boolean
152             If strict Then
153                 Return String.Equals(obj.Key, Key) AndAlso String.Equals(obj.Value, Value)
154             Else
155                 Return String.Equals(obj.Key, Key, StringComparison.OrdinalIgnoreCase) AndAlso
156                        String.Equals(obj.Value, Value, StringComparison.OrdinalIgnoreCase)
157             End If
158         End Function
159
160         Public Shared Function Distinct(source As KeyValuePair()) As KeyValuePair()
161             Dim List = (From obj In source Select obj Order By obj.Key Ascending).AsList
162
163             For i As Integer = 0 To List.Count - 1
164                 If i >= List.Count Then
165                     Exit For
166                 End If
167                 Dim item = List(i)
168
169                 For j As Integer = i + 1 To List.Count - 1
170                     If j >= List.Count Then
171                         Exit For
172                     End If
173                     If item.Equals(List(j)) Then
174                         Call List.RemoveAt(j)
175                         j -= 1
176                     End If
177                 Next
178             Next
179
180             Return List.ToArray
181         End Function
182     End Class
183 End Namespace