@ Trivial private final Map < String , Object > appendStateComparison ( StringBuilder sb , TaskState state , boolean inState ) { Map < String , Object > params = new HashMap < String , Object > ( ) ; switch ( state ) { case SCHEDULED : sb . append ( "t.STATES" ) . append ( inState ? "<" : ">=" ) . append ( ":s" ) ; params . put ( "s" , TaskState . SUSPENDED . bit ) ; break ; case SUSPENDED : sb . append ( inState ? "t.STATES>=:s1 AND t.STATES<:s2" : "(t.STATES<:s1 OR t.STATES>=:s2)" ) ; params . put ( "s1" , TaskState . SUSPENDED . bit ) ; params . put ( "s2" , TaskState . ENDED . bit ) ; break ; case ENDED : case CANCELED : sb . append ( "t.STATES" ) . append ( inState ? ">=" : "<" ) . append ( ":s" ) ; params . put ( "s" , state . bit ) ; break ; case SUCCESSFUL : sb . append ( inState ? "t.STATES>=:s1 AND t.STATES<:s2" : "(t.STATES<:s1 OR t.STATES>=:s2)" ) ; params . put ( "s1" , TaskState . SUCCESSFUL . bit ) ; params . put ( "s2" , TaskState . FAILURE_LIMIT_REACHED . bit ) ; break ; case FAILURE_LIMIT_REACHED : sb . append ( inState ? "t.STATES>=:s1 AND t.STATES<:s2" : "(t.STATES<:s1 OR t.STATES>=:s2)" ) ; params . put ( "s1" , TaskState . FAILURE_LIMIT_REACHED . bit ) ; params . put ( "s2" , TaskState . CANCELED . bit ) ; break ; default : sb . append ( "MOD(t.STATES,:s*2)" ) . append ( inState ? ">=" : "<" ) . append ( ":s" ) ; params . put ( "s" , state . bit ) ; } return params ; }
Appends conditions to a JPA query to filter based on the presence or absence of the specified state . This method optimizes to avoid the MOD function if possible .