001package org.unix4j.convert; 002 003import java.util.Collection; 004 005public class ArrayConverters { 006 public static final ValueConverter<Object[]> COLLECTION_TO_ARRAY = new ValueConverter<Object[]>() { 007 @Override 008 public Object[] convert(Object value) throws IllegalArgumentException { 009 if (value instanceof Collection) { 010 return ((Collection<?>)value).toArray(); 011 } 012 return null; 013 } 014 }; 015 public static final ValueConverter<Object[]> OBJECT_TO_SINGLETON_ARRAY = new ValueConverter<Object[]>() { 016 @Override 017 public Object[] convert(Object value) throws IllegalArgumentException { 018 if (value != null) { 019 return new Object[] {value}; 020 } 021 return null; 022 } 023 }; 024 025 public static final ValueConverter<Object[]> COLLECTION_OR_ARRAY_TO_FLAT_ARRAY = new ConcatenatedConverter<Object[]>(ListConverters.COLLECTION_OR_ARRAY_TO_FLAT_LIST, COLLECTION_TO_ARRAY); 026 public static final ValueConverter<Object[]> DEFAULT = new CompositeValueConverter<Object[]>().add(COLLECTION_TO_ARRAY).add(OBJECT_TO_SINGLETON_ARRAY); 027 public static final ValueConverter<Object[]> FLATTEN = new CompositeValueConverter<Object[]>().add(COLLECTION_OR_ARRAY_TO_FLAT_ARRAY).add(OBJECT_TO_SINGLETON_ARRAY); 028}