Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright Benjamin Piwowarski, 2007. All rights reserved. Permission to use, copy, modify and redistribute is granted, provided that this copyright notice is retained and the author is given credit whenever appropriate. This software is distributed "as is", without any warranty, including any implied warranty of merchantability or fitness for a particular use. The author assumes no responsibility for, and shall not be liable for, any special, indirect, or consequential damages, or any damages whatsoever, arising out of or in connection with the use of this software.
 
 package bpiwowar.argparser.handlers;
 
 
Convert a comma separated list into an array

Author(s):
bpiwowar
 
 
 	static public class TypeChecker implements TypeCheckerInterface {

Returns true if the type if ? extends Collection<String>
 
 		public boolean isCompatible(Type gtype) {
 			if (gtype instanceof ParameterizedType) {
 				// list the raw type information
 				ParameterizedType ptype = (ParameterizedTypegtype;
 				Type rtype = ptype.getRawType();
 				if (rtype instanceof Class<?>) {
 					Class<?> ctype = (Class<?>)rtype;
 					if (Collection.class.isAssignableFrom(ctype)) {
 						// list the actual type arguments
 						Type[] targs = ptype.getActualTypeArguments();
 						if (targs.length > 0 && targs[0].equals(Integer.class)) {
 							return true;
 						}
 					}
 				}
 			}
 			
 			return false;
 		}
 
 		/*
 		 * (non-Javadoc)
 		 * 
 		 * @see java.lang.Object#toString()
 		 */
 		public String toString() {
 			return "Collection<String> checker";
 		}
 
 	}

Used to give a separator different than the comma
 
 	@Target(value = { . })
 	public @interface Options {

Returns:
Strings separator
 
 		String separator() default ",";

Returns:
Set the object to null if the option is empty
 
 		boolean nullify() default false;
 	}
 
 	String separator = ",";
 
 	boolean nullify = false;
 
 	public IntegerCollectionHandler(Object objectField field) {
 		super(objectfield);
 
 		// Get the separator
 		Options annotation = field.getAnnotation(Options.class);
 		if (annotation != null) {
 			 = annotation.separator();
 			 = annotation.nullify();
 		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see argparser.BooleanHolderInterface#isValue()
	 */
	@SuppressWarnings("unchecked")
	public String getValue() {
		try {
			Collection<Stringcollection = (Collection<String>) get();
			if (collection == null)
				return "";
			String r = null;
			for (String s : collection)
				if (r == null)
					r = s;
				else
					r +=  + s;
			return r;
catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see yrla.argparser.holders.Holder#scan(yrla.argparser.StringScanner)
	 */
	@SuppressWarnings("unchecked")
	public Object process(ArgParser argParserStringScanner scannerthrows StringScanException {
		try {
			String value = scanner.scanString();
			StringTokenizer tokenizer = new StringTokenizer(value);
			Collection<Integercollection = (Collection<Integer>) get();
			if (!tokenizer.hasMoreTokens() && ) {
				addValue(null);
else {
				if (collection == null)
					collection = (Collection<Integer>) newInstance();
				collection.clear();
				while (tokenizer.hasMoreTokens())
					collection.add(Integer.parseInt(tokenizer.nextToken()));
			}
			return value;
catch (Exception e) {
			throw new StringScanException(e);
		}
	}
New to GrepCode? Check out our FAQ X