1

I am experiencing a strange behaviour when checking null pointer in a date field inside a collection object

public class FilterTest {
       public static void main(String[] args) {
              Collection<EmployeeDto> employees = new ArrayList<EmployeeDto>();
           employees.add(new EmployeeDto("", 1111, new Date()));
           employees.add(new EmployeeDto("123", 2222, null));
           employees.add(new EmployeeDto("22", 3333, new Date()));
           employees.add(new EmployeeDto("22", 4444, null));
           employees.add(new EmployeeDto("11", 5555, null));
           employees.add(new EmployeeDto("22", 6666, null));
           employees.add(new EmployeeDto(null, 6666,null));

           StandardEvaluationContext stdContext = new StandardEvaluationContext();
           stdContext.setVariable("emps", employees);      

           ExpressionParser parser = new SpelExpressionParser();    


          **String key4= "#emps.?[dateOfBirth != null ? T(DateUtil).formatDate(dateOfBirth), 'dd/MM/yyyy').contains('25/07/2012') : false]" ;**          


              Collection<Object> proj1 = (Collection<Object>) parser.parseExpression(
                      key4).getValue(stdContext);
              System.out.println(proj1);    



}
class EmployeeDto {
       String name;
       public String getName() {
              return name;
       }

       public void setName(String name) {
              this.name = name;
       }

       public int getAge() {
              return age;
       }

       public void setAge(int age) {
              this.age = age;
       }

       int age;
       Date dateOfBirth;

       public EmployeeDto(String name, int age, Date dateOfBirth) {
              this.name = name;
              this.age = age;
              this.dateOfBirth = dateOfBirth;
       }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
}

The expression should return collection of objects satisfying the condition. But now , I am getting an exception that dateOfBirth cannot be found on null. So I tried with a new expression

 String key4= "#emps.?[dateOfBirth != null ? T(DateUtil).formatDate(#emps.![dateOfBirth], 'dd/MM/yyyy').contains('25/07/2012') : false]" ;

This works fine. But when I add null in the first object of the collection, it return results as 0, and when I checked, I found that null is passed to the DateUtil class formatDate method. ie the emploee collection is like `

employees.add(new EmployeeDto("", 1111, null)); // first value is null, and I am getting empty result, but there is one corrrect result
           **employees.add(new EmployeeDto("12", 2222, null));**
           employees.add(new EmployeeDto("22", 3333, new Date()));
           employees.add(new EmployeeDto("33", 4444, null));
           employees.add(new EmployeeDto("FIVE", 5555, null));
           employees.add(new EmployeeDto("SIX", 6666, null));
           employees.add(new EmployeeDto(null, 6666,null));

Could you please help me to solve this issue, can anyone tell the correct expression I should use?

4

2 に答える 2

0

あなたは先日同様の質問をしました - この場合、コレクション選択の RESULTS に対してプロジェクションを実行する必要があります。これはより簡単な例です...

    Expression exp = new SpelExpressionParser()
        .parseExpression("(#emps.?[name != null]).![name.toUpperCase()]");

最初の部分 (...) は、基準を満たす要素を選択します。次に、その新しいコレクションに対してプロジェクションを実行します。

于 2012-07-25T12:40:24.253 に答える
0

DateUtil クラスに、次のようなメソッドを追加します。

    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return isSameDay(cal1, cal2);
    }

次に、次のようにします。

StandardEvaluationContext stdContext = new StandardEvaluationContext();
stdContext.setVariable("emps", employees);
stdContext.setVariable("yourTestDate", new Date());
ExpressionParser parser = new SpelExpressionParser();   

String key4 = "#emps.?[T(DateUtil).isSameDate(dateOfBirth, yourTestDate)]" ;
Collection<Object> proj1 = (Collection<Object>) parser.parseExpression(key4).getValue(stdContext);
System.out.println(proj1);   
于 2012-07-25T08:29:33.073 に答える