001package com.box.sdk.internal.utils; 002 003import java.util.ArrayList; 004import java.util.Collection; 005import java.util.LinkedList; 006import java.util.List; 007 008/** 009 * {@link Collection} related utlities. 010 */ 011public class CollectionUtils { 012 013 /** 014 * Only static members. 015 */ 016 protected CollectionUtils() { 017 } 018 019 /** 020 * Re-maps a provided collection. 021 * 022 * @param <T_Result> type of result 023 * @param <T_Source> type of source 024 * @param source for mapping 025 * @param mapper element mapper 026 * @return mapped source 027 */ 028 public static <T_Result, T_Source> List<T_Result> map(Collection<T_Source> source, 029 Mapper<T_Result, T_Source> mapper) { 030 List<T_Result> result = new LinkedList<T_Result>(); 031 for (T_Source element : source) { 032 result.add(mapper.map(element)); 033 } 034 return result; 035 } 036 037 /** 038 * Creates list from iterable. 039 * 040 * @param iterable Iterable that will be used to create list. 041 * @param <T> Collection element 042 * @return List with all items from iterable. Elements are in the same order as iterable is returning them. 043 */ 044 public static <T> List<T> createListFrom(Iterable<T> iterable) { 045 List<T> result = new ArrayList<T>(); 046 for (T element : iterable) { 047 result.add(element); 048 } 049 return result; 050 } 051 052 /** 053 * Contract for {@link Collection}-s mapping. 054 * 055 * @param <T_Result> type of result 056 * @param <T_Source> type of source 057 */ 058 public interface Mapper<T_Result, T_Source> { 059 060 /** 061 * Maps a provided value. 062 * 063 * @param input for mapping 064 * @return mapped value 065 */ 066 T_Result map(T_Source input); 067 068 } 069}