Java Code Examples for org.springframework.util.CollectionUtils#hasUniqueObject()

The following examples show how to use org.springframework.util.CollectionUtils#hasUniqueObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataAccessUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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();
}
 
Example 2
Source File: DataAccessUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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();
}
 
Example 3
Source File: DataAccessUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 */
public static <T> T uniqueResult(Collection<T> 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();
}
 
Example 4
Source File: DataAccessUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 */
public static <T> T uniqueResult(Collection<T> 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();
}
 
Example 5
Source File: ContactDaoImpl.java    From resteasy-examples with Apache License 2.0 4 votes vote down vote up
private Contact findSingle(String hql, String paramName, Object value) {
	List<Contact> results = (List<Contact>) getHibernateTemplate().findByNamedParam(hql,
			paramName, value);
	return CollectionUtils.hasUniqueObject(results) ? results.get(0) : null;
}
 
Example 6
Source File: DataAccessUtils.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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();
}
 
Example 7
Source File: DataAccessUtils.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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();
}
 
Example 8
Source File: DataAccessUtils.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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})
 * @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(Collection<T> results) throws IncorrectResultSizeDataAccessException {
	int size = (results != null ? results.size() : 0);
	if (size == 0) {
		throw new EmptyResultDataAccessException(1);
	}
	if (!CollectionUtils.hasUniqueObject(results)) {
		throw new IncorrectResultSizeDataAccessException(1, size);
	}
	return results.iterator().next();
}
 
Example 9
Source File: DataAccessUtils.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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})
 * @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(Collection<T> results) throws IncorrectResultSizeDataAccessException {
	int size = (results != null ? results.size() : 0);
	if (size == 0) {
		throw new EmptyResultDataAccessException(1);
	}
	if (!CollectionUtils.hasUniqueObject(results)) {
		throw new IncorrectResultSizeDataAccessException(1, size);
	}
	return results.iterator().next();
}