001package org.unix4j.convert;
002
003import java.io.File;
004
005public class FileArrayConverters {
006        public static final ValueConverter<File[]> FILE_ARRAY_TO_FILE_ARRAY = new ValueConverter<File[]>() {
007                @Override
008                public File[] convert(Object value) throws IllegalArgumentException {
009                        if (value instanceof File[]) {
010                                return (File[])value;
011                        }
012                        return null;
013                }
014        };
015        public static final ValueConverter<File[]> OBJECT_ARRAY_TO_FILE_ARRAY = new ValueConverter<File[]>() {
016                @Override
017                public File[] convert(Object value) throws IllegalArgumentException {
018                        if (value instanceof Object[]) {
019                                final Object[] array = (Object[])value;
020                                final int len = array.length;
021                                final File[] result = new File[len];
022                                for (int i = 0; i < len; i++) {
023                                        final Object element = array[i];
024                                        result[i] = FileConverters.STRING.convert(element);
025                                }
026                                return result;
027                        }
028                        return null;
029                }
030        };
031        public static final ValueConverter<File[]> OBJECT_TO_SINGLETON_FILE_ARRAY = new ValueConverter<File[]>() {
032                @Override
033                public File[] convert(Object value) throws IllegalArgumentException {
034                        if (value != null) {
035                                return new File[] {FileConverters.STRING.convert(value)};
036                        }
037                        return null;
038                }
039        };
040        public static final ValueConverter<File[]> ARRAY_TO_FILE_ARRAY = new CompositeValueConverter<File[]>().add(FILE_ARRAY_TO_FILE_ARRAY).add(OBJECT_ARRAY_TO_FILE_ARRAY);
041        public static final ValueConverter<File[]> COLLECTION_TO_FILE_ARRAY = new ConcatenatedConverter<File[]>(ArrayConverters.COLLECTION_TO_ARRAY, OBJECT_ARRAY_TO_FILE_ARRAY);
042        
043        public static final ValueConverter<File[]> COLLECTION_OR_ARRAY_TO_FILE_ARRAY = new CompositeValueConverter<File[]>().add(COLLECTION_TO_FILE_ARRAY).add(ARRAY_TO_FILE_ARRAY);
044        public static final ValueConverter<File[]> COLLECTION_OR_ARRAY_TO_FLAT_FILE_ARRAY = new ConcatenatedConverter<File[]>(ListConverters.COLLECTION_OR_ARRAY_TO_FLAT_LIST, COLLECTION_TO_FILE_ARRAY);
045        
046        public static final ValueConverter<File[]> DEFAULT = new CompositeValueConverter<File[]>().add(COLLECTION_OR_ARRAY_TO_FILE_ARRAY).add(OBJECT_TO_SINGLETON_FILE_ARRAY);
047        public static final ValueConverter<File[]> FLATTEN = new CompositeValueConverter<File[]>().add(COLLECTION_OR_ARRAY_TO_FLAT_FILE_ARRAY).add(OBJECT_TO_SINGLETON_FILE_ARRAY);
048}