List of usage examples for java.util Collections emptyList
@SuppressWarnings("unchecked") public static final <T> List<T> emptyList()
From source file:Main.java
public static <T> List<T> nullToEmpty(List<T> list) { return list == null ? Collections.emptyList() : list; }
From source file:com.ms.commons.lang.BeanUtils.java
public static <T extends Object> List<T> convert(Class<T> clazz, Collection<?> raw, ValueEditable... specialConverts) { if (Argument.isEmpty(raw)) { return Collections.emptyList(); }/*from w w w. ja v a2 s .c o m*/ List<T> data = new ArrayList<T>(raw.size()); for (Object obj : raw) { T vo; try { vo = clazz.newInstance(); copyProperties(vo, obj, specialConverts); data.add(vo); } catch (Exception e) { e.printStackTrace(); } } return data; }
From source file:Main.java
public static <T> List<T> nonNullFrom(List<T> list) { if (list == null || list.isEmpty()) { return Collections.emptyList(); }/*from w w w . j a v a 2 s . c o m*/ return Collections.unmodifiableList(list); }
From source file:Main.java
public static <T> List<T> readList(String root, String filename, Class<T> type) { List<T> objects = new ArrayList<>(); File file = new File(root, filename); try {/*ww w. j a va 2 s . c o m*/ if (file.exists()) { FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); Object object = ois.readObject(); if (object instanceof List) { for (Object it : (List) object) { objects.add(type.cast(it)); } } ois.close(); return Collections.unmodifiableList(objects); } } catch (Exception e) { Log.e(TAG, String.format("Failed to read [%s]", file), e); } return Collections.emptyList(); }
From source file:Main.java
/** * Using like subList() but it take randoms elements. * * @param list/* w ww .j av a 2 s. c om*/ * @param sizeOfSubList */ public static <E> void randomSubList(List<E> list, int sizeOfSubList) { List<E> subList = Collections.emptyList(); if (isNotEmpty(list) && list.size() > sizeOfSubList) { subList = new ArrayList<E>(sizeOfSubList); Random generator = new Random(); for (int i = 0; i < sizeOfSubList; i++) { int random = generator.nextInt(list.size()); subList.add(list.get(random)); list.remove(random); } } list.clear(); list.addAll(subList); }
From source file:Main.java
/** * @param list// w w w. j av a 2s .com * @param splitSize * @return splited collection */ public static <T> List<List<T>> split(List<T> list, int splitSize) { if (list == null || list.isEmpty() || splitSize <= 0) { return Collections.emptyList(); } List<List<T>> result = new ArrayList<List<T>>(); if (list.size() > splitSize) { int fromIndex = 0; while (true) { int toIndex = fromIndex + splitSize; if (toIndex > list.size()) { toIndex = list.size(); } result.add(list.subList(fromIndex, toIndex)); fromIndex += splitSize; if (fromIndex >= list.size()) { break; } } } else { result.add(list); } return result; }
From source file:Main.java
/** * Copies the contents of a List to a new, unmodifiable List. If the List is null, an empty List will be returned. * <p>// w w w . j a va 2 s .c o m * This is especially useful for methods which take List parameters. Directly assigning such a List to a member * variable can allow external classes to modify internal state. Classes may have different state depending on the * size of a List; for example, a button might be disabled if the the List size is zero. If an external object * can change the List, the containing class would have no way have knowing a modification occurred. * * @param <T> The type of element in the List. * @param list The List to copy. * @return A new, unmodifiable List which contains all of the elements of the List parameter. Will never be null. */ public static <T> List<T> copyToUnmodifiableList(List<T> list) { if (list != null) { List<T> listCopy = new ArrayList(list); return Collections.unmodifiableList(listCopy); } else { return Collections.emptyList(); } }
From source file:Main.java
/** * Wraps a possibly null list in an immutable wrapper. * * @param <T> the type parameter * @param source Nullable list to wrap.// www . jav a2 s .co m * @return {@link Collections#unmodifiableList(List)} if given list is not null, otherwise {@link java.util.Collections#EMPTY_LIST}. */ public static <T> List<T> wrap(final List<T> source) { if (source != null) { return Collections.unmodifiableList(source); } return Collections.emptyList(); }
From source file:com.navercorp.pinpoint.common.server.util.InetAddressUtils.java
public static List<InetAddress> toInetAddressList(List<String> addressList) { if (CollectionUtils.isEmpty(addressList)) { return Collections.emptyList(); }/*from w w w.ja va 2 s . c o m*/ final List<InetAddress> inetAddressList = new ArrayList<InetAddress>(addressList.size()); for (String ignoreAddress : addressList) { if (StringUtils.isBlank(ignoreAddress)) { continue; } // not throw UnknownHostException final InetAddress address = InetAddresses.forString(ignoreAddress); if (address != null) { inetAddressList.add(address); } } return inetAddressList; }
From source file:Main.java
/** * Given two collections of elements of type <T>, return a collection containing the items from both lists * //from ww w .ja v a2 s . c o m * @param <T> * Type * @param collection1 * Collection #1 * @param collection2 * Collection #2 * @return Collection with items from both lists */ public static <T> Collection<T> union(Collection<? extends T> collection1, Collection<? extends T> collection2) { if (isEmpty(collection1)) { if (isEmpty(collection2)) { // if both are empty, return empty list return Collections.emptyList(); } // if just 1 is empty, return 2 return new ArrayList<T>(collection2); } // at this point when know 1 is not empty if (isEmpty(collection2)) { // so if 2 is, return 1. return new ArrayList<T>(collection1); } // we know both 1 and 2 aren't empty Set<T> union = new HashSet<T>(collection1.size() + collection2.size()); union.addAll(collection1); union.addAll(collection2); return new ArrayList<T>(union); }