public final class Utils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Utils.AnalyticsNetworkExecutorService
A
ThreadPoolExecutor implementation by Analytics
instances. |
static class |
Utils.AnalyticsThreadFactory |
static class |
Utils.NullableConcurrentHashMap<K,V>
A
ConcurrentHashMap that rejects null keys and values instead of failing. |
Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_COLLECT_DEVICE_ID |
static int |
DEFAULT_FLUSH_INTERVAL |
static int |
DEFAULT_FLUSH_QUEUE_SIZE |
static java.lang.String |
THREAD_PREFIX |
Modifier and Type | Method and Description |
---|---|
static <T> T |
assertNotNull(T object,
java.lang.String item)
Throws a
NullPointerException if the given object is null. |
static <K,V> java.util.Map<K,V> |
assertNotNullOrEmpty(java.util.Map<K,V> data,
java.lang.String name)
Throws a
NullPointerException if the given map is null or empty. |
static java.lang.String |
assertNotNullOrEmpty(java.lang.String text,
java.lang.String name)
Throws a
NullPointerException if the given text is null or empty. |
static java.io.BufferedReader |
buffer(java.io.InputStream is)
Buffers the given
InputStream . |
static void |
closeQuietly(java.io.Closeable closeable)
Close the given
Closeable . |
static float |
coerceToFloat(java.lang.Object value,
float defaultValue)
Returns the float representation at
value if it exists and is a float or can be coerced
to a float. |
static void |
copySharedPreferences(android.content.SharedPreferences src,
android.content.SharedPreferences target)
Copies all the values from
src to target . |
static void |
createDirectory(java.io.File location)
Ensures that a directory is created in the given location, throws an IOException otherwise.
|
static <T> java.util.Map<java.lang.String,T> |
createMap() |
static java.lang.String |
getDeviceId(android.content.Context context)
Creates a unique device id.
|
static java.io.InputStream |
getInputStream(java.net.HttpURLConnection connection) |
static java.lang.String |
getResourceString(android.content.Context context,
java.lang.String key)
Get the string resource for the given key.
|
static android.content.SharedPreferences |
getSegmentSharedPreferences(android.content.Context context,
java.lang.String tag)
Returns a shared preferences for storing any library preferences.
|
static <T> T |
getSystemService(android.content.Context context,
java.lang.String serviceConstant)
Returns the system service for the given string.
|
static boolean |
hasFeature(android.content.Context context,
java.lang.String feature)
Returns true if the application has the given feature.
|
static boolean |
hasPermission(android.content.Context context,
java.lang.String permission)
Returns true if the application has the given permission.
|
static <T> java.util.List<T> |
immutableCopyOf(java.util.List<T> list)
Returns an immutable copy of the provided list.
|
static <K,V> java.util.Map<K,V> |
immutableCopyOf(java.util.Map<K,V> map)
Returns an immutable copy of the provided map.
|
static boolean |
isConnected(android.content.Context context)
Returns
true if the phone is connected to a network, or if we don't have the enough
permissions. |
static boolean |
isNullOrEmpty(java.lang.CharSequence text)
Returns true if the string is null, or empty (once trimmed).
|
static boolean |
isNullOrEmpty(java.util.Collection collection)
Returns true if the collection is null or has a size of 0.
|
static boolean |
isNullOrEmpty(java.util.Map map)
Returns true if the map is null or empty, false otherwise.
|
static <T> boolean |
isNullOrEmpty(T[] data)
Returns true if the array is null or has a size of 0.
|
static boolean |
isOnClassPath(java.lang.String className)
Return
true if a class with the given name is found. |
static <T> java.util.Set<T> |
newSet(T... values)
Creates a mutable HashSet instance containing the given elements in unspecified order
|
static java.util.Date |
parseISO8601Date(java.lang.String date)
Parse a date from ISO-8601 formatted string.
|
static java.lang.String |
readFully(java.io.BufferedReader reader)
Reads the give
BufferedReader into a String. |
static java.lang.String |
readFully(java.io.InputStream is)
Reads the give
InputStream into a String. |
static java.lang.String |
toISO8601Date(java.util.Date date)
Deprecated.
|
static java.util.Date |
toISO8601Date(java.lang.String date)
Deprecated.
|
static java.lang.String |
toISO8601String(java.util.Date date)
Returns
date formatted as yyyy-MM-ddThh:mm:ss.sssZ |
static org.json.JSONObject |
toJsonObject(java.util.Map<java.lang.String,?> map)
Return a copy of the contents of the given map as a
JSONObject . |
static <T> java.util.Map<java.lang.String,T> |
transform(java.util.Map<java.lang.String,T> in,
java.util.Map<java.lang.String,java.lang.String> mapper)
Transforms the given map by replacing the keys mapped by
mapper . |
public static final java.lang.String THREAD_PREFIX
public static final int DEFAULT_FLUSH_INTERVAL
public static final int DEFAULT_FLUSH_QUEUE_SIZE
public static final boolean DEFAULT_COLLECT_DEVICE_ID
public static <T> java.util.Set<T> newSet(T... values)
public static java.lang.String toISO8601Date(java.util.Date date)
toISO8601String(Date)
.public static java.lang.String toISO8601String(java.util.Date date)
date
formatted as yyyy-MM-ddThh:mm:ss.sssZpublic static java.util.Date parseISO8601Date(java.lang.String date)
date
- ISO string to parse in the appropriate format.public static java.util.Date toISO8601Date(java.lang.String date) throws java.text.ParseException
parseISO8601Date(String)
.java.text.ParseException
public static float coerceToFloat(java.lang.Object value, float defaultValue)
value
if it exists and is a float or can be coerced
to a float. Returns defaultValue
otherwise.public static boolean hasPermission(android.content.Context context, java.lang.String permission)
public static boolean hasFeature(android.content.Context context, java.lang.String feature)
public static <T> T getSystemService(android.content.Context context, java.lang.String serviceConstant)
public static boolean isNullOrEmpty(java.lang.CharSequence text)
public static boolean isNullOrEmpty(java.util.Collection collection)
public static <T> boolean isNullOrEmpty(T[] data)
public static boolean isNullOrEmpty(java.util.Map map)
public static java.lang.String assertNotNullOrEmpty(java.lang.String text, java.lang.String name)
NullPointerException
if the given text is null or empty.public static <K,V> java.util.Map<K,V> assertNotNullOrEmpty(java.util.Map<K,V> data, java.lang.String name)
NullPointerException
if the given map is null or empty.public static <T> T assertNotNull(T object, java.lang.String item)
NullPointerException
if the given object is null.public static <K,V> java.util.Map<K,V> immutableCopyOf(java.util.Map<K,V> map)
public static <T> java.util.List<T> immutableCopyOf(java.util.List<T> list)
public static java.lang.String getDeviceId(android.content.Context context)
public static android.content.SharedPreferences getSegmentSharedPreferences(android.content.Context context, java.lang.String tag)
public static java.lang.String getResourceString(android.content.Context context, java.lang.String key)
public static boolean isConnected(android.content.Context context)
true
if the phone is connected to a network, or if we don't have the enough
permissions. Returns false
otherwise.public static boolean isOnClassPath(java.lang.String className)
true
if a class with the given name is found.public static void closeQuietly(java.io.Closeable closeable)
Closeable
. If an exception is thrown during Closeable.close()
,
this will quietly ignore it. Does nothing if closeable
is null
.public static java.io.BufferedReader buffer(java.io.InputStream is)
InputStream
.public static java.lang.String readFully(java.io.InputStream is) throws java.io.IOException
InputStream
into a String.java.io.IOException
public static java.lang.String readFully(java.io.BufferedReader reader) throws java.io.IOException
BufferedReader
into a String.java.io.IOException
public static java.io.InputStream getInputStream(java.net.HttpURLConnection connection) throws java.io.IOException
java.io.IOException
public static <T> java.util.Map<java.lang.String,T> transform(java.util.Map<java.lang.String,T> in, java.util.Map<java.lang.String,java.lang.String> mapper)
mapper
. Any keys not in the
mapper preserve their original keys. If a key in the mapper maps to null or a blank string,
that value is dropped.
e.g. transform({a: 1, b: 2, c: 3}, {a: a, c: ""}) -> {$a: 1, b: 2} - transforms a to $a - keeps b - removes c
public static org.json.JSONObject toJsonObject(java.util.Map<java.lang.String,?> map)
JSONObject
. Instead of failing on
null
values like the JSONObject
map constructor, it cleans them up and
correctly converts them to JSONObject.NULL
.public static <T> java.util.Map<java.lang.String,T> createMap()
public static void createDirectory(java.io.File location) throws java.io.IOException
java.io.IOException
public static void copySharedPreferences(android.content.SharedPreferences src, android.content.SharedPreferences target)
src
to target
.