All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unix4j.convert.StringArrayConverters Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
package org.unix4j.convert;

import java.lang.reflect.Array;

public class StringArrayConverters {
	public static final ValueConverter STRING_ARRAY_TO_STRING_ARRAY = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value instanceof String[]) {
				return (String[])value;
			}
			return null;
		}
	};
	public static final ValueConverter OBJECT_ARRAY_TO_STRING_ARRAY = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value instanceof Object[]) {
				final Object[] array = (Object[])value;
				final int len = array.length;
				final String[] result = new String[len];
				for (int i = 0; i < len; i++) {
					final Object element = array[i];
					result[i] = element == null ? null : element.toString(); 
				}
				return result;
			}
			return null;
		}
	};
	public static final ValueConverter ANY_ARRAY_TO_STRING_ARRAY = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value != null && value.getClass().isArray()) {
				final int len = Array.getLength(value);
				final String[] result = new String[len];
				for (int i = 0; i < len; i++) {
					final Object element = Array.get(value, i);
					result[i] = element == null ? null : element.toString();
				}
				return result;
			}
			return null;
		}
	};
	public static final ValueConverter WHITESPACE_DELIMITED = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				return value.toString().split("\\s+");
			}
			return null;
		}
	};
	public static final ValueConverter SPACE_DELIMITED = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				return value.toString().split(" ");
			}
			return null;
		}
	};
	public static final ValueConverter COMMA_DELIMITED = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				return value.toString().split(",");
			}
			return null;
		}
	};
	public static final ValueConverter TAB_DELIMITED = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				return value.toString().split(",");
			}
			return null;
		}
	};
	public static final ValueConverter OBJECT_TO_SINGLETON_STRING_ARRAY = new ValueConverter() {
		@Override
		public String[] convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				return new String[] {value.toString()};
			}
			return null;
		}
	};
	public static final ValueConverter ARRAY_TO_STRING_ARRAY = new CompositeValueConverter().add(STRING_ARRAY_TO_STRING_ARRAY).add(OBJECT_ARRAY_TO_STRING_ARRAY).add(ANY_ARRAY_TO_STRING_ARRAY);
	public static final ValueConverter COLLECTION_TO_STRING_ARRAY = new ConcatenatedConverter(ArrayConverters.COLLECTION_TO_ARRAY, OBJECT_ARRAY_TO_STRING_ARRAY);
	
	public static final ValueConverter COLLECTION_OR_ARRAY_TO_STRING_ARRAY = new CompositeValueConverter().add(COLLECTION_TO_STRING_ARRAY).add(ARRAY_TO_STRING_ARRAY);
	public static final ValueConverter COLLECTION_OR_ARRAY_TO_FLAT_STRING_ARRAY = new ConcatenatedConverter(ListConverters.COLLECTION_OR_ARRAY_TO_FLAT_LIST, COLLECTION_TO_STRING_ARRAY);
	
	public static final ValueConverter DEFAULT = new CompositeValueConverter().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
	public static final ValueConverter FLATTEN = new CompositeValueConverter().add(COLLECTION_OR_ARRAY_TO_FLAT_STRING_ARRAY).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
	public static final ValueConverter DEFAULT_WHITESPACE_DELIMITED = new CompositeValueConverter().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(WHITESPACE_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
	public static final ValueConverter DEFAULT_SPACE_DELIMITED = new CompositeValueConverter().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(SPACE_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
	public static final ValueConverter DEFAULT_COMMA_DELIMITED = new CompositeValueConverter().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(COMMA_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
	public static final ValueConverter DEFAULT_TAB_DELIMITED = new CompositeValueConverter().add(COLLECTION_OR_ARRAY_TO_STRING_ARRAY).add(TAB_DELIMITED).add(OBJECT_TO_SINGLETON_STRING_ARRAY);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy