I am developing a Java application that involves extracting various attributes from work orders and presenting them in different formats. I have implemented a system where I use enums to represent different attributes (such as work order number, client details, vehicle information, etc.) and corresponding value extractors to retrieve the values for these attributes from the work order objects.
// Enums and interfaces representing different attributes and value extractors
public interface WorkOrderValueExtractor {
String extractValue(WorkOrder workOrder);
}
public class ClientValueExtractor implements WorkOrderValueExtractor {
@Override
public String extractValue(WorkOrder workOrder) {
return WOExcelDownloadUtil.prepareStringToDisplay(
workOrder.getFirstNameOfClient(),
workOrder.getLastNameOfClient());
}
}
// Other value extractor implementations for different attributes...
public enum WorkOrderListHeader {
WORK_ORDER_NUMBER("WO#", WorkOrder::getNumber()),
CLIENT("Client", new ClientValueExtractor()),
VEHICLE("Vehicle", new VehicleValueExtractor()),
// other enum constants...
private final String label;
private final WorkOrderValueExtractor extractor;
WorkOrderListHeader(String label, WorkOrderValueExtractor extractor) {
this.label = label;
this.extractor = extractor;
}
public WorkOrderValueExtractor getExtractor() {
return extractor;
}
}
public class WorkOrderListValueExtractor {
private final List<WorkOrderListHeader> workOrderListHeaderList;
public WorkOrderListValueExtractor(List<WorkOrderListHeader> workOrderListHeaderList) {
this.workOrderListHeaderList = workOrderListHeaderList;
}
public List<String> prepareWorkOrderListRow(WorkOrder workOrder) {
return workOrderListHeaderList.stream()
.map(header -> header.getExtractor().extractValue(workOrder))
.collect(Collectors.toList());
}
}
Now the problem is some of these extractors will use dependencies that I want to inject using spring and lombok @RequiredArgsConstructor
but declaring new Extractor()
in the enum will prevent me from doing that. How can I structure my code so that I can achieve this?
I have structured my code as explained above but that is preventing me from using dependency injection.
4