001package org.unix4j.convert; 002 003import java.lang.reflect.Array; 004import java.util.ArrayList; 005import java.util.Arrays; 006import java.util.Collection; 007import java.util.Collections; 008import java.util.List; 009 010public class ListConverters { 011 public static final ValueConverter<List<?>> COLLECTION_TO_LIST = new ValueConverter<List<?>>() { 012 @Override 013 public List<?> convert(Object value) throws IllegalArgumentException { 014 if (value instanceof Collection) { 015 if (value instanceof List) { 016 return (List<?>)value; 017 } 018 return new ArrayList<Object>((Collection<?>)value); 019 } 020 return null; 021 } 022 }; 023 public static final ValueConverter<List<Object>> OBJECT_ARRAY_TO_LIST = new ValueConverter<List<Object>>() { 024 @Override 025 public List<Object> convert(Object value) throws IllegalArgumentException { 026 if (value instanceof Object[]) { 027 return Arrays.asList((Object[])value); 028 } 029 return null; 030 } 031 }; 032 public static final ValueConverter<List<Object>> ANY_ARRAY_TO_LIST = new ValueConverter<List<Object>>() { 033 @Override 034 public List<Object> convert(Object value) throws IllegalArgumentException { 035 if (value != null && value.getClass().isArray()) { 036 final int len = Array.getLength(value); 037 final List<Object> result = new ArrayList<Object>(len); 038 for (int i = 0; i < len; i++) { 039 final Object element = Array.get(value, i); 040 result.add(element); 041 } 042 return result; 043 } 044 return null; 045 } 046 }; 047 public static final ValueConverter<List<Object>> ARRAY_TO_LIST = new CompositeValueConverter<List<Object>>().add(OBJECT_ARRAY_TO_LIST).add(ANY_ARRAY_TO_LIST); 048 049 public static final ValueConverter<List<Object>> OBJECT_TO_SINGLETON_LIST = new ValueConverter<List<Object>>() { 050 @Override 051 public List<Object> convert(Object value) throws IllegalArgumentException { 052 if (value != null) { 053 return Collections.singletonList(value); 054 } 055 return null; 056 } 057 }; 058 059 public static final ValueConverter<List<?>> COLLECTION_TO_FLAT_LIST = new ValueConverter<List<?>>() { 060 @Override 061 public List<?> convert(Object value) throws IllegalArgumentException { 062 if (value instanceof Collection) { 063 final Collection<?> source = (Collection<?>)value; 064 final List<Object> target = new ArrayList<Object>(source.size()); 065 for (final Object element : source) { 066 final List<?> unnested = COLLECTION_OR_ARRAY_TO_FLAT_LIST.convert(element); 067 if (unnested == null) { 068 target.add(element); 069 } else { 070 target.addAll(unnested); 071 } 072 } 073 return target; 074 } 075 return null; 076 } 077 }; 078 public static final ValueConverter<List<?>> ARRAY_TO_FLAT_LIST = new ConcatenatedConverter<List<?>>(ARRAY_TO_LIST, COLLECTION_TO_FLAT_LIST); 079 public static final ValueConverter<List<?>> COLLECTION_OR_ARRAY_TO_FLAT_LIST = new CompositeValueConverter<List<?>>().add(COLLECTION_TO_FLAT_LIST).add(ARRAY_TO_FLAT_LIST); 080 081 public static final ValueConverter<List<?>> DEFAULT = new CompositeValueConverter<List<?>>().add(COLLECTION_TO_LIST).add(ARRAY_TO_LIST).add(OBJECT_TO_SINGLETON_LIST); 082 public static final ValueConverter<List<?>> FLATTEN = new CompositeValueConverter<List<?>>().add(COLLECTION_OR_ARRAY_TO_FLAT_LIST).add(OBJECT_TO_SINGLETON_LIST); 083}