簡體   English   中英

使用 Java 8 Predicates 改進代碼 - 比較多個參數

[英]Improving code with Java 8 Predicates - comparing multiple arguments

我不確定我有正確的標題。 我想對請求執行一些驗證。 我在現有代碼的很多地方都有這個條件測試。 它位於實用程序類中。 該方法是DoesRequestLineFallsWithinDateRange(object)。

我想知道我是否可以通過使用謂詞並停用該方法來改進代碼並在 Java 8 中更加優雅。

處理請求:我想檢查每一行是否滿足條件。 所以調用方法調用這個實用程序類來檢查每一行是否在數據范圍內。 在此示例中,是訂閱期內的項目 (requestLine)。

一個請求進來,一些處理將訂閱成員對象添加到請求行。 然后在每一行上調用實用程序方法以檢查請求行是否在訂閱周期內。

該方法首先檢查它應該使用的日期字段是否不為空,以防止出現空異常。

然后使用請求行日期,與訂閱成員開始/結束日期字段進行比較以確定日期是否在范圍內。

我想用 Java 8 Predicate 函數調用替換該方法。

這是測試集合中每一行是否在數據范圍內的方法示例。

public static boolean DoesRequestLineFallsWithinDateRange(RequestLine requestLine) {
    if (isDatesNotNull(requestLine)) {
      return requestLine.getServiceStartDate().compareTo(requestLine.getSubscription().getStartDate()) >= 0
          && requestLine.getServiceStartDate().compareTo(requestLine.getSubscription().getEndDate()) <= 0;
    }
   return false;
  }

public class RequestLine(){
   private Date serviceStartDate;
   private Subscription subscription;
.....

}

這是使用 Java 8 PredicatesLocalDate API 編寫的示例代碼

    public boolean doesRequestLineFallsWithinDateRange(RequestLine requestLine)
    {
        BiPredicate<LocalDate, LocalDate> afterDatePredicate = (startData, endDate) -> (startData.isAfter(endDate));
        BiPredicate<LocalDate, LocalDate> beforeDatePredicate = (startData, endDate) -> (startData.isBefore(endDate));

        if (Objects.isNull(requestLine))
        {
            return false;
        }

        LocalDate serviceStartDate = requestLine.getServiceStartDate();
        LocalDate startDate = requestLine.getSubscription().getStartDate();
        LocalDate endDate = requestLine.getSubscription().getEndDate();

        return afterDatePredicate.test(serviceStartDate, startDate) && beforeDatePredicate.test(serviceStartDate,
                endDate);

    }

引用的 Java 代碼

請求線

public class RequestLine

{
    private Subscription subscription;
    private LocalDate serviceStartDate;

    // getters and setters
}

訂閱.java

public class Subscription
{
    private LocalDate startDate;
    private LocalDate endDate;

    // getters and setters
}

我嘗試集成您的代碼以使用方法引用作為謂詞並過濾 RequestLine 流。 有趣的方法是execute :它接收一個流並使用對 doRequestLineFallsWithinDateRange 方法的引用對其進行過濾。


import java.util.Date;
import java.util.stream.Stream;

public class RequestLine {

    public void execute(Stream<RequestLine> stream) {
        // this is the interesting code
        stream.filter(this::doesRequestLineFallsWithinDateRange);
    }

    public boolean doesRequestLineFallsWithinDateRange(RequestLine requestLine) {
        if (isDatesNotNull(requestLine)) {
            return requestLine.getServiceStartDate().compareTo(requestLine.getSubscription().getStartDate()) >= 0
                    && requestLine.getServiceStartDate().compareTo(requestLine.getSubscription().getEndDate()) <= 0;
        }
        return false;
    }

    //code created to complete the example
    public static class Subscription {

        private Date startDate;

        public Date getEndDate() {
            return endDate;
        }

        private Date endDate;

        public Date getStartDate() {
            return  startDate;
        }
    }



    private boolean isDatesNotNull(RequestLine requestLine) {
        return requestLine.serviceStartDate!=null;
    }


    public Date getServiceStartDate() {
        return serviceStartDate;
    }

    public Subscription getSubscription() {
        return subscription;
    }

    private Date serviceStartDate;
    private Subscription subscription;

}

使用Predicate::and鏈接多個謂詞

Predicate<RequestLine> rlPred = this::isDatesNotNull;

rlPred = rlPred.and(p -> p.getServiceStartDate().compareTo(p.getSubscription().getStartDate()) >= 0)
               .and(p -> p.getServiceStartDate().compareTo(p.getSubscription().getEndDate()) <= 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM