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

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

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

import org.unix4j.option.DefaultOptionSet;
import org.unix4j.option.Option;
import org.unix4j.option.OptionSet;

public class OptionSetConverters {
	public static class OptionToSingletonSetConverter & Option> implements ValueConverter> {
		private final Class optionEnumClass;
		public OptionToSingletonSetConverter(Class optionEnumClass) {
			this.optionEnumClass = optionEnumClass;
		}
		@Override
		public OptionSet convert(Object value) throws IllegalArgumentException {
			if (optionEnumClass.isInstance(value)) {
				final O option = optionEnumClass.cast(value);
				return toSingletonSet(option);
			}
			return null;
		}
	}
	public static class NameOrAcronymToOptionConverter & Option> implements ValueConverter {
		private final O[] options;
		public NameOrAcronymToOptionConverter(Class optionEnumClass) {
			this.options = optionEnumClass.getEnumConstants();
		}
		@Override
		public O convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				final String optionString = value.toString();
				if (optionString.length() > 0) {
					for (final O option : options) {
						if (option.name().equals(optionString) || (optionString.length() == 1 && optionString.charAt(0) == option.acronym())) {
							return option;
						}
					}
				}
			}
			return null;
		}
	}
	public static class OptionNameStringToSingletonSetConverter & Option> implements ValueConverter> {
		private final Class optionEnumClass;
		public OptionNameStringToSingletonSetConverter(Class optionEnumClass) {
			this.optionEnumClass = optionEnumClass;
		}
		@Override
		public OptionSet convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				final String optionString = value.toString();
				if (optionString.startsWith("--")) {
					final String optionName = optionString.substring(2);  
					final O option;
					try {
						option = Enum.valueOf(optionEnumClass, optionName);
					} catch (IllegalArgumentException e) {
						return null;
					}
					return toSingletonSet(option);
				}
			}
			return null;
		}
	}
	public static class AcronymStringToOptionSetConverter & Option> implements ValueConverter> {
		private final Class optionEnumClass;
		private final O[] options;
		public AcronymStringToOptionSetConverter(Class optionEnumClass) {
			this.optionEnumClass = optionEnumClass;
			this.options = optionEnumClass.getEnumConstants();
		}
		@Override
		public OptionSet convert(Object value) throws IllegalArgumentException {
			if (value != null) {
				final String optionString = value.toString();
				if (optionString.startsWith("-")) {
					DefaultOptionSet set = null;
					for (int i = 1; i < optionString.length(); i++) {
						final char acronym = optionString.charAt(i);
						O found = null;
						for (final O option : options) {
							if (option.acronym() == acronym) {
								found = option;
								break;
							}
						}
						if (found == null) {
							return null;
						}
						if (set == null) {
							set = new DefaultOptionSet(optionEnumClass);
						}
						set.set(found);
						set.setUseAcronymFor(found, true);
					} 
					return set;
				}
			}
			return null;
		}
	}
	public static class IterableOfOptionNameOrAcronymToOptionSetConverter & Option> implements ValueConverter> {
		private final Class optionEnumClass;
		private final NameOrAcronymToOptionConverter optionConverter;
		public IterableOfOptionNameOrAcronymToOptionSetConverter(Class optionEnumClass) {
			this.optionEnumClass = optionEnumClass;
			this.optionConverter = new NameOrAcronymToOptionConverter(optionEnumClass);
		}
		@Override
		public OptionSet convert(Object value) throws IllegalArgumentException {
			if (value instanceof Iterable) {
				final Iterable iterable = (Iterable)value;
				DefaultOptionSet set = null;
				for (final Object optionNameOrAcronym : iterable) {
					final O option = optionConverter.convert(optionNameOrAcronym);
					if (option == null) {
						return null;
					}
					if (set == null) {
						set = new DefaultOptionSet(optionEnumClass);
					}
					set.set(option);
					set.setUseAcronymFor(option, !option.name().equals(optionNameOrAcronym));
				}
				return set;
			}
			return null;
		}
	}
	
	public static class OptionSetConverter & Option> extends CompositeValueConverter> {
		public OptionSetConverter(Class optionEnumClass) {
			add(new OptionToSingletonSetConverter(optionEnumClass));
			add(new IterableOfOptionNameOrAcronymToOptionSetConverter(optionEnumClass));
			add(new ConcatenatedConverter>(new NameOrAcronymToOptionConverter(optionEnumClass), new OptionToSingletonSetConverter(optionEnumClass)));
		}
	}
	
	private static  & Option> OptionSet toSingletonSet(O option) {
		if (option instanceof OptionSet) {
			@SuppressWarnings("unchecked")
			final OptionSet set = (OptionSet)option;
			return set;
		}
		return new DefaultOptionSet(option);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy