2017-03-02 121 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

相关问题