/** * Return a unique int result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to an int. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique int result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to an int */ public static int intResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).intValue(); }
/** * Return a unique long result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to a long. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique long result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to a long */ public static long longResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).longValue(); }
/** * Return a unique long result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to a long. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique long result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to a long */ public static long longResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).longValue(); }
/** * Return a unique int result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to an int. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique int result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to an int */ public static int intResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).intValue(); }
@Test public void withLong() { Collection<Long> col = new HashSet<>(1); col.add(5L); assertEquals(Long.valueOf(5L), DataAccessUtils.uniqueResult(col)); assertEquals(Long.valueOf(5L), DataAccessUtils.requiredUniqueResult(col)); assertEquals(Long.valueOf(5L), DataAccessUtils.objectResult(col, Long.class)); assertEquals("5", DataAccessUtils.objectResult(col, String.class)); assertEquals(5, DataAccessUtils.intResult(col)); assertEquals(5, DataAccessUtils.longResult(col)); }
@Test public void withSameIntegerInstanceTwice() { Integer i = 5; Collection<Integer> col = new ArrayList<>(1); col.add(i); col.add(i); assertEquals(Integer.valueOf(5), DataAccessUtils.uniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.requiredUniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.objectResult(col, Integer.class)); assertEquals("5", DataAccessUtils.objectResult(col, String.class)); assertEquals(5, DataAccessUtils.intResult(col)); assertEquals(5, DataAccessUtils.longResult(col)); }
@Test public void withInteger() { Collection<Integer> col = new HashSet<>(1); col.add(5); assertEquals(Integer.valueOf(5), DataAccessUtils.uniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.requiredUniqueResult(col)); assertEquals(Integer.valueOf(5), DataAccessUtils.objectResult(col, Integer.class)); assertEquals("5", DataAccessUtils.objectResult(col, String.class)); assertEquals(5, DataAccessUtils.intResult(col)); assertEquals(5, DataAccessUtils.longResult(col)); }
@Test public void withDate() { Date date = new Date(); Collection<Date> col = new HashSet<>(1); col.add(date); assertEquals(date, DataAccessUtils.uniqueResult(col)); assertEquals(date, DataAccessUtils.requiredUniqueResult(col)); assertEquals(date, DataAccessUtils.objectResult(col, Date.class)); assertEquals(date.toString(), DataAccessUtils.objectResult(col, String.class)); try { DataAccessUtils.intResult(col); fail("Should have thrown TypeMismatchDataAccessException"); } catch (TypeMismatchDataAccessException ex) { // expected } try { DataAccessUtils.longResult(col); fail("Should have thrown TypeMismatchDataAccessException"); } catch (TypeMismatchDataAccessException ex) { // expected } }
@Test public void withString() { Collection<String> col = new HashSet<>(1); col.add("test1"); assertEquals("test1", DataAccessUtils.uniqueResult(col)); assertEquals("test1", DataAccessUtils.requiredUniqueResult(col)); assertEquals("test1", DataAccessUtils.objectResult(col, String.class)); try { DataAccessUtils.intResult(col); fail("Should have thrown TypeMismatchDataAccessException"); } catch (TypeMismatchDataAccessException ex) { // expected } try { DataAccessUtils.longResult(col); fail("Should have thrown TypeMismatchDataAccessException"); } catch (TypeMismatchDataAccessException ex) { // expected } }
DataAccessUtils.objectResult(col, String.class); fail("Should have thrown IncorrectResultSizeDataAccessException");
DataAccessUtils.objectResult(col, String.class); fail("Should have thrown IncorrectResultSizeDataAccessException");
/** * Return a unique long result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertable to a long. * @param results the result Collection (can be <code>null</code>) * @return the unique long result * @throws IncorrectResultSizeDataAccessException if more than one * result object or none at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertable to a long */ public static long longResult(Collection results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return ((Number) objectResult(results, Number.class)).longValue(); }
/** * Return a unique int result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertable to an int. * @param results the result Collection (can be <code>null</code>) * @return the unique int result * @throws IncorrectResultSizeDataAccessException if more than one * result object or none at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertable to an int */ public static int intResult(Collection results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return ((Number) objectResult(results, Number.class)).intValue(); }
/** * Return a unique int result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to an int. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique int result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to an int */ public static int intResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).intValue(); }
/** * Return a unique int result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to an int. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique int result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to an int */ public static int intResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).intValue(); }
/** * Return a unique long result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to a long. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique long result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to a long */ public static long longResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).longValue(); }
/** * Return a unique long result from the given Collection. * Throws an exception if 0 or more than 1 result objects found, * of if the unique result object is not convertible to a long. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique long result * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @throws EmptyResultDataAccessException if no result object * at all has been found in the given Collection * @throws TypeMismatchDataAccessException if the unique object * in the collection is not convertible to a long */ public static long longResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { return objectResult(results, Number.class).longValue(); }