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