Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.KITKAT;
      4 
      5 import java.text.FieldPosition;
      6 import java.util.HashMap;
      7 import java.util.Locale;
      8 import java.util.Map;
      9 import libcore.icu.DateIntervalFormat;
     10 import org.robolectric.annotation.Implementation;
     11 import org.robolectric.annotation.Implements;
     12 
     13 @Implements(value = DateIntervalFormat.class, isInAndroidSdk = false, minSdk = KITKAT)
     14 public class ShadowDateIntervalFormat {
     15 
     16   private static long address;
     17   private static Map<Long, com.ibm.icu.text.DateIntervalFormat> INTERVAL_CACHE = new HashMap<>();
     18 
     19   @Implementation
     20   public static long createDateIntervalFormat(String skeleton, String localeName, String tzName) {
     21     address++;
     22     INTERVAL_CACHE.put(address, com.ibm.icu.text.DateIntervalFormat.getInstance(skeleton, new Locale(localeName)));
     23     return address;
     24   }
     25 
     26   @Implementation
     27   public static void destroyDateIntervalFormat(long address) {
     28     INTERVAL_CACHE.remove(address);
     29   }
     30 
     31   @Implementation
     32   @SuppressWarnings("JdkObsolete")
     33   public static String formatDateInterval(long address, long fromDate, long toDate) {
     34     StringBuffer buffer = new StringBuffer();
     35 
     36     FieldPosition pos = new FieldPosition(0);
     37     INTERVAL_CACHE.get(address).format(new com.ibm.icu.util.DateInterval(fromDate, toDate), buffer, pos);
     38 
     39     return buffer.toString();
     40   }
     41 }
     42