/** * Sort the results by the value of the {@link Field}, ordered by values in the array. This is a bit of a hack. * * @param field the Field to order by * @param order values for the specified field, in the order they should appear in the result set */ public static <T> Order byArray(Field<T> field, T[] order) { if (order == null || order.length == 0) { return Order.asc("0"); } CaseBuilder caseBuilder = Function.caseExpr(field); for (int i = 0; i < order.length; i++) { caseBuilder.when(order[i], i); } caseBuilder.elseExpr(order.length); return Order.asc(caseBuilder.end()); }
public void testCaseWhen() { final String PASS = "PASS"; final String FAIL = "FAIL"; Function<String> caseWhen = Function.caseExpr("Hello") .when("a", FAIL) .when("b", FAIL) caseWhen = Function.caseExpr("Hello") .when("a", FAIL) .when("b", FAIL) caseWhen = Function.caseExpr(Field.NULL) .when(null, FAIL) .when(Field.NULL, FAIL) caseWhen = Function.caseExpr(TestModel.LUCKY_NUMBER) .when(0, "zero") .when(7, "default")
/** * Sort the results by the value of the {@link Field}, ordered by values in the array. This is a bit of a hack. * * @param field the Field to order by * @param order values for the specified field, in the order they should appear in the result set */ public static <T> Order byArray(Field<T> field, T[] order) { if (order == null || order.length == 0) { return Order.asc("0"); } CaseBuilder caseBuilder = Function.caseExpr(field); for (int i = 0; i < order.length; i++) { caseBuilder.when(order[i], i); } caseBuilder.elseExpr(order.length); return Order.asc(caseBuilder.end()); }