Search code examples
javaweb-servicesgetter-setter

Setting More than 100 Object having different Values


I am working on a legacy project, where we are consuming a SOAP service, that service requires lots of Field Value to be set at our end before we can send it to the JAXB object. At the moment we are doing it via setter method as shown below -

100 such label needs to be set,

    FieldValue field1 = new FieldValue(); // field value
    field1.setFieldName("FieldLabel1");
    field1.setValue(fileValue);
    lstfield.add(field1);

    FieldValue field2 = new FieldValue(); // Action date in format 12-Feb-14
    field2.setFieldName("FieldLabel2");
    field2.setValue(DateUtill.getCurrentdate());
    lstfield.add(field2);

Setting 100 FieldValue objects would be cumbersome for maintainance, if incase the webservice provider changes any label name it would be very hectic to find and change the label name.

Another way to set is using a constructor but that doesn't seem to be right option. I was looking if there is any another way to do the above mentioned task that would be very helpful.


Solution

  • You can create 100 FieldValue objects and add them to a list by using for block:

        List<FieldValue> fieldValueList = new ArrayList<>();
        final int NUM_OF_OBJECTS = 100;
        for (int i = 0; i < NUM_OF_OBJECTS; i++) {
          fieldValueList.add(new FieldValue());
        }
    

    Then you can write a method in FieldValue class to set fieldName and value of objects:

      public void setFieldNameAndValue(String fieldName, Object value) {
        setFieldName(fieldName);
        setValue(value);
      }
    

    and now you can set fieldName and value for each item of the generated list:

        fieldValueList.get(0).setFieldNameAndValue("FieldLabel1", fileValue);
        fieldValueList.get(1).setFieldNameAndValue("FieldLabel2", DateUtill.getCurrentdate());
        // set fieldName and value for remained objects
    

    Another way is using Array instead of List:

        final int NUM_OF_OBJECTS = 100;
        FieldValue[] fieldValues = new FieldValue[NUM_OF_OBJECTS];
        for (int i = 0; i < NUM_OF_OBJECTS; i++) {
          fieldValues[i] = new FieldValue();
        }
    
        fieldValues[0].setFieldNameAndValue("FieldLabel1", fileValue);
        fieldValues[1].setFieldNameAndValue("FieldLabel2", DateUtill.getCurrentdate());
    

    UPDATED with one more solution: I think declaring setFieldNameAndValue in the FieldValue class is not a good way. Please forget that solution :D

        final int NUM_OF_OBJECTS = 100;
        final ArrayList<FieldValue> filedValueList = new ArrayList<>(NUM_OF_OBJECTS);
        addNewFieldValue(filedValueList, "fieldName1", fieldValue);
        addNewFieldValue(filedValueList, "fieldName2", DateUtill.getCurrentdate());
    

    addNewFieldValue can be programmed like this:

      private static void addNewFieldValue(ArrayList<FieldValue> filedValueList, String fieldName, Object value) {
        final FieldValue fieldValue = new FieldValue();
        fieldValue.setFieldName(fieldName);
        fieldValue.setValue(value);
        filedValueList.add(fieldValue);
      }
    

    Hope these lines can help you :)