All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arextest.diff.handler.keycompute.KeyCompute Maven / Gradle / Ivy

There is a newer version: 0.2.15
Show newest version
package com.arextest.diff.handler.keycompute;

import com.arextest.diff.factory.TaskThreadFactory;
import com.arextest.diff.model.RulesConfig;
import com.arextest.diff.model.enumeration.Constant;
import com.arextest.diff.model.key.KeyComputeResponse;
import com.arextest.diff.model.key.ListSortEntity;
import com.arextest.diff.model.key.ReferenceEntity;
import com.arextest.diff.model.log.NodeEntity;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KeyCompute {

  private static final Logger LOGGER = LoggerFactory.getLogger(KeyCompute.class);

  public KeyComputeResponse doHandler(RulesConfig rulesConfig, Object baseObj, Object testObj)
      throws ExecutionException, InterruptedException, TimeoutException {

    List allReferenceEntities = rulesConfig.getReferenceEntities();
    List listSortConfig = rulesConfig.getListSortEntities();

    CompletableFuture, HashMap>> future1 =
        CompletableFuture.supplyAsync(() -> {
          ListKeyProcess keyProcessLeft = new ListKeyProcess(allReferenceEntities, listSortConfig);
          keyProcessLeft.computeAllListKey(baseObj);
          return keyProcessLeft.getListIndexKeys();
        }, TaskThreadFactory.jsonObjectThreadPool);

    CompletableFuture, HashMap>> future2 =
        CompletableFuture.supplyAsync(() -> {
          ListKeyProcess keyProcessLeft = new ListKeyProcess(allReferenceEntities, listSortConfig);
          keyProcessLeft.computeAllListKey(testObj);
          return keyProcessLeft.getListIndexKeys();
        }, TaskThreadFactory.jsonObjectThreadPool);

    CompletableFuture combinedFuture = CompletableFuture.allOf(future1, future2);
    try {
      combinedFuture.get(Constant.KEY_COMPUTE_WAIT_MINUTES_TIME, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
      LOGGER.error("KeyCompute doHandler TimeoutException", e);
      throw e;
    }

    HashMap, HashMap> listIndexKeysLeft = future1.get();
    HashMap, HashMap> listIndexKeysRight = future2.get();

    KeyComputeResponse response = new KeyComputeResponse();
    response.setAllReferenceEntities(allReferenceEntities);
    response.setListSortEntities(listSortConfig);
    response.setListIndexKeysLeft(listIndexKeysLeft);
    response.setListIndexKeysRight(listIndexKeysRight);
    return response;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy