谓词

谓词

 

我看到Ninputer的在Visual Basic 2005中创建和使用谓词觉得这个东西非常好,但是他也许没有时间继续
再加上我在重做刘慈欣的《电子诗人》时要用到,所以我就先实现了一个。


#Region "
谓词 "
''' <summary>谓词</summary>
''' <remarks>
''' by F.R.C.
''' Copyright All Free
''' 你可以随意修改或将它集成到你的软件中
'''
''' 用于从使用Predicate(Of T)的表、集合等选取符合特定条件的表或集合
''' 记住在源文件前加上Imports [项目名].Predicater
''' 代码构思来自Ninputer Blog : http://blog.joycode.com/ninputer/
'''
''' 使用示例
''' Dim l1 As New List(Of Integer)
''' For i As Integer = 1 To 100
''' l1.Add(i)
''' Next
'''
''' Dim l2 As List(Of Integer)
''' l2 = l1.FindAll(GreaterThan(1) And Not GreaterThan(9) Or EqualsTo(23))
''' 结果为l2={2,3,4,5,6,7,8,9,23}
''' </remarks>
Public Class Predicater 
    Public Shared Function GreaterThan(Of T As IComparable(Of T))(ByVal Value As T) As Constructor(Of T) 
        Dim Pred As New Constructor(Of T)(Value) 
        Pred.Predicate = AddressOf Pred.GreaterThanProcedure 
        Return Pred 
    End Function 
    Public Shared Function SmallerThan(Of T As IComparable(Of T))(ByVal Value As T) As Constructor(Of T) 
        Dim Pred As New Constructor(Of T)(Value) 
        Pred.Predicate = AddressOf Pred.SmallerThanProcedure 
        Return Pred 
    End Function 
    Public Shared Function EqualsTo(Of T As IComparable(Of T))(ByVal Value As T) As Constructor(Of T) 
        Dim Pred As New Constructor(Of T)(Value) 
        Pred.Predicate = AddressOf Pred.EqualsToProcedure 
        Return Pred 
    End Function 
    Public Shared Function NotEqualsTo(Of T As IComparable(Of T))(ByVal Value As T) As Constructor(Of T) 
        Dim Pred As New Constructor(Of T)(Value) 
        Pred.Predicate = AddressOf Pred.NotEqualsToProcedure 
        Return Pred 
    End Function 

    ''' <summary>谓词构造类</summary> 
    ''' <remarks>提供谓词的边界参数</remarks> 
    Public Class Constructor(Of T As IComparable(Of T)) 
        Private _Value As T 
        Public Predicate As Predicate(Of T) 

        Public Sub New(ByVal Value As T) 
            _Value = Value 
        End Sub 

        Friend Function GreaterThanProcedure(ByVal obj As T) As Boolean 
            Return obj.CompareTo(_Value) > 0 
        End Function 
        Friend Function SmallerThanProcedure(ByVal obj As T) As Boolean 
            Return obj.CompareTo(_Value) < 0 
        End Function 
        Friend Function EqualsToProcedure(ByVal obj As T) As Boolean 
            Return obj.CompareTo(_Value) = 0 
        End Function 
        Friend Function NotEqualsToProcedure(ByVal obj As T) As Boolean 
            Return obj.CompareTo(_Value) <> 0 
        End Function 

        Public Shared Widening Operator CType(ByVal Provider As Constructor(Of T)) As Predicate(Of T) 
            If Provider Is Nothing Then Return Nothing 
            Return Provider.Predicate 
        End Operator 
        Public Shared Widening Operator CType(ByVal Predicate As Predicate(Of T)) As Constructor(Of T) 
            Dim PredicateProvider As New Constructor(Of T)(Nothing) 
            PredicateProvider.Predicate = Predicate 
            Return PredicateProvider 
        End Operator 
        Public Shared Operator And(ByVal Left As Constructor(Of T), ByVal Right As Constructor(Of T)) As Predicate(Of T) 
            Return AddressOf (New ConstructorLogical(Of T)(Left, Right)).PredicateAnd 
        End Operator 
        Public Shared Operator Or(ByVal Left As Constructor(Of T), ByVal Right As Constructor(Of T)) As Predicate(Of T) 
            Return AddressOf (New ConstructorLogical(Of T)(Left, Right)).PredicateOr 
        End Operator 
        Public Shared Operator Xor(ByVal Left As Constructor(Of T), ByVal Right As Constructor(Of T)) As Predicate(Of T) 
            Return AddressOf (New ConstructorLogical(Of T)(Left, Right)).PredicateXor 
        End Operator 
        Public Shared Operator Not(ByVal Right As Constructor(Of T)) As Predicate(Of T) 
            Return AddressOf (New ConstructorLogical(Of T)(Nothing, Right)).PredicateNot 
        End Operator 
    End Class 

    ''' <summary>谓词逻辑构造类</summary> 
    ''' <remarks>提供谓词的逻辑运算</remarks> 
    Friend Class ConstructorLogical(Of S As IComparable(Of S)) 
        Private Predicate1 As Predicate(Of S) 
        Private Predicate2 As Predicate(Of S) 
        Friend Sub New(ByVal Left As Constructor(Of S), ByVal Right As Constructor(Of S)) 
            Predicate1 = Left 
            Predicate2 = Right 
        End Sub 
        Friend Function PredicateAnd(ByVal obj As S) As Boolean 
            Return Predicate1(obj) And Predicate2(obj) 
        End Function 
        Friend Function PredicateOr(ByVal obj As S) As Boolean 
            Return Predicate1(obj) Or Predicate2(obj) 
        End Function 
        Friend Function PredicateXor(ByVal obj As S) As Boolean 
            Return Predicate1(obj) Xor Predicate2(obj) 
        End Function 
        Friend Function PredicateNot(ByVal obj As S) As Boolean 
            Return Not Predicate2(obj) 
        End Function 
    End Class
End Class
#End Region

posted @ 2005-08-15 22:26  地狱门神  阅读(839)  评论(8编辑  收藏  举报