/** * Return a unique result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 instance found. * @param results the result Collection (can be {@code null}) * @return the unique result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ @Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Throws an exception if 0 or more than 1 instance found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique result object * @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 * @see org.springframework.util.CollectionUtils#hasUniqueObject */ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 instance found. * @param results the result Collection (can be {@code null}) * @return the unique result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ @Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Throws an exception if 0 or more than 1 instance found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique result object * @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 * @see org.springframework.util.CollectionUtils#hasUniqueObject */ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
@Test public void testHasUniqueObject() { List<String> list = new LinkedList<>(); list.add("myElement"); list.add("myOtherElement"); assertFalse(CollectionUtils.hasUniqueObject(list)); list = new LinkedList<>(); list.add("myElement"); assertTrue(CollectionUtils.hasUniqueObject(list)); list = new LinkedList<>(); list.add("myElement"); list.add(null); assertFalse(CollectionUtils.hasUniqueObject(list)); list = new LinkedList<>(); list.add(null); list.add("myElement"); assertFalse(CollectionUtils.hasUniqueObject(list)); list = new LinkedList<>(); list.add(null); list.add(null); assertTrue(CollectionUtils.hasUniqueObject(list)); list = new LinkedList<>(); list.add(null); assertTrue(CollectionUtils.hasUniqueObject(list)); list = new LinkedList<>(); assertFalse(CollectionUtils.hasUniqueObject(list)); }
/** * Return a unique result object from the given Collection. * Throws an exception if 0 or more than 1 result objects found. * @param results the result Collection (can be <code>null</code>) * @return the unique result object * @throws IncorrectResultSizeDataAccessException if more than one * result object or none at all has been found in the given Collection */ public static Object requiredUniqueResult(Collection results) throws IncorrectResultSizeDataAccessException { int size = (results != null ? results.size() : 0); if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, size); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 instance found. * @param results the result Collection (can be {@code null}) * @return the unique result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ @Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * Returns null if 0 result objects found; throws an exception * if more than 1 found. * @param results the result Collection (can be <code>null</code>) * @return the unique result object, or <code>null</code> if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection */ public static Object uniqueResult(Collection results) throws IncorrectResultSizeDataAccessException { int size = (results != null ? results.size() : 0); if (size == 0) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, size); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Returns {@code null} if 0 result objects found; * throws an exception if more than 1 instance found. * @param results the result Collection (can be {@code null}) * @return the unique result object, or {@code null} if none * @throws IncorrectResultSizeDataAccessException if more than one * result object has been found in the given Collection * @see org.springframework.util.CollectionUtils#hasUniqueObject */ @Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { return null; } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Throws an exception if 0 or more than 1 instance found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique result object * @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 * @see org.springframework.util.CollectionUtils#hasUniqueObject */ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
/** * Return a unique result object from the given Collection. * <p>Throws an exception if 0 or more than 1 instance found. * @param results the result Collection (can be {@code null} * but is not expected to contain {@code null} elements) * @return the unique result object * @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 * @see org.springframework.util.CollectionUtils#hasUniqueObject */ public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException { if (CollectionUtils.isEmpty(results)) { throw new EmptyResultDataAccessException(1); } if (!CollectionUtils.hasUniqueObject(results)) { throw new IncorrectResultSizeDataAccessException(1, results.size()); } return results.iterator().next(); }
@Override public User findByUserAccount(String userAccount) { UserExample example = new UserExample(); example.createCriteria().andAccountEqualTo(userAccount); List<User> userList = userMapper.selectByExample(example); if (CollectionUtils.hasUniqueObject(userList)) { return userList.get(0); } else { return null; } }
@Override public Set<Role> findRoles(String account) { UserExample example = new UserExample(); example.createCriteria().andAccountEqualTo(account); List<User> users = userMapper.selectByExample(example); Set<Role> roles = new HashSet<>(); if (CollectionUtils.hasUniqueObject(users)) { User user = users.get(0); UserRoleExample secUserRoleExample = new UserRoleExample(); secUserRoleExample.createCriteria().andUserIdEqualTo(user.getId()); List<UserRole> userRoles = userRoleMapper.selectByExample(secUserRoleExample); for (UserRole userRole : userRoles) { roles.add(roleMapper.selectByPrimaryKey(userRole.getRoleId())); } } return roles; }