Record Class Filter

java.lang.Object
java.lang.Record
io.leandev.appfuse.search.Filter
Record Components:
node - 篩選器的內部節點表示

public record Filter(Node node) extends Record

查詢篩選器,用於建構結構化的查詢條件。

此類別提供流暢的 API 來建構複雜的查詢條件,支援:

  • 比較運算:等於、不等於、大於、小於、介於等
  • 邏輯運算:AND、OR 組合條件
  • 空值處理:IS NULL、IS NOT NULL
  • 集合運算:IN、NOT IN、HAS

使用範例:

// 簡單條件
Filter filter = Filter.eq("status", "ACTIVE");

// 組合條件
Filter filter = Filter.eq("status", "ACTIVE")
    .and(Filter.ge("amount", 100))
    .or(Filter.isNull("category"));
  • Constructor Details

    • Filter

      public Filter(String selector, ComparisonOperator operator, Object expectation)
      使用指定的選擇器、運算子和期望值建立篩選器。
      Parameters:
      selector - 欄位選擇器
      operator - 比較運算子
      expectation - 期望值
    • Filter

      public Filter()
      建立空的篩選器。
    • Filter

      public Filter(Node node)
      Creates an instance of a Filter record class.
      Parameters:
      node - the value for the node record component
  • Method Details

    • empty

      public static Filter empty()
      建立空的篩選器。
      Returns:
      空的篩選器實例
    • of

      public static Filter of(String selector, ComparisonOperator operator, Object expectation)

      使用指定的選擇器、運算子和期望值建立篩選器。

      對於非 IS/IS_NOT 運算子,若期望值為 null、空字串或空集合,會回傳空篩選器。

      Parameters:
      selector - 欄位選擇器
      operator - 比較運算子
      expectation - 期望值
      Returns:
      篩選器實例;若期望值無效則回傳空篩選器
    • eq

      public static Filter eq(String selector, Object expectation)
      建立等於條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      等於條件的篩選器
    • neq

      public static Filter neq(String selector, Object expectation)
      建立不等於條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      不等於條件的篩選器
    • like

      public static Filter like(String selector, CharSequence expectation, boolean nullable)

      建立模糊比對條件的篩選器。

      會將期望值包裝為 *expectation* 格式進行模糊比對。

      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      nullable - 是否允許 null 值匹配
      Returns:
      模糊比對條件的篩選器
    • like

      public static Filter like(String selector, CharSequence expectation)
      建立模糊比對條件的篩選器(不允許 null 值匹配)。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      模糊比對條件的篩選器
    • startsWith

      public static Filter startsWith(String selector, CharSequence expectation)

      建立前綴比對條件的篩選器。

      會將期望值包裝為 expectation* 格式進行前綴比對。

      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      前綴比對條件的篩選器
    • ge

      public static Filter ge(String selector, Object expectation)
      建立大於或等於條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      大於或等於條件的篩選器
    • gt

      public static Filter gt(String selector, Object expectation)
      建立大於條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      大於條件的篩選器
    • lt

      public static Filter lt(String selector, Object expectation)
      建立小於條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      小於條件的篩選器
    • le

      public static Filter le(String selector, Object expectation)
      建立小於或等於條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      小於或等於條件的篩選器
    • in

      public static Filter in(String selector, Object expectation)
      建立 IN 條件的篩選器,檢查欄位值是否在指定集合中。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值集合
      Returns:
      IN 條件的篩選器
    • notIn

      public static Filter notIn(String selector, Object expectation)
      建立 NOT IN 條件的篩選器,檢查欄位值是否不在指定集合中。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值集合
      Returns:
      NOT IN 條件的篩選器
    • has

      public static Filter has(String selector, Object expectation)
      建立 HAS 條件的篩選器,檢查集合欄位是否包含指定值。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      HAS 條件的篩選器
    • between

      public static Filter between(String selector, Object[] expectation)
      建立區間條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      expectation - 包含起始值和結束值的陣列
      Returns:
      區間條件的篩選器
    • between

      public static Filter between(String selector, Object start, Object end)
      建立區間條件的篩選器。
      Parameters:
      selector - 欄位選擇器
      start - 區間起始值
      end - 區間結束值
      Returns:
      區間條件的篩選器
    • is

      public static Filter is(String selector, Object expectation)
      建立 IS 條件的篩選器,用於精確比對(包含 null 值)。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      IS 條件的篩選器
    • isNot

      public static Filter isNot(String selector, Object expectation)
      建立 IS NOT 條件的篩選器,用於精確排除比對(包含 null 值)。
      Parameters:
      selector - 欄位選擇器
      expectation - 期望值
      Returns:
      IS NOT 條件的篩選器
    • isNull

      public static Filter isNull(String selector)
      建立 IS NULL 條件的篩選器,檢查欄位值是否為 null。
      Parameters:
      selector - 欄位選擇器
      Returns:
      IS NULL 條件的篩選器
    • isNotNull

      public static Filter isNotNull(String selector)
      建立 IS NOT NULL 條件的篩選器,檢查欄位值是否不為 null。
      Parameters:
      selector - 欄位選擇器
      Returns:
      IS NOT NULL 條件的篩選器
    • isEmpty

      public boolean isEmpty()
      檢查此篩選器是否為空。
      Returns:
      若為空篩選器則回傳 true
    • isNotEmpty

      public boolean isNotEmpty()
      檢查此篩選器是否不為空。
      Returns:
      若不為空篩選器則回傳 true
    • and

      public Filter and(Filter... filters)
      將多個篩選器以 AND 邏輯組合到此篩選器。
      Parameters:
      filters - 要組合的篩選器
      Returns:
      組合後的新篩選器
    • and

      public Filter and(List<Filter> filters)
      將篩選器列表以 AND 邏輯組合到此篩選器。
      Parameters:
      filters - 要組合的篩選器列表
      Returns:
      組合後的新篩選器
    • and

      public Filter and(String selector, ComparisonOperator operator, Object expectation)
      將指定條件以 AND 邏輯組合到此篩選器。
      Parameters:
      selector - 欄位選擇器
      operator - 比較運算子
      expectation - 期望值
      Returns:
      組合後的新篩選器
    • or

      public Filter or(Filter... filters)
      將多個篩選器以 OR 邏輯組合到此篩選器。
      Parameters:
      filters - 要組合的篩選器
      Returns:
      組合後的新篩選器
    • or

      public Filter or(List<Filter> filters)
      將篩選器列表以 OR 邏輯組合到此篩選器。
      Parameters:
      filters - 要組合的篩選器列表
      Returns:
      組合後的新篩選器
    • or

      public Filter or(String selector, ComparisonOperator operator, Object expectation)
      將指定條件以 OR 邏輯組合到此篩選器。
      Parameters:
      selector - 欄位選擇器
      operator - 比較運算子
      expectation - 期望值
      Returns:
      組合後的新篩選器
    • map

      public Filter map(Function<ComparisonNode, Node> fn)
      對篩選器中的所有比較節點套用轉換函式。
      Parameters:
      fn - 轉換函式,接收比較節點並回傳新節點
      Returns:
      轉換後的新篩選器
    • selector

      public String selector()
      取得比較節點的欄位選擇器。
      Returns:
      欄位選擇器;若非比較節點則回傳 null
    • expectation

      public Object expectation()
      取得比較節點的期望值。
      Returns:
      期望值;若非比較節點則回傳 null
    • operator

      public Object operator()
      取得節點的運算子。
      Returns:
      邏輯運算子或比較運算子;若為空節點則回傳 null
    • children

      public List<Filter> children()
      取得邏輯節點的子篩選器列表。
      Returns:
      子篩選器列表;若非邏輯節點則回傳空列表
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • node

      public Node node()
      Returns the value of the node record component.
      Returns:
      the value of the node record component