private int compareRefs()

in uimaj-core/src/main/java/org/apache/uima/cas/impl/CasCompare.java [1782:1903]


  private int compareRefs(TOP rfs1, TOP rfs2, TypeImpl callerTi, FeatureImpl callerFi) {
    if (inSortContext && isTypeMapping) {
      if (isSrcCas) {
        if (rfs1 != null && typeMapper.mapTypeSrc2Tgt(rfs1._getTypeImpl()) == null) {
          rfs1 = null;
        }
        if (rfs2 != null && typeMapper.mapTypeSrc2Tgt(rfs2._getTypeImpl()) == null) {
          rfs2 = null;
        }
      } else {
        if (rfs1 != null && typeMapper.mapTypeTgt2Src(rfs1._getTypeImpl()) == null) {
          rfs1 = null;
        }
        if (rfs2 != null && typeMapper.mapTypeTgt2Src(rfs2._getTypeImpl()) == null) {
          rfs2 = null;
        }
      }
    }

    if (rfs1 == null) {
      if (rfs2 != null) {
        if (!inSortContext && isTypeMapping
                && typeMapper.mapTypeTgt2Src(rfs2._getTypeImpl()) == null) {
          return 0;
        } else {
          if (!inSortContext && IS_DEBUG_STOP_ON_MISCOMPARE && !inSortContext) {
            System.out.println("debug stop rfs1 is null, rfs2 is not");
          }
          return -1;
        }
        // return (!inSortContext && isTypeMapping &&
        // typeMapper.mapTypeTgt2Src(rfs2._getTypeImpl()) == null)
        // ? 0 // no source type for this target type, treat as equal
        // : -1;
      }
      return 0; // both are null. no loops in ref chain possible
    }

    // rfs1 != null at this point

    if (rfs2 == null) {
      if (!inSortContext && isTypeMapping
              && typeMapper.mapTypeSrc2Tgt(rfs1._getTypeImpl()) == null) {
        return 0;
      } else {
        if (!inSortContext && IS_DEBUG_STOP_ON_MISCOMPARE && !inSortContext) {
          System.out.println("debug stop rfs1 is not null rfs2 is null");
        }
        return 1;
      }
      // return (!inSortContext && isTypeMapping &&
      // typeMapper.mapTypeSrc2Tgt(rfs1._getTypeImpl()) == null)
      // ? 0 // no target type for this target type, treat as equal
      // : 1;
    }

    if (rfs1 == rfs2) {
      // only for inSortContext
      return 0;
    }

    // next commented out to enable finding length of chain
    // if (inSortContext && rfs1._id == rfs2._id) {
    // return compareRefResult(rfs1, rfs2);
    // }

    // both are not null
    // do a recursive check
    // debug
    // if (rfs1._id == 1103 && ! inSortContext) {
    // System.out.println("debug stop 1103");
    // }
    Pair<TOP, TOP> refs = new Pair<>(rfs1, rfs2);
    Integer prevComp = prevCompare.get(refs);
    if (prevComp != null) {
      int v = prevComp;
      if (v == 0) {
        v = compareRefResult(rfs1, rfs2); // stop recursion, return based on loops
        if (v != 0 && !inSortContext) {
          mismatchFs(rfs1, rfs2, callerTi, callerFi);
        }
        return v;
      } else {
        if (!inSortContext && IS_DEBUG_STOP_ON_MISCOMPARE) {
          System.out.println("debug stop");
        }
        return v;
      }
      // return (v == 0)
      // ? compareRefResult(rfs1, rfs2) // stop recursion, return based on loops
      // : v;
    }
    prevCompare.put(refs, 0); // preset in case recursion compares this again

    // need special handling to detect cycles lengths that are back to the original
    if (prev1.prevCompareTop != null) {
      prev1.addTop();
      prev2.addTop();
    }

    prev1.add(rfs1);
    prev2.add(rfs2);
    assert !prev1.fsList.isEmpty();
    // TOP savedFs1 = fs1;
    // TOP savedFs2 = fs2;

    // fs1 = rfs1;
    // fs2 = rfs2;
    try {
      int v = compareFss(rfs1, rfs2, callerTi, callerFi);
      if (v != 0) {
        prevCompare.put(refs, v);
      }
      return v;
    } finally {
      prev1.rmvLast(rfs1);
      prev2.rmvLast(rfs2);

      // fs1 = savedFs1;
      // fs2 = savedFs2;
    }
  }