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

com.arextest.diff.compare.feature.ListKeyIndexSelector Maven / Gradle / Ivy

There is a newer version: 0.2.15
Show newest version
package com.arextest.diff.compare.feature;

import com.arextest.diff.handler.log.LogMarker;
import com.arextest.diff.handler.log.register.LogRegister;
import com.arextest.diff.model.compare.CompareContext;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.util.Map;
import java.util.Set;

public class ListKeyIndexSelector implements IndexSelector {

  private Map indexKeysLeft;
  private Map indexKeysRight;
  private CompareContext compareContext;

  public ListKeyIndexSelector() {
  }

  public ListKeyIndexSelector(Map indexKeysLeft,
      Map indexKeysRight, CompareContext compareContext) {
    this.indexKeysLeft = indexKeysLeft;
    this.indexKeysRight = indexKeysRight;
    this.compareContext = compareContext;
  }

  @Override
  public int findCorrespondLeftIndex(int curRightIndex, Set leftComparedIndexes,
      ArrayNode obj1Array, ArrayNode obj2Array) throws Exception {
    int correspondLeftIndex = -1;
    if (indexKeysRight != null) {
      String rightKey = indexKeysRight.get(curRightIndex);

      int cnt = 0;
      boolean alreadyFind = false;
      if (indexKeysLeft != null) {
        for (Map.Entry entry : indexKeysLeft.entrySet()) {
          if (rightKey.equals(entry.getValue())) {
            cnt++;
            if (correspondLeftIndex == -1 && !leftComparedIndexes.contains(entry.getKey())
                && !alreadyFind) {
              correspondLeftIndex = entry.getKey();
              alreadyFind = true;
            }
          }
        }
      }

      if (cnt > 1) {
        LogRegister.register(correspondLeftIndex == -1 ? null : obj1Array.get(correspondLeftIndex),
            obj2Array.get(curRightIndex),
            LogMarker.REPEAT_RIGHT_KEY, compareContext);
      }
    }
    return correspondLeftIndex;
  }

  @Override
  public int findCorrespondRightIndex(int curLeftIndex, Set rightComparedIndexes,
      ArrayNode obj1Array, ArrayNode obj2Array) throws Exception {
    // when indexKeyLef is null, obj1Array must be empty. but the indexKeyLeft also judge null;
    int correspondRightIndex = -1;
    if (indexKeysLeft != null) {
      String leftKey = indexKeysLeft.get(curLeftIndex);

      int cnt = 0;
      boolean alreadyFind = false;
      if (indexKeysRight != null) {
        for (Map.Entry entry : indexKeysRight.entrySet()) {
          if (leftKey.equals(entry.getValue())) {
            cnt++;
            if (correspondRightIndex == -1 && !rightComparedIndexes.contains(entry.getKey())
                && !alreadyFind) {
              correspondRightIndex = entry.getKey();
              alreadyFind = true;
            }
          }
        }


      }

      if (cnt > 1) {
        LogRegister.register(obj1Array.get(curLeftIndex),
            correspondRightIndex == -1 ? null : obj2Array.get(correspondRightIndex),
            LogMarker.REPEAT_LEFT_KEY, compareContext);
      }
    }
    return correspondRightIndex;
  }

  @Override
  public String judgeLeftIndexStandard(int curLeftIndex) {
    if (indexKeysLeft == null) {
      return indexKey(curLeftIndex);
    }
    return indexKeysLeft.get(curLeftIndex);
  }

  @Override
  public String judgeRightIndexStandard(int curRightIndex) {
    if (indexKeysRight == null) {
      return indexKey(curRightIndex);
    }
    return indexKeysRight.get(curRightIndex);
  }

  private String indexKey(int index) {
    return "Index:[" + index + "]";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy