CPRestrictions { public static <T extends Object> Criterion in(String expression, Iterable<T> values) { if (values == null || !values.iterator().hasNext()) { throw new IllegalArgumentException("values is null or empty"); } Iterator<List<T>> blocks = Iterables.partition( values, configProvider.get().getInt(DatabaseConfigFactory.IN_OPERATOR_BLOCK_SIZE) ).iterator(); Criterion criterion = Restrictions.in(expression, blocks.next()); while (blocks.hasNext()) { criterion = Restrictions.or(criterion, Restrictions.in(expression, blocks.next())); } return criterion; } private  CPRestrictions(); static Criterion in(String expression, Iterable<T> values); static Criterion in(String expression, Object[] values); static Criterion like(String expression, String value); static Criterion like(String expression, String value, char escapeChar); static Criterion ilike(String expression, String value); static Criterion ilike(String expression, String value, char escapeChar);  }
@Test public void testIn() { List<String> items = new LinkedList<>(); StringBuilder expected = new StringBuilder("taylor in ("); int inBlockSize = config.getInt(DatabaseConfigFactory.IN_OPERATOR_BLOCK_SIZE); for (int i = 0; i < inBlockSize * 3; ++i) { items.add(String.valueOf(i)); if (items.size() % inBlockSize == 0) { expected.append(i).append(") or taylor in ("); } else { expected.append(i).append(", "); } } expected.setLength(expected.length() - 15); Criterion crit = CPRestrictions.in("taylor", items); LogicalExpression le = (LogicalExpression) crit; assertEquals("or", le.getOp()); assertEquals(expected.toString(), le.toString()); }
@Test public void testInSimple() { List<String> items = new LinkedList<>(); String expected = "swift in ("; int i = 0; int inBlockSize = config.getInt(DatabaseConfigFactory.IN_OPERATOR_BLOCK_SIZE); for (; i < inBlockSize - 1; i++) { expected += i + ", "; items.add("" + i); } expected += i + ")"; items.add("" + i); Criterion crit = CPRestrictions.in("swift", items); InExpression ie = (InExpression) crit; assertEquals(expected, ie.toString()); }
@Test public void testInAsArray() { List<String> items = new LinkedList<>(); StringBuilder expected = new StringBuilder("taylor in ("); int inBlockSize = config.getInt(DatabaseConfigFactory.IN_OPERATOR_BLOCK_SIZE); for (int i = 0; i < inBlockSize * 3; ++i) { items.add(String.valueOf(i)); if (items.size() % inBlockSize == 0) { expected.append(i).append(") or taylor in ("); } else { expected.append(i).append(", "); } } expected.setLength(expected.length() - 15); Criterion crit = CPRestrictions.in("taylor", items.toArray()); LogicalExpression le = (LogicalExpression) crit; assertEquals("or", le.getOp()); assertEquals(expected.toString(), le.toString()); }
@Test public void testInSimpleAsArray() { List<String> items = new LinkedList<>(); String expected = "swift in ("; int i = 0; int inBlockSize = config.getInt(DatabaseConfigFactory.IN_OPERATOR_BLOCK_SIZE); for (; i < inBlockSize - 1; i++) { expected += i + ", "; items.add("" + i); } expected += i + ")"; items.add("" + i); Criterion crit = CPRestrictions.in("swift", items.toArray()); InExpression ie = (InExpression) crit; assertEquals(expected, ie.toString()); }