0

I defined many data classes as needed for the operation, eg. DataForMSDForAdvanceReqeust. Each data class is like below:

@Data
class DataForMSDForAdvanceReqeust implements Serializable {
    private final String id;
    private final String advanceRequestValue;
    private final String advanceRequestShowText;

    public DataForMSDForAdvanceReqeust(String id, String advanceRequestValue, String advanceRequestShowText) {
        this.id = id;
        this.advanceRequestValue = advanceRequestValue;
        this.advanceRequestShowText = advanceRequestShowText;
    }
}

Then in the following functionfetchDataMSDForAdvanceRequest, I will be using this above DataForMSDForAdvanceReqeust in creating return values like List<DataForMSDForAdvanceReqeust> result = new ArrayList<>(); and somewhere else in the following code:

@ResponseBody
@RequestMapping(value = "/fetchDataMSDForAdvanceRequest", method = RequestMethod.POST)
public List<DataForMSDForAdvanceReqeust> fetchDataMSDForAdvanceRequest(
        @RequestBody @NonNull MasterSearchRequestParam param) {

    SearchFilterForAdvanceRequestVo searchFilter = SearchFilterForAdvanceRequestVo.of(new HashMap<>());
    List<MasterSearchSimpleResult> tmp = new ArrayList<>();
    tmp = availableAdvanceRequestSearchService.dbsearch(true, searchFilter);

    List<DataForMSDForAdvanceReqeust> result = new ArrayList<>();
    int count = 0;
    String inputString = param.getInputKeyword();
    if (inputString.length() != 0) {
        for (MasterSearchSimpleResult searchR : tmp) {
            if (searchR.getShowText().contains(inputString)) {
                result.add(new DataForMSDForAdvanceReqeust(Integer.toString(++count), searchR.getValue(), searchR
                        .getShowText()));
            }
        }
    } else {
        for (MasterSearchSimpleResult searchR : tmp) {
            result.add(new DataForMSDForAdvanceReqeust(Integer.toString(++count), searchR.getValue(), searchR
                    .getShowText()));
        }
    }
    return result;
}

I want to seperate this part of code out, as for many other fetch data function similiar to fetchDataMSDForAdvanceRequest above, the below part is almost identical, except the return type is different:

    List<DataForMSDForAdvanceReqeust> result = new ArrayList<>();
    int count = 0;
    String inputString = param.getInputKeyword();
    if (inputString.length() != 0) {
        for (MasterSearchSimpleResult searchR : tmp) {
            if (searchR.getShowText().contains(inputString)) {
                result.add(new DataForMSDForAdvanceReqeust(Integer.toString(++count), searchR.getValue(), searchR
                        .getShowText()));
            }
        }
    } else {
        for (MasterSearchSimpleResult searchR : tmp) {
            result.add(new DataForMSDForAdvanceReqeust(Integer.toString(++count), searchR.getValue(), searchR
                    .getShowText()));
        }
    }
    return result;

I want to write a function that will takes DataForMSDForAdvanceReqeust as the parameter, as there are many different @Data classes available. It looks like a Generic class, but I am not sure how to carry it out if using that. I am also looking for other methods, if possible.

Do you have any idea about passing a data class as the parameter of another function?

Max
  • 49
  • 1
  • 8
  • What package is `@Data` coming from? Lombok? – Michael May 24 '18 at 08:31
  • @Michael you are right – Max May 24 '18 at 08:43
  • In which case, the only thing common between @Data objects is that they are all objects which have various getters and setters. There is no way to use generics to constrain a class based on its annotations. Annotations are meta data. You could consider using a [marker interface](https://stackoverflow.com/questions/25850328/marker-interfaces-in-java). – Michael May 24 '18 at 08:45

0 Answers0