Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import com.xtremelabs.robolectric.internal.Implementation;
      4 import com.xtremelabs.robolectric.internal.Implements;
      5 import com.xtremelabs.robolectric.internal.RealObject;
      6 
      7 import android.util.Pair;
      8 
      9 import java.lang.reflect.Field;
     10 
     11 /**
     12  * Shadow of {@code Pair}
     13  */
     14 @SuppressWarnings({"UnusedDeclaration"})
     15 @Implements(Pair.class)
     16 public class ShadowPair {
     17     @RealObject private Pair realPair;
     18 
     19     public void __constructor__(Object first, Object second) {
     20         setFields(realPair, first, second);
     21     }
     22 
     23     @Implementation
     24     public static <F, S> Pair<F, S> create(F f, S s) {
     25         return new Pair<F, S>(f, s);
     26     }
     27 
     28     @Override @Implementation
     29     public int hashCode() {
     30         return realPair.first.hashCode() + realPair.second.hashCode();
     31     }
     32 
     33     @Override @Implementation
     34     public boolean equals(Object o) {
     35         if (o == this) return true;
     36         if (!(o instanceof Pair)) return false;
     37         final Pair other = (Pair) o;
     38         return realPair.first.equals(other.first) && realPair.second.equals(other.second);
     39     }
     40 
     41     private static void setFields(Pair p, Object first, Object second) {
     42         try {
     43             Field f = Pair.class.getDeclaredField("first");
     44             f.setAccessible(true);
     45             f.set(p, first);
     46 
     47             Field s = Pair.class.getDeclaredField("second");
     48             s.setAccessible(true);
     49             s.set(p, second);
     50         } catch (NoSuchFieldException e) {
     51             throw new RuntimeException(e);
     52         } catch (IllegalAccessException e) {
     53             throw new RuntimeException(e);
     54         }
     55     }
     56 }
     57 
     58 
     59