Adapted from com.google.common.base.Strings#emptyToNull.
(Iterable<? extends T> iterable, Class<T> type)
| 157 | |
| 158 | /** Adapted from {@code com.google.common.base.Strings#emptyToNull}. */ |
| 159 | @SuppressWarnings("unchecked") |
| 160 | public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) { |
| 161 | Collection<T> collection; |
| 162 | if (iterable instanceof Collection) { |
| 163 | collection = (Collection<T>) iterable; |
| 164 | } else { |
| 165 | collection = new ArrayList<>(); |
| 166 | for (T element : iterable) { |
| 167 | collection.add(element); |
| 168 | } |
| 169 | } |
| 170 | T[] array = (T[]) Array.newInstance(type, collection.size()); |
| 171 | return collection.toArray(array); |
| 172 | } |
| 173 | |
| 174 | /** Returns an unmodifiable collection which may be empty, but is never null. */ |
| 175 | public static <T> Collection<T> valuesOrEmpty(Map<String, Collection<T>> map, String key) { |