我必须检查参数是否存在,如果是,则添加'''''他们之间

时间:2016-06-20 09:34:55

标签: java

  • 如果参数parent_Id存在,则将它们与:(parent_Id:leadingNumber)
  • 分开
  • 如果参数不存在,则只有leadingNumber:
  • 如果参数为空,则只有leadingNumber:
  • (空指针)

这是现有代码:

private void mapLeadingNumber() throws Exception
    {
        if ( logger.isDebugEnabled() ) logger.debug( "[" + this.orderId + "] mapLeadingNumber HTS - start" );

    String leadingNumber = "";

    try
    {  
        Product[] products = this.order.mapProducts("LeadingNumber", null, "LeadingNumberAttribute");                                                   
        for (int i=0; i<products.length; i++)
        {
            Product htsProduct = products[i];
            if (htsProduct.getAttribute().length > 0)
            {
                leadingNumber = htsProduct.getAttribute()[0].getAttributeValue();
                this.spcRequest.getSpcRequest().addNewNpstnLeadingNumberAvail().setNpstnLeadingNumberPtr(leadingNumber);
            }       
        }    
        if ( logger.isDebugEnabled() ) logger.debug( "[" + this.orderId + "] mapLeadingNumber HTS mapped value " + leadingNumber );      
    }

    catch (Exception e)
    {
        throw new MappingException("Exception in mapLeadingNumber: " + e.getMessage(), e);               
    }

    if ( logger.isDebugEnabled() ) logger.debug( "[" + this.orderId + "] mapLeadingNumber HTS - end" );

    String parent_Id = "";

    try
    {  
        Product[] products = this.order.mapProducts("Parent_Id", null, "Parent_IdAttribute");                                                   
        for (int i=0; i<products.length; i++)
        {
            Product htsProduct = products[i];
            if (htsProduct.getAttribute().length > 0)
            {
                parent_Id = htsProduct.getAttribute()[0].getAttributeValue();
                this.spcRequest.getSpcRequest().addNewNpstnLeadingNumberAvail().setNpstnLeadingNumberPtr(parent_Id);
            }       
        }    
        if ( logger.isDebugEnabled() ) logger.debug( "[" + this.orderId + "] mapLeadingNumber HTS mapped value " + parent_Id );      
    }

    catch (Exception e)
    {
        throw new MappingException("Exception in mapLeadingNumber: " + e.getMessage(), e);               
    }

    if ( logger.isDebugEnabled() ) logger.debug( "[" + this.orderId + "] mapLeadingNumber HTS - end" );

}

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

public static void main(String [] args) {
    String parentId = "parentId";
    String leadingNumber = "leadingNumber";
    System.out.println(join(parentId, leadingNumber));
    parentId = null;
    System.out.println(join(parentId, leadingNumber));
    parentId = "";
    System.out.println(join(parentId, leadingNumber));
}

public static String join(String parentId, String leadingNumber) {
    List<String> list = (List<String>)Arrays.asList(new String[]{parentId, leadingNumber}).stream().filter(s -> s != null && s != "").collect(Collectors.toList());
    return String.join(":", list);
}