com.pushtorefresh.storio.internal.Queries Maven / Gradle / Ivy
Show all versions of common Show documentation
package com.pushtorefresh.storio.internal;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableList;
/**
* Util methods for queries.
*
* For internal usage only!
*/
public final class Queries {
private Queries() {
throw new IllegalStateException("No instances please");
}
/**
* Converts array of objects to {@code List}.
*
* @param args array objects that will be converted to list of strings.
* @return non-null, unmodifiable list of strings.
*/
@NonNull
public static List unmodifiableNonNullListOfStrings(@Nullable Object[] args) {
if (args == null || args.length == 0) {
return emptyList();
} else {
final List list = new ArrayList(args.length);
//noinspection ForLoopReplaceableByForEach -> on Android it's faster
for (int i = 0; i < args.length; i++) {
final Object arg = args[i];
list.add(arg != null ? arg.toString() : "null");
}
return unmodifiableList(list);
}
}
/**
* Coverts list of objects to {@code List}.
*
* @param args list of objects that will be converted to list of strings.
* @return non-null, unmodifiable list of strings.
*/
@NonNull
public static List unmodifiableNonNullListOfStrings(@Nullable List> args) {
if (args == null || args.isEmpty()) {
return emptyList();
} else {
final List list = new ArrayList(args.size());
for (Object arg : args) {
list.add(arg != null ? arg.toString() : "null");
}
return unmodifiableList(list);
}
}
/**
* Converts list of something to unmodifiable non-null list.
*
* @param list list to convert, can be {@code null}.
* @return non-null, unmodifiable list of something.
*/
@SuppressWarnings("unchecked")
@NonNull
public static List unmodifiableNonNullList(@Nullable List list) {
return list == null || list.isEmpty()
? (List) emptyList()
: unmodifiableList(list);
}
/**
* Converts set of something to unmodifiable non-null set.
*
* @param set set to convert, can be {@code null}.
* @return non-null, unmodifiable set of something.
*/
@SuppressWarnings("unchecked")
@NonNull
public static Set unmodifiableNonNullSet(@Nullable Set set) {
return set == null || set.isEmpty()
? (Set) emptySet()
: Collections.unmodifiableSet(set);
}
/**
* Converts list of strings to non-null array of strings.
*
* @param list of strings.
* @return non-null array of strings.
*/
@NonNull
public static String[] nonNullArrayOfStrings(@Nullable List list) {
return list == null || list.isEmpty()
? new String[]{}
: list.toArray(new String[list.size()]);
}
/**
* Converts list of strings to nullable array of strings.
*
* @param list list of strings.
* @return nullable array of strings.
*/
@Nullable
public static String[] nullableArrayOfStrings(@Nullable List list) {
return list == null || list.isEmpty()
? null
: list.toArray(new String[list.size()]);
}
/**
* Converts nullable string to non-null empty string if string was null
* and returns string as is otherwise.
*
* @param str string to convert, can be null.
* @return non-null string, can be empty.
*/
@NonNull
public static String nonNullString(@Nullable String str) {
return str == null ? "" : str;
}
/**
* Coverts nullable string to nullable string and if string was null or empty
* and returns string as is otherwise.
*
* @param str string to convert, can be null.
* @return nullable string, can not be empty.
*/
@Nullable
public static String nullableString(@Nullable String str) {
return str == null || str.isEmpty() ? null : str;
}
}