我怎样才能把这个 if else 语句改成更优雅的东西
public void setAttributes(List<UserAttribute> attributes) {
this.attributes = attributes;
for (UserAttribute attribute : attributes) {
if (attribute.getName().equals(SERVICE_LEVEL_EXTERNAL_ID)) {
featureCode = attribute.getValue();
} else if (attribute.getName().equals(ATTRIBUTE_DESKTOP_USED)) {
quotaUsage = new BigInteger(attribute.getValue());
} else if (StringUtils.equals(ATTRIBUTE_INSERTION_TIME, attribute.getName())) {
insertTime = transformLongToDateSafely(Long.parseLong(attribute.getValue()), MM_dd_yyyy_HHmm, UTC);
} else if (StringUtils.equals(ATTRIBUTE_ARCHIVED_TIME, attribute.getName())) {
archivedTime = transformLongToDateSafely(Long.parseLong(attribute.getValue()), MM_dd_yyyy_HHmm, UTC);
} else if (StringUtils.equals(ATTRIBUTE_LAST_MODIFIED_TIME, attribute.getName())) {
lastModifiedTime = transformLongToDateSafely(Long.parseLong(attribute.getValue()), MM_dd_yyyy_HHmm, UTC);
} else if (StringUtils.equals(CUSTOM_EXTERNAL_ID_1, attribute.getName())) {
dvQuotaUsage = attribute.getValue();
} else if (StringUtils.equals(CUSTOM_EXTERNAL_ID_2, attribute.getName())) {
mmQuotaUsage = attribute.getValue();
} else if (StringUtils.equals(LAST_LOGIN_ACCESS_MOBILE, attribute.getName())) {
lastMobileLogin = DateUtils.transformDateFormatSafely(attribute.getValue(), EEE_MMM_d_HHmmss_zzz_yyyy, MM_dd_yyyy_HHmm, UTC);
} else if (StringUtils.equals(LAST_LOGIN_ACCESS_PC, attribute.getName())) {
lastPCLogin = DateUtils.transformDateFormatSafely(attribute.getValue(), EEE_MMM_d_HHmmss_zzz_yyyy, MM_dd_yyyy_HHmm, UTC);
} else if (StringUtils.equals(LAST_LOGIN_ACCESS_WEB, attribute.getName())) {
lastWebLogin = DateUtils.transformDateFormatSafely(attribute.getValue(), EEE_MMM_d_HHmmss_zzz_yyyy, MM_dd_yyyy_HHmm, UTC);
} else if (StringUtils.equals(ATTRIBUTE_LOCATION, attribute.getName())) {
dcLocator = attribute.getValue();
}
}
}
Sonar 抱怨由于多个 if else 语句导致认知复杂性。我怎样才能使它变得简单和更好。
听起来像
switch
case
是你想要的:
public void setAttributes(List<UserAttribute> attributes) {
this.attributes = attributes;
for (UserAttribute attribute : attributes) {
switch(attribute.getName()){
case SERVICE_LEVEL_EXTERNAL_ID:
featureCode = attribute.getValue();
break:
case ATTRIBUTE_DESKTOP_USED:
quotaUsage = new BigInteger(attribute.getValue());
break;
case ATTRIBUTE_ARCHIVED_TIME:
archivedTime = transformLongToDateSafely(Long.parseLong(attribute.getValue()), MM_dd_yyyy_HHmm, UTC);
break;
case ATTRIBUTE_LAST_MODIFIED_TIME:
lastModifiedTime = transformLongToDateSafely(Long.parseLong(attribute.getValue()), MM_dd_yyyy_HHmm, UTC);
break;
case CUSTOM_EXTERNAL_ID_1:
dvQuotaUsage = attribute.getValue();
break;
case CUSTOM_EXTERNAL_ID_2:
mmQuotaUsage = attribute.getValue();
break;
case LAST_LOGIN_ACCESS_MOBILE:
lastMobileLogin = DateUtils.transformDateFormatSafely(attribute.getValue(), EEE_MMM_d_HHmmss_zzz_yyyy, MM_dd_yyyy_HHmm, UTC);
break;
case LAST_LOGIN_ACCESS_PC:
lastPCLogin = DateUtils.transformDateFormatSafely(attribute.getValue(), EEE_MMM_d_HHmmss_zzz_yyyy, MM_dd_yyyy_HHmm, UTC);
break;
case LAST_LOGIN_ACCESS_WEB
lastWebLogin = DateUtils.transformDateFormatSafely(attribute.getValue(), EEE_MMM_d_HHmmss_zzz_yyyy, MM_dd_yyyy_HHmm, UTC);
break;
case ATTRIBUTE_LOCATION:
dcLocator = attribute.getValue();
break;
default:
//what to do if the attribute name is unhandled until now
}
}
}