static CodeBlock getCompareStatement()

in litho-processor/src/main/java/com/facebook/litho/specmodels/generator/ComponentBodyGenerator.java [897:1010]


  static CodeBlock getCompareStatement(
      String methodName,
      MethodParamModel field,
      String firstComparator,
      String secondComparator,
      EnumSet<RunMode> runMode) {
    final CodeBlock.Builder codeBlock = CodeBlock.builder();

    @Comparable.Type int comparableType = getComparableType(field, runMode);
    switch (comparableType) {
      case Comparable.FLOAT:
        codeBlock
            .beginControlFlow("if (Float.compare($L, $L) != 0)", firstComparator, secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.DOUBLE:
        codeBlock
            .beginControlFlow("if (Double.compare($L, $L) != 0)", firstComparator, secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.ARRAY:
        codeBlock
            .beginControlFlow(
                "if (!$T.equals($L, $L))", Arrays.class, firstComparator, secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.PRIMITIVE:
        codeBlock
            .beginControlFlow("if ($L != $L)", firstComparator, secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.COMPARABLE_DRAWABLE:
        codeBlock
            .beginControlFlow("if (!$L.isEquivalantTo($L))", firstComparator, secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.COLLECTION_COMPLEVEL_0:
        codeBlock
            .beginControlFlow(
                "if ($L != null ? !$L.equals($L) : $L != null)",
                firstComparator,
                firstComparator,
                secondComparator,
                secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.COLLECTION_COMPLEVEL_1:
      case Comparable.COLLECTION_COMPLEVEL_2:
      case Comparable.COLLECTION_COMPLEVEL_3:
      case Comparable.COLLECTION_COMPLEVEL_4:
        // N.B. This relies on the IntDef to be in increasing order.
        int level = comparableType - Comparable.COLLECTION_COMPLEVEL_0;
        codeBlock
            .beginControlFlow("if ($L != null)", firstComparator)
            .beginControlFlow(
                "if ($L == null || $L.size() != $L.size())",
                secondComparator,
                firstComparator,
                secondComparator)
            .addStatement("return false")
            .endControlFlow()
            .add(
                getComponentCollectionCompareStatement(
                    level,
                    (DeclaredTypeSpec) field.getTypeSpec(),
                    firstComparator,
                    secondComparator))
            .nextControlFlow("else if ($L != null)", secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.COMPONENT:
      case Comparable.SECTION:
      case Comparable.EVENT_HANDLER:
      case Comparable.EVENT_HANDLER_IN_PARAMETERIZED_TYPE:
        codeBlock
            .beginControlFlow(
                "if ($L != null ? !$L.isEquivalentTo($L) : $L != null)",
                firstComparator,
                firstComparator,
                secondComparator,
                secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;

      case Comparable.OTHER:
        codeBlock
            .beginControlFlow(
                "if ($L != null ? !$L.equals($L) : $L != null)",
                firstComparator,
                firstComparator,
                secondComparator,
                secondComparator)
            .addStatement("return false")
            .endControlFlow();
        break;
    }

    return codeBlock.build();
  }