Java 编写checkorelsetrow泛型函数的更好方法
我对Employee和Address DAO类有两个函数调用,在这里我检查是否已经使用了Employee名称或地址 为了使检查和抛出异常变得通用,我创建了以下通用函数 CommonUtil.java中的checkorelsetrowJava 编写checkorelsetrow泛型函数的更好方法,java,lambda,java-8,throwable,supplier,Java,Lambda,Java 8,Throwable,Supplier,我对Employee和Address DAO类有两个函数调用,在这里我检查是否已经使用了Employee名称或地址 为了使检查和抛出异常变得通用,我创建了以下通用函数 CommonUtil.java中的checkorelsetrow public static <R, C, T extends Throwable> R checkOrElseThrow(R rtn, C chk, Supplier<? extends T> ex) throws T { if (c
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;
}
public Employee checkAndReturnEmployee(Employee employee) {
return checkOrElseThrow(
employee,
employee.getAddressName(),
() -> new EntityNotFoundException("Employee already in use for another address"));
}
public Address checkAndReturnAddress(Address address) {
return checkOrElseThrow(
address,
address.getEmployeeName(),
() -> new EntityNotFoundException("Address already in use for another address"));
}
在AddressDAO.java中检查并返回地址
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;
}
public Employee checkAndReturnEmployee(Employee employee) {
return checkOrElseThrow(
employee,
employee.getAddressName(),
() -> new EntityNotFoundException("Employee already in use for another address"));
}
public Address checkAndReturnAddress(Address address) {
return checkOrElseThrow(
address,
address.getEmployeeName(),
() -> new EntityNotFoundException("Address already in use for another address"));
}
问题:
我的解决方案运行良好,但我想知道是否有其他更好的方法重写我编写的泛型函数(checkorelsetrow)编写此函数的最佳方法是不重写
public Employee checkAndReturnEmployee(Employee employee) {
if (employee.getAddressName() == null) {
throw new EntityNotFoundException("Employee already in use for another address"));
}
return employee;
}
上面的代码同样简短,但可读性要高得多。更清楚的是条件是什么,以及不满足条件时会发生什么
您的自定义函数只用于尝试为Java创建一种新语法,这种语法别人不会理解,您可能很快就会忘记。考虑使用
Java.util.Optional
,因为您试图实现的行为已经存在。我发现它比if(smth!=null)检查要优雅得多
Optional.ofNullable(employee)
.map(Employee::getAddressName)
.orElseThrow(() -> new EntityNotFoundException("Employee already in use for another address");
一般来说,我更喜欢
可选
,主要是因为如果还需要实体
的空检查,则可能会嵌套多个if
或链接条件(本问题的情况并非如此)。然后您需要类似于if(entity!=null&&entity.getAddress()==null){throw…}
的东西,这比带有可选项的链接版本更难看、可读性更低。当然,后一种说法也有一点句法上的味道。因为问题更多地围绕着通用实现,所以您可以修改现有的实现,以使用谓词来测试任何标准,并将其计算为:
public <R, T extends Throwable> R checkOrElseThrow(R returnValue, Predicate<R> successCriteria,
Supplier<? extends T> ex) throws T {
if (successCriteria.test(returnValue)) {
return returnValue;
}
throw ex.get();
}
别忘了检查一下员工nullability@mstzn原始代码也没有检查。How abtOptional.ofNullable(carDO)。filter(e->e.getAddressName()!=null)。orelsetrow(()->new EntityNotFoundException(“员工已在使用另一个地址”)代码>与简单的常规Java相比,这会给您带来什么?由于可选的.ofNullable(employee).filter(e->e.getAddressName()!=null)
,它会对employee
和e.getAddressName
进行null检查,并在单行中返回employee
对象employee.getAddressName()!=空
检查“地址已被使用”?您没有检查数据库以查看该地址是否已被其他员工使用。由于我们正在检查函数中的null,因此现在似乎不需要使用泛型函数。How abtOptional.ofNullable(carDO).filter(e->e.getAddressName()!=null).orelsThrow(()->newEntityNotFoundException(“员工已在使用另一个地址”)
@AlexMan当您可以执行公共员工检查和返回员工(员工员工)抛出EntityNotFoundException{return CheckOrelsThrow(员工,emp->emp.getAddressName().equals(“成功”),()->new EntityNotFoundException时,泛型方法的实际值将发生变化(“员工已在使用另一个地址”));}
的同时检查当前的情况。