朋友
我有两个重载方法,它们抛出相同的ServiceException
private ModResponse updateNDelTerms(GlobalTermDeleteType item, boolean isGlobal)
{
StdTerm stdTerm = getTerm(item.getstdTermId());
if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
{
throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
}
if (stdTerm.isGlobal() && !isGlobal)
{
throw new ServiceException(
"Global Term can not be updated: incorrect URL.",
CANNOT_UPDATE_GLOBAL_TERM, INCORRECT_URL);
}
if (stdTerm.isLocked() != null && stdTerm.isLocked())
{
throw new ServiceException("Global Term can not be updated: stdTerm is locked.",
CANNOT_UPDATE_GLOBAL_TERM, TERM_LOCKED);
}
return updateNDel(item, stdTerm);
}
第二种方法是
public ItemResponse<List<stdTermItemType>> copyTerm(
BigInteger stdTermId, boolean isGlobal,
boolean isFalse)
{
StdTerm stdTerm = getTerm(stdTermId);
if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
{
throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
}
if (stdTerm.isGlobal() && !isGlobal)
{
throw new ServiceException(
"Global Term can not be updated: incorrect URL.",
CANNOT_COPY_GLOBAL_TERM, INCORRECT_URL);
}
if (stdTerm.isLocked() != null && stdTerm.isLocked())
{
throw new ServiceException("Rate sheet term can not be updated: stdTerm is locked.",
CANNOT_COPY_GLOBAL_TERM, TERM_LOCKED);
}
return copyGlobleTerm(stdTerm, pasteTermObj, isFalse);
}
我正在考虑重构这两个方法并使用Extract公共代码到一个方法, 但是由于 CANNOT_COPY_GLOBAL_TERM 和 CANNOT_UPDATE_GLOBAL_TERM 的不同,这两种方法都是唯一的 我无法实现Extract to Method重构。 请建议。
答案 0 :(得分:1)
将不同的内容传递给提取的方法。
如果您在JDK 8上运行,也可以将这些方法转换为lambdas并传入它们。使用更实用的样式。
答案 1 :(得分:1)
private StdTerm retrieveStdTerm(BigInteger stdTermId, boolean isGlobal, String errorTerm)
{
StdTerm stdTerm = getTerm(BigInteger stdTermId);
if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
{
throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
}
if (stdTerm.isGlobal() && !isGlobal)
{
throw new ServiceException(
"Global Term can not be updated: incorrect URL.",
errorTerm, INCORRECT_URL);
}
if (stdTerm.isLocked() != null && stdTerm.isLocked())
{
throw new ServiceException("Global Term can not be updated: stdTerm is locked.",
errorTerm, TERM_LOCKED);
}
return stdTerm;
}
private ModResponse updateNDelTerms(GlobalTermDeleteType item, boolean isGlobal)
{
StdTerm stdTerm = retrieveStdTerm(item.getstdTermId(), isGlobal,
CANNOT_DELETE_GLOBAL_TERM);
return updateNDel(item, stdTerm);
}
public ItemResponse<List<stdTermItemType>> copyTerm(BigInteger stdTermId, boolean isGlobal,
boolean isFalse)
{
StdTerm stdTerm = retrieveStdTerm(stdTermId, isGlobal, CANNOT_COPY_GLOBAL_TERM);
return copyGlobleTerm(stdTerm, pasteTermObj, isFalse);
}