| 1 | #Region "Microsoft.VisualBasic::b697fa46dcb6544ff2cf80b0e8c757d4, Microsoft.VisualBasic.Core\ComponentModel\LazyLoader.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 LazyLoader |
| 35 | ' |
| 36 | ' |
| 37 | ' Delegate Function |
| 38 | ' |
| 39 | ' |
| 40 | ' Delegate Function |
| 41 | ' |
| 42 | ' Properties: URL, Value |
| 43 | ' |
| 44 | ' Constructor: (+1 Overloads) Sub New |
| 45 | ' |
| 46 | ' Function: ToString, WriteData |
| 47 | ' |
| 48 | ' Sub: __loadData, __printMSG |
| 49 | ' |
| 50 | ' Class Lazy |
| 51 | ' |
| 52 | ' Properties: Value |
| 53 | ' |
| 54 | ' Constructor: (+2 Overloads) Sub New |
| 55 | ' Function: ToString |
| 56 | ' |
| 57 | ' |
| 58 | ' |
| 59 | ' |
| 60 | ' |
| 61 | ' /********************************************************************************/ |
| 62 | |
| 63 | #End Region |
| 64 | |
| 65 | Imports Microsoft.VisualBasic.Serialization |
| 66 | Imports Microsoft.VisualBasic.Serialization.JSON |
| 67 | |
| 68 | Namespace ComponentModel |
| 69 | |
| 70 | ''' <summary> |
| 71 | ''' 当所需要进行加载的数据的量非常大的时候,则可以使用本方法进行延时按需加载 |
| 72 | ''' </summary> |
| 73 | ''' <typeparam name="TOutput"></typeparam> |
| 74 | ''' <remarks></remarks> |
| 75 | Public Class LazyLoader(Of TOutput As Class, TSource) |
| 76 | |
| 77 | Public Delegate Function DataLoadMethod(source As TSource) As TOutput |
| 78 | Public Delegate Function DataWriteMethod(source As TSource, obj As TOutput) As Boolean |
| 79 | |
| 80 | ''' <summary> |
| 81 | ''' Gets the value from the data source <see cref="URL"></see> |
| 82 | ''' </summary> |
| 83 | ''' <value></value> |
| 84 | ''' <returns></returns> |
| 85 | ''' <remarks></remarks> |
| 86 | Public Property Value As TOutput |
| 87 | Get |
| 88 | If _innerData Is Nothing Then |
| 89 | Call __loadData() |
| 90 | End If |
| 91 | |
| 92 | Return _innerData |
| 93 | End Get |
| 94 | Set(value As TOutput) |
| 95 | _innerData = value |
| 96 | End Set |
| 97 | End Property |
| 98 | |
| 99 | Const DATA_LOADED_MESSAGE As String = "[LATE_LOADER_MSG] {0} data load done! //{1}; ({2}) ........{3}ms." |
| 100 | |
| 101 | Dim _url As TSource |
| 102 | Dim _methodInfo As DataLoadMethod |
| 103 | Dim _innerData As TOutput |
| 104 | |
| 105 | Private Sub __loadData() |
| 106 | Dim sw As Stopwatch = Stopwatch.StartNew |
| 107 | _innerData = _methodInfo(_url) |
| 108 | Call __printMSG(sw.ElapsedMilliseconds) |
| 109 | End Sub |
| 110 | |
| 111 | Private Sub __printMSG(ElapsedMilliseconds As Long) |
| 112 | Dim url As String = Me.URL.ToString.ToFileURL |
| 113 | Dim method As String = _methodInfo.ToString |
| 114 | Dim msg As String = |
| 115 | String.Format(DATA_LOADED_MESSAGE, url, _innerData.GetType.FullName, method, ElapsedMilliseconds) |
| 116 | |
| 117 | Call msg.__DEBUG_ECHO |
| 118 | End Sub |
| 119 | |
| 120 | ''' <summary> |
| 121 | ''' The data source.(数据源) |
| 122 | ''' </summary> |
| 123 | ''' <value></value> |
| 124 | ''' <returns></returns> |
| 125 | ''' <remarks></remarks> |
| 126 | Public Property URL As TSource |
| 127 | Get |
| 128 | Return _url |
| 129 | End Get |
| 130 | Set(value As TSource) |
| 131 | _url = value |
| 132 | _innerData = Nothing |
| 133 | End Set |
| 134 | End Property |
| 135 | |
| 136 | Sub New(url As TSource, p As DataLoadMethod) |
| 137 | _url = url |
| 138 | _methodInfo = p |
| 139 | End Sub |
| 140 | |
| 141 | Public Overrides Function ToString() As String |
| 142 | Return _url.ToString.ToFileURL |
| 143 | End Function |
| 144 | |
| 145 | ''' <summary> |
| 146 | ''' Write the data back onto the filesystem.(将数据回写进入文件系统之中) |
| 147 | ''' </summary> |
| 148 | ''' <param name="WriteMethod"></param> |
| 149 | ''' <returns></returns> |
| 150 | ''' <remarks></remarks> |
| 151 | Public Function WriteData(WriteMethod As DataWriteMethod) As Boolean |
| 152 | Return WriteMethod(URL, _innerData) |
| 153 | End Function |
| 154 | |
| 155 | Public Shared Narrowing Operator CType(obj As LazyLoader(Of TOutput, TSource)) As TOutput |
| 156 | Return obj.Value |
| 157 | End Operator |
| 158 | End Class |
| 159 | |
| 160 | ''' <summary> |
| 161 | ''' The layze loader. |
| 162 | ''' </summary> |
| 163 | ''' <typeparam name="TOut"></typeparam> |
| 164 | Public Class Lazy(Of TOut) |
| 165 | |
| 166 | ''' <summary> |
| 167 | ''' the data source handler. |
| 168 | ''' </summary> |
| 169 | Protected __factory As Func(Of TOut) |
| 170 | ''' <summary> |
| 171 | ''' The output result cache data. |
| 172 | ''' </summary> |
| 173 | Protected _cache As TOut |
| 174 | |
| 175 | ''' <summary> |
| 176 | ''' Get cache data if it exists, or the data will be loaded first. |
| 177 | ''' </summary> |
| 178 | ''' <returns></returns> |
| 179 | Public ReadOnly Property Value As TOut |
| 180 | Get |
| 181 | If _cache Is Nothing Then |
| 182 | _cache = __factory() |
| 183 | End If |
| 184 | |
| 185 | Return _cache |
| 186 | End Get |
| 187 | End Property |
| 188 | |
| 189 | Sub New(value As TOut) |
| 190 | _cache = value |
| 191 | End Sub |
| 192 | |
| 193 | ''' <summary> |
| 194 | ''' Init this lazy loader with the data source handler. |
| 195 | ''' </summary> |
| 196 | ''' <param name="valueFactory"> |
| 197 | ''' The data source provider handler. |
| 198 | ''' </param> |
| 199 | Sub New(valueFactory As Func(Of TOut)) |
| 200 | __factory = valueFactory |
| 201 | End Sub |
| 202 | |
| 203 | Public Overrides Function ToString() As String |
| 204 | If _cache Is Nothing Then |
| 205 | Return GetType(TOut).FullName |
| 206 | Else |
| 207 | Return _cache.GetJson |
| 208 | End If |
| 209 | End Function |
| 210 | |
| 211 | Public Shared Narrowing Operator CType(lazy As Lazy(Of TOut)) As TOut |
| 212 | Return lazy.Value |
| 213 | End Operator |
| 214 | End Class |
| 215 | End Namespace |