public List<Employee> getEmployeesBySurname(String surname)
Fortunately, java.util.Collections is a big help here in that it provides a bunch of methods that return an empty collection type. These comprise:
public static final <T> Set<T> emptySet()
public static final <K,V> Map<K,V> emptyMap()
public static final <T> List<T> emptyList()
In our employee details scenario, we need to change the EmployeeDAO so that it returns an empty list if no data is available.
public List<Employee> getEmployeesBySurname(String surname) {
List<Employee> employees = dbHelper.getEmployeesBySurname(surname);
if (isNull(employees)) {
employees = Collections.emptyList();
}
return employees;
}
private boolean isNull(Object o) {
return o == null;
}
Job Done!
No comments:
Post a comment