我有两个函数调用 Employee 和 Address DAO 类,我检查员工姓名或地址是否已被使用
为了使其通用以检查和抛出异常,我创建了以下通用函数
CommonUtil.java中的checkOrElseThrow
public static <R, C, T extends Throwable> R checkOrElseThrow(R rtn, C chk, Supplier<? extends T> ex) throws T
{
if (chk != null)
{
throw ex.get();
}
return rtn;
}
上面的通用函数在EmployeeDAO.java和AddressDAO.java中被调用,如下所示
EmployeeDAO.java中的checkAndReturnEmployee
public Employee checkAndReturnEmployee(Employee employee) {
return checkOrElseThrow(
employee,
employee.getAddressName(),
() -> new EntityNotFoundException("Employee already in use for another address"));
}
AddressDAO.java中的checkAndReturnAddress
public Address checkAndReturnAddress(Address address) {
return checkOrElseThrow(
address,
address.getEmployeeName(),
() -> new EntityNotFoundException("Address already in use for another address"));
}
问题
我的解决方案运行良好,但我想知道是否有任何其他更好的方法来重写我编写的通用函数(checkOrElseThrow)