package androidx.test.ext.truth.content;

import android.content.Intent;
import com.google.common.annotations.Beta;
import com.google.common.base.Objects;
import com.google.common.truth.Correspondence;

/**
 * Collection of {@link com.google.common.truth.Correspondence} helpers for asserting lists of
 * {@link Intent}s.
 *
 * @see {@link com.google.common.truth.IterableSubject#comparingElementsUsing(Correspondence)}
 */
public final class IntentCorrespondences {

  private IntentCorrespondences() {}

  public static Correspondence<Intent, Intent> action() {
    return new Correspondence<Intent, Intent>() {
      @Override
      public boolean compare(Intent actual, Intent expected) {
        return Objects.equal(actual.getAction(), expected.getAction());
      }

      @Override
      public String toString() {
        return "has getAction() equal to";
      }
    };
  }

  public static Correspondence<Intent, Intent> data() {
    return new Correspondence<Intent, Intent>() {
      @Override
      public boolean compare(Intent actual, Intent expected) {
        return Objects.equal(actual.getData(), expected.getData());
      }

      @Override
      public String toString() {
        return "has getData() equal to";
      }
    };
  }

  @Beta
  public static Correspondence<Intent, Intent> all(
      final Correspondence<Intent, Intent>... correspondences) {
    return new Correspondence<Intent, Intent>() {
      @Override
      public boolean compare(Intent actual, Intent expected) {
        for (Correspondence<Intent, Intent> innerCorrespondence : correspondences) {
          if (!innerCorrespondence.compare(actual, expected)) {
            return false;
          }
        }
        return true;
      }

      @Override
      public String toString() {
        StringBuilder combinedString = new StringBuilder();
        for (int i = 0; i < correspondences.length; i++) {
          combinedString.append(correspondences[i]);
          if ((i + 1) < correspondences.length) {
            combinedString.append(" and ");
          }
        }
        return combinedString.toString();
      }
    };
  }
}

@@ -560,36 +560,32 @@
 () %7B%0A    return 
-new 
 Correspondence%3CI
@@ -586,237 +586,101 @@
 ence
-%3CIntent, Intent%3E() %7B%0A      @Override%0A      public boolean compare(Intent actual, Intent expected) %7B%0A        return Objects.equal(actual.getAction(), expected.getAction());%0A      %7D%0A%0A      @Override%0A      public String toString() %7B
+.from(%0A        (actual, expected) -%3E Objects.equal(actual.getAction(), expected.getAction()),
 %0A   
@@ -676,39 +676,32 @@
 ion()),%0A        
-return 
 %22has getAction()
@@ -706,39 +706,25 @@
 () equal to%22
-;%0A      %7D%0A    %7D
+)
 ;%0A  %7D%0A%0A  pub
@@ -777,36 +777,32 @@
 () %7B%0A    return 
-new 
 Correspondence%3CI
@@ -803,95 +803,31 @@
 ence
-%3CIntent, Intent%3E() %7B%0A      @Override%0A      public boolean compare(Intent 
+.from(%0A        (
 actual,
- Intent
  exp
@@ -833,32 +833,18 @@
 pected) 
-%7B%0A        return
+-%3E
  Objects
@@ -891,67 +891,9 @@
 a())
-;%0A      %7D%0A%0A      @Override%0A      public String toString() %7B
+,
 %0A   
@@ -897,23 +897,16 @@
         
-return 
 %22has get
@@ -921,31 +921,17 @@
 qual to%22
-;%0A      %7D%0A    %7D
+)
 ;%0A  %7D%0A%0A 
@@ -1062,120 +1062,312 @@
     
-return new Correspondence%3CIntent, Intent%3E() %7B%0A      @Override%0A      public boolean compare(Intent 
+StringBuilder combinedString = new StringBuilder();%0A    for (int i = 0; i %3C correspondences.length; i++) %7B%0A      combinedString.append(correspondences%5Bi%5D);%0A      if ((i + 1) %3C correspondences.length) %7B%0A        combinedString.append(%22 and %22);%0A      %7D%0A    %7D%0A    return Correspondence.from(%0A        (
 actual,
- Intent
  exp
@@ -1372,16 +1372,19 @@
 xpected)
+ -%3E
  %7B%0A     
@@ -1382,24 +1382,26 @@
 %3E %7B%0A        
+  
 for (Corresp
@@ -1461,24 +1461,26 @@
 ondences) %7B%0A
+  
           if
@@ -1539,24 +1539,26 @@
             
+  
 return false
@@ -1559,32 +1559,24 @@
  false;%0A    
-      %7D%0A
         %7D%0A  
@@ -1581,33 +1581,16 @@
     
-    return true;%0A
       %7D%0A
 %0A   
@@ -1589,360 +1589,49 @@
   %7D%0A
-%0A
       
-@Override%0A      public String toString() %7B%0A        StringBuilder combinedString = new StringBuilder();%0A        for (int i = 0; i %3C correspondences.length; i++) %7B%0A          combinedString.append(correspondences%5Bi%5D);%0A          if ((i + 1) %3C correspondences.length) %7B%0A            combinedString.append(%22 and %22);%0A          %7D%0A        %7D%0A        return
+    return true;%0A        %7D,%0A       
  com
@@ -1652,31 +1652,17 @@
 String()
-;%0A      %7D%0A    %7D
+)
 ;%0A  %7D%0A%7D%0A

Migrate Correspondence subclasses to instead call Correspondence.from.