Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Latest commit

 

History

History
152 lines (127 loc) · 6.47 KB

1-explain-java.md

File metadata and controls

152 lines (127 loc) · 6.47 KB

Certainly! Let's break down the Refactor class and its components in simple terms.

Overview of the Refactor Class

The Refactor class is the main class that handles processing messages. It uses various other classes to manage data, process messages, and send messages. Here’s a step-by-step breakdown:

  1. Initialization:

    • The Refactor class initializes several objects:
      • dataRepository: Manages data storage.
      • latestMessages: Stores the latest messages based on their type.
      • messageService: Sends messages.
  2. Main Method:

    • The main method creates an instance of Refactor and processes two types of messages:
      • A single data message
      • A multi-data message
  3. Processing Messages:

    • The processMessage method determines whether the message contains single or multi-data.
    • It retrieves the corresponding data from the dataRepository.
    • Depending on the type of data, it parses the message and creates a specific type of message (LocationMessage, SingleDataMessage, or MultiDataMessage).
    • The parsed message is then stored in latestMessages and sent using messageService.

Detailed Breakdown

1. Initialization

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final Logger logger = LoggerFactory.getLogger(Refactor.class);
private final DataRepository dataRepository = new DataRepository();
private final Map<DataType, BaseMessage> latestMessages = new HashMap<>();
private final MessageService messageService = new MessageService();
  • OBJECT_MAPPER: Used for JSON processing.
  • logger: For logging errors and information.
  • dataRepository: Manages data storage.
  • latestMessages: Stores the latest messages based on their type.
  • messageService: Sends messages.

2. Main Method

public static void main(String[] args) {
    Refactor refactor = new Refactor();

    Map<String, Object> singleMap = new HashMap<>(Map.of(
            "key1", "value1",
            "key2", "value2"
    ));
    RefactorMessage singleDataRefactorMessage = new RefactorMessage(singleMap, 0L, null, null, "2025-01-12 00:00", new HashMap<>());
    refactor.processMessage(singleDataRefactorMessage);

    Map<String, Object> multiMap = new HashMap<>(Map.of(
            "key1", "value1",
            "key2", "value2"
    ));
    RefactorMessage multiDataRefactorMessage = new RefactorMessage(null, null, multiMap, 0L, "2025-01-12 00:00", new Object());
    refactor.processMessage(multiDataRefactorMessage);
}
  • Creates an instance of Refactor.
  • Processes a single data message and a multi-data message.

3. Processing Messages

private void processMessage(RefactorMessage refactorMessage) {
    Long id;
    boolean isMultiData = false;
    if (refactorMessage.getSingleData() != null) {
        id = refactorMessage.getSingleDataId();
    } else {
        id = refactorMessage.getMultiDataId();
        isMultiData = true;
    }

    SingleDataDto singleDataDto = null;
    MultiDataDto multiDataDto = null;
    try {
        if (isMultiData) {
            multiDataDto = dataRepository.findMultiData(id);
        } else {
            singleDataDto = dataRepository.findSingleData(id);
        }
    } catch (RepositoryException e) {
        logger.error("Connection with repository failed id: {}", id, e);
        return;
    }

    Instant occurrenceTime = Instant.parse(refactorMessage.getOccurrenceTime().substring(0, refactorMessage.getOccurrenceTime().indexOf("/")));

    BaseMessage baseMessage = null;

    if ((!isMultiData) && singleDataDto != null && singleDataDto.getDataType().equals(DataType.TYPE1.getValue()) && refactorMessage.getResult() instanceof HashMap) {
        baseMessage = parseLocation(occurrenceTime, refactorMessage, singleDataDto);
    } else if (!isMultiData) {
        baseMessage = parseData(occurrenceTime, refactorMessage, singleDataDto);
    } else {
        baseMessage = parseData(occurrenceTime, refactorMessage, multiDataDto);
    }

    if (baseMessage != null) {
        latestMessages.put(baseMessage.getDataType(), baseMessage);
        messageService.sendMessage(baseMessage);
    }
}
  • Determines whether the message contains single or multi-data.
  • Retrieves the corresponding data from the dataRepository.
  • Parses the message and creates a specific type of message (LocationMessage, SingleDataMessage, or MultiDataMessage).
  • Stores the parsed message in latestMessages and sends it using messageService.

4. Parsing Messages

private LocationMessage parseLocation(Instant occurrenceTime, RefactorMessage refactorMessage, SingleDataDto singleDataDto) {
    LocationMessage locationMessage = new LocationMessage();

    GeoJsonObject geoJson;
    try {
        String geoJsonString = OBJECT_MAPPER.writeValueAsString(refactorMessage.getResult());
        geoJson = OBJECT_MAPPER.readValue(geoJsonString, GeoJsonObject.class);
    } catch (JsonProcessingException e) {
        logger.error("Something went wrong with mapping to GeoJson, not processing location", e);
        return null;
    }

    locationMessage.setDataType(singleDataDto.getDataType());
    locationMessage.setOccurrenceTime(occurrenceTime);
    locationMessage.setGeoJson(geoJson);

    return locationMessage;
}

private SingleDataMessage parseData(Instant occurenceTime, RefactorMessage refactorMessage, SingleDataDto singleDataDto) {
    SingleDataMessage singleDataMessage = new SingleDataMessage();
    singleDataMessage.setDataType(singleDataDto.getDataType());
    singleDataMessage.setOccurrenceTime(occurenceTime);
    return singleDataMessage;
}

private MultiDataMessage parseData(Instant occurenceTime, RefactorMessage refactorMessage, MultiDataDto multiDataDto) {
    MultiDataMessage multiDataMessage = new MultiDataMessage();
    multiDataMessage.setDataType(multiDataDto.getDataType());
    multiDataMessage.setOccurrenceTime(occurenceTime);
    return multiDataMessage;
}
  • Parses the message based on its type and creates a specific type of message (LocationMessage, SingleDataMessage, or MultiDataMessage).

Summary

The Refactor class is responsible for processing messages, retrieving data from a repository, parsing messages into specific types, and sending them. It uses various helper classes to manage different aspects of the process, making it modular and easier to understand.

This breakdown should help a junior developer grasp the functionality of the code by breaking it down into smaller, more manageable parts.