001package org.unix4j.convert;
002
003import java.lang.reflect.Array;
004
005public class StringArrayConverters {
006        public static final ValueConverter<String[]> STRING_ARRAY_TO_STRING_ARRAY = new ValueConverter<String[]>() {
007                @Override
008                public String[] convert(Object value) throws IllegalArgumentException {
009                        if (value instanceof String[]) {
010                                return (String[])value;
011                        }
012                        return null;
013                }
014        };
015        public static final ValueConverter<String[]> OBJECT_ARRAY_TO_STRING_ARRAY = new ValueConverter<String[]>() {
016                @Override
017                public String[] convert(Object value) throws IllegalArgumentException {
018                        if (value instanceof Object[]) {
019                                final Object[] array = (Object[])value;
020                                final int len = array.length;
021                                final String[] result = new String[len];
022                                for (int i = 0; i < len; i++) {
023                                        final Object element = array[i];
024                                        result[i] = element == null ? null : element.toString(); 
025                                }
026                                return result;
027                        }
028                        return null;
029                }
030        };
031        public static final ValueConverter<String[]> ANY_ARRAY_TO_STRING_ARRAY = new ValueConverter<String[]>() {
032                @Override
033                public String[] convert(Object value) throws IllegalArgumentException {
034                        if (value != null && value.getClass().isArray()) {
035                                final int len = Array.getLength(value);
036                                final String[] result = new String[len];
037                                for (int i = 0; i < len; i++) {
038                                        final Object element = Array.get(value, i);
039                                        result[i] = element == null ? null : element.toString();
040                                }
041                                return result;
042                        }
043                        return null;
044                }
045        };
046        public static final ValueConverter<String[]> WHITESPACE_DELIMITED = new ValueConverter<String[]>() {
047                @Override
048                public String[] convert(Object value) throws IllegalArgumentException {
049                        if (value != null) {
050                                return value.toString().split("\\s+");
051                        }
052                        return null;
053                }
054        };
055        public static final ValueConverter<String[]> SPACE_DELIMITED = new ValueConverter<String[]>() {
056                @Override
057                public String[] convert(Object value) throws IllegalArgumentException {
058                        if (value != null) {
059                                return value.toString().split(" ");
060                        }
061                        return null;
062                }
063        };
064        public static final ValueConverter<String[]> COMMA_DELIMITED = new ValueConverter<String[]>() {
065                @Override
066                public String[] convert(Object value) throws IllegalArgumentException {
067                        if (value != null) {
068                                return value.toString().split(",");
069                        }
070                        return null;
071                }
072        };
073        public static final ValueConverter<String[]> TAB_DELIMITED = new ValueConverter<String[]>() {
074                @Override
075                public String[] convert(Object value) throws IllegalArgumentException {
076                        if (value != null) {
077                                return value.toString().split(",");
078                        }
079                        return null;
080                }
081        };
082        public static final ValueConverter<String[]> OBJECT_TO_SINGLETON_STRING_ARRAY = new ValueConverter<String[]>() {
083                @Override
084                public String[] convert(Object value) throws IllegalArgumentException {
085                        if (value != null) {
086                                return new String[] {value.toString()};
087                        }
088                        return null;
089                }
090        };
091        public static final ValueConverter<String[]> ARRAY_TO_STRING_ARRAY = new CompositeValueConverter<String[]>().add(STRING_ARRAY_TO_STRING_ARRAY).add(OBJECT_ARRAY_TO_STRING_ARRAY).add(ANY_ARRAY_TO_STRING_ARRAY);
092        public static final ValueConverter<String[]> COLLECTION_TO_STRING_ARRAY = new ConcatenatedConverter<String[]>(ArrayConverters.COLLECTION_TO_ARRAY, OBJECT_ARRAY_TO_STRING_ARRAY);
093        
094        public static final ValueConverter<String[]> COLLECTION_OR_ARRAY_TO_STRING_ARRAY = new CompositeValueConverter<String[]>().add(COLLECTION_TO_STRING_ARRAY).add(ARRAY_TO_STRING_ARRAY);
095        public static final ValueConverter<String[]> COLLECTION_OR_ARRAY_TO_FLAT_STRING_ARRAY = new ConcatenatedConverter<String[]>(ListConverters.COLLECTION_OR_ARRAY_TO_FLAT_LIST, COLLECTION_TO_STRING_ARRAY);
096        
097        public static final ValueConverter<String[]> DEFAULT = new CompositeValueConverter<String[]>().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
098        public static final ValueConverter<String[]> FLATTEN = new CompositeValueConverter<String[]>().add(COLLECTION_OR_ARRAY_TO_FLAT_STRING_ARRAY).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
099        public static final ValueConverter<String[]> DEFAULT_WHITESPACE_DELIMITED = new CompositeValueConverter<String[]>().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(WHITESPACE_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
100        public static final ValueConverter<String[]> DEFAULT_SPACE_DELIMITED = new CompositeValueConverter<String[]>().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(SPACE_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
101        public static final ValueConverter<String[]> DEFAULT_COMMA_DELIMITED = new CompositeValueConverter<String[]>().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(COMMA_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
102        public static final ValueConverter<String[]> DEFAULT_TAB_DELIMITED = new CompositeValueConverter<String[]>().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(TAB_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
103}