/** * Converts an {@link Iterable} whose members are automatically down-cast to * a given type. * * <p>All modifications are automatically written to the backing iterator. * Not synchronized. * * @param iterable Backing iterable * @param clazz Class to cast to * @return An iterable whose members are of the desired type. */ public static <E> Iterable<E> cast( final Iterable<? super E> iterable, final Class<E> clazz) { return () -> cast(iterable.iterator(), clazz); }
/** * Converts an {@link Iterable} whose members are automatically down-cast to * a given type. * * <p>All modifications are automatically written to the backing iterator. * Not synchronized. * * @param iterable Backing iterable * @param clazz Class to cast to * @return An iterable whose members are of the desired type. */ public static <E> Iterable<E> cast( final Iterable<? super E> iterable, final Class<E> clazz) { return () -> cast(iterable.iterator(), clazz); }
/** * Concatenates the operands of a call to this operator. */ public static SqlLiteral concatenateOperands(SqlCall call) { final List<SqlNode> operandList = call.getOperandList(); assert operandList.size() > 0; assert operandList.get(0) instanceof SqlLiteral : operandList.get(0).getClass(); return SqlUtil.concatenateLiterals( Util.cast(operandList, SqlLiteral.class)); } }
/** * Concatenates the operands of a call to this operator. */ public static SqlLiteral concatenateOperands(SqlCall call) { final List<SqlNode> operandList = call.getOperandList(); assert operandList.size() > 0; assert operandList.get(0) instanceof SqlLiteral : operandList.get(0).getClass(); return SqlUtil.concatenateLiterals( Util.cast(operandList, SqlLiteral.class)); } }
/** * Tests {@link org.apache.calcite.util.CastingList} and {@link Util#cast}. */ @Test public void testCastingList() { final List<Number> numberList = new ArrayList<>(); numberList.add(1); numberList.add(null); numberList.add(2); List<Integer> integerList = Util.cast(numberList, Integer.class); assertEquals(3, integerList.size()); assertEquals(Integer.valueOf(2), integerList.get(2)); // Nulls are OK. assertNull(integerList.get(1)); // Can update the underlying list. integerList.set(1, 345); assertEquals(Integer.valueOf(345), integerList.get(1)); integerList.set(1, null); assertNull(integerList.get(1)); // Can add a member of the wrong type to the underlying list. numberList.add(3.1415D); assertEquals(4, integerList.size()); // Access a member which is of the wrong type. try { integerList.get(3); fail("expected exception"); } catch (ClassCastException e) { // ok } }
/** * Tests {@link org.apache.calcite.util.CastingList} and {@link Util#cast}. */ @Test public void testCastingList() { final List<Number> numberList = new ArrayList<>(); numberList.add(1); numberList.add(null); numberList.add(2); List<Integer> integerList = Util.cast(numberList, Integer.class); assertEquals(3, integerList.size()); assertEquals(Integer.valueOf(2), integerList.get(2)); // Nulls are OK. assertNull(integerList.get(1)); // Can update the underlying list. integerList.set(1, 345); assertEquals(Integer.valueOf(345), integerList.get(1)); integerList.set(1, null); assertNull(integerList.get(1)); // Can add a member of the wrong type to the underlying list. numberList.add(3.1415D); assertEquals(4, integerList.size()); // Access a member which is of the wrong type. try { integerList.get(3); fail("expected exception"); } catch (ClassCastException e) { // ok } }