2017-03-02 125 views
-1

我有一個類有三個Date字段。有沒有一種簡單的方法來檢查字段是否爲null?

public static final class Filter { 
    private final Date startDateFrom; 
    private final Date startDateTo; 

    private final Date endDateFrom; 
    private final Date endDateTo; 

    private final Date regDateFrom; 
    private final Date regDateTo; 

    public Filter(Date startDateFrom, Date startDateTo, 
        Date endDateFrom, Date endDateTo, 
        Date regDateFrom, Date regDateTo) { 
     this.startDateFrom = startDateFrom; 
     this.startDateTo = startDateTo; 

     this.endDateFrom = endDateFrom; 
     this.endDateTo = endDateTo; 

     this.regDateFrom = regDateFrom; 
     this.regDateTo = regDateTo; 
    } 
} 

所以我需要檢查並確保至少一對同時具有不null日期,並檢查DateFrom之前或等於DateTo所有非空日期。

我做了這個檢查是否有至少一對:

public boolean allDatesMissing() { 
     if (startDateFrom != null && startDateTo != null) { 
      return false; 
     } else if (endDateFrom != null && endDateTo != null) { 
      return false; 
     } else if (regDateFrom != null && regDateTo != null) { 
      return false; 
     } 
     return true; 
    } 

而這個檢查fromDate是前toDate

public void checkDates(Date from, Date to) throws RequestException{ 
    if (from != null) { 
     if (to != null) { 
      if (from.after(to)) { 
       throw new MyException(INCORRECT_DATES_PERIOD); 
      } 
     } else { 
      throw new MyException(MISSING_PARAMETER); 
     } 
    } else if (to != null){ 
     throw new MyException(MISSING_PARAMETER); 
    } 
} 

有什麼simplier方式做相同?

+2

小的改進:'如果(from.getTime()> to.getTime()){' - > 'if(from.after(to)){' –

+0

看到這個問題:[Avoiding!= null statements](http://stackoverflow.com/q/271526/2815219) –

+0

考慮定義一個類間隔 –

回答

1

您已聲明過濾器類static..Then它應該是一個內部類..

package demo; 

import java.util.Date; 

public class FilterDemo { 

public static void main(String[] args) throws Exception { 
    Filter f = new Filter(new Date(2017, 01, 01), new Date(2017, 02, 01), 
      null, null, null, null); 

} 

public static final class Filter { 
    private final Date startDateFrom; 
    private final Date startDateTo; 

    private final Date endDateFrom; 
    private final Date endDateTo; 

    private final Date regDateFrom; 
    private final Date regDateTo; 
    // dateCounter will maintain the count of correct date pair 
    public static int dateCount = 0; 

    public Filter(Date startDateFrom, Date startDateTo, Date endDateFrom, 
      Date endDateTo, Date regDateFrom, Date regDateTo) 
      throws Exception { 
     this.startDateFrom = startDateFrom; 
     this.startDateTo = startDateTo; 
     isExist(startDateFrom, startDateTo); 
     this.endDateFrom = endDateFrom; 
     this.endDateTo = endDateTo; 
     isExist(endDateFrom, endDateTo); 
     this.regDateFrom = regDateFrom; 
     this.regDateTo = regDateTo; 
     isExist(regDateFrom, regDateTo); 
     // throw exception after initializing 3 pair,if count is still 0 
     if (dateCount == 0) 
      throw new Exception("All pairs are null"); 
    } 

    public static void isExist(Date from, Date to) throws Exception { 
     if (from != null && to != null) { 
      checkDates(from, to); 
     } 
    } 

    public static void checkDates(Date from, Date to) throws Exception { 
     if (from.getTime() > to.getTime()) { 
      throw new Exception("INCORRECT_DATES_PERIOD"); 
     } 
     //increase dateCount if date pair is correct 
     dateCount++; 
    } 
} 

} 
+0

絕對是絕對的對! 'Filter'類是內部的。很好的答案。 – Frum

相關問題