对于每个非null值,代码都会正确地将值重置为null

时间:2013-10-23 06:35:29

标签: java

在下面的代码中,我将setOriginatingNumber重置为null,因为对于每个非null值,

if (config.getOriginPhoneNumber() != null) {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
}

它会重置为空白电话号码。这可能会导致多个问题 此方案仅在用户编辑现有电话号码值并更改为字母数字时才存在。

如何确保config.getOriginPhoneNumber()的每个非null值代码设置为空白电话号

public SmsChannelConfig applyTo(
            final SmsChannelConfigRepository repository,
            final UserContext context, final ModuleRepository moduleRepository) {
        final SmsChannelConfig config;
        if (source == null) {
            config = new SmsChannelConfig(context);
        } else {
            config = source;
        }
        super.applyTo(repository, config);
        if (!Strings.isNullOrEmpty(getOriginatorType())
                && "Number".equals(getOriginatorType())) {
            PhoneNumber originphoneNumber = new PhoneNumber();
            originphoneNumber.setNumber(getPhoneNumber());
            originphoneNumber.setCountryCode(getCountryCode());
            config.setOriginPhoneNumber(originphoneNumber);
            config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
            config.setOriginText(null);
        } else {
            config.setOriginText(getOriginator());
            if (config.getOriginPhoneNumber() != null) {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
            }
        }
        config.setOperator(getOperator());
        if (!Strings.isNullOrEmpty(getOperator())) {
            List<ExternalResource> resources = SmsChannelConfigUtil
                    .getSmsProviders(moduleRepository, context);
            for (ExternalResource resource : resources) {
                if (getOperator().equals(resource.getId().getResourceName())) {
                    config.setOperatorName(resource.toString());
                }
            }
        }
        config.setDescription(getDescription());
        config.setName(getName());
        if (repository.query().all().isEmpty()) {
            repository.setDefault(context.getOrganization(), config);
        }
        return config;
    }

仅当getOriginatorType()不等于“Number”时才将originphoneNumber值重置为null,以便它不会产生多个问题

2 个答案:

答案 0 :(得分:2)

替换:

if (config.getOriginPhoneNumber() != null) {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
}

使用:

if (config.getOriginPhoneNumber().matches("^.*[\\w+].*$") {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
}

检查电话号码是否为字母数字。如果您想要测试仅字符,则应将[\\w+]替换为[a-zA-Z]+

答案 1 :(得分:1)

更改if块:

if (!Strings.isNullOrEmpty(getOriginatorType())
                && "Number".equals(getOriginatorType())) {

到此:

if (!Strings.isNullOrEmpty(getOriginatorType()){
    if("Number".equals(getOriginatorType())){
            PhoneNumber originphoneNumber = new PhoneNumber();
            originphoneNumber.setNumber(getPhoneNumber());
            originphoneNumber.setCountryCode(getCountryCode());
            config.setOriginPhoneNumber(originphoneNumber);
            config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
            config.setOriginText(null);
    } else {
            // RESET originphoneNumber value to null
    }
}