View Javadoc
1   package org.oxerr.commons.ws.rs.provider;
2   
3   import java.lang.annotation.Annotation;
4   import java.lang.reflect.Type;
5   
6   import javax.inject.Singleton;
7   import javax.ws.rs.ext.ParamConverter;
8   import javax.ws.rs.ext.ParamConverterProvider;
9   import javax.ws.rs.ext.Provider;
10  
11  /**
12   * {@code ParamConverterProvider} for converting between a {@code String} and
13   * {@link Character}.
14   */
15  @Provider
16  @Singleton
17  public class CharacterProvider implements ParamConverterProvider {
18  
19  	/**
20  	 * {@inheritDoc}
21  	 */
22  	@Override
23  	public <T> ParamConverter<T> getConverter(Class<T> rawType,
24  			Type genericType, Annotation[] annotations) {
25  		return (rawType != Character.class) ? null : new ParamConverter<T>() {
26  
27  			@Override
28  			public T fromString(final String value) {
29  				if (value == null) {
30  					throw new IllegalArgumentException("value cannot be null");
31  				}
32  
33  				final T ret;
34  				if (value.isEmpty()) {
35  					ret = null;
36  				} else {
37  					ret = rawType.cast(Character.valueOf(value.charAt(0)));
38  				}
39  
40  				return ret;
41  			}
42  
43  			@Override
44  			public String toString(final T value)
45  					throws IllegalArgumentException {
46  				if (value == null) {
47  					throw new IllegalArgumentException("value cannot be null.");
48  				}
49  				return value.toString();
50  			}
51  		};
52  	}
53  
54  }