001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.cmdline;
021
022 import org.crsh.cmdline.type.ValueType;
023 import org.crsh.cmdline.type.ValueTypeFactory;
024
025 import java.lang.reflect.ParameterizedType;
026 import java.lang.reflect.Type;
027 import java.util.List;
028
029 public final class ParameterType<V> {
030
031 static ParameterType create(ValueTypeFactory factory, Type type) throws IllegalValueTypeException {
032 Class<?> declaredType;
033 Multiplicity multiplicity;
034 if (type instanceof Class<?>) {
035 declaredType = (Class<Object>)type;
036 multiplicity = Multiplicity.SINGLE;
037 } else if (type instanceof ParameterizedType) {
038 ParameterizedType parameterizedType = (ParameterizedType)type;
039 Type rawType = parameterizedType.getRawType();
040 if (rawType instanceof Class<?>) {
041 Class<?> classRawType = (Class<Object>)rawType;
042 if (List.class.equals(classRawType)) {
043 Type elementType = parameterizedType.getActualTypeArguments()[0];
044 if (elementType instanceof Class<?>) {
045 declaredType = (Class<Object>)elementType;
046 multiplicity = Multiplicity.MULTI;
047 } else {
048 throw new IllegalValueTypeException();
049 }
050 } else {
051 throw new IllegalValueTypeException();
052 }
053 } else {
054 throw new IllegalValueTypeException();
055 }
056 } else {
057 throw new IllegalValueTypeException();
058 }
059
060 //
061 Class<?> effectiveType;
062 ValueType valueType;
063 if (declaredType == String.class) {
064 effectiveType = String.class;
065 valueType = ValueType.STRING;
066 } else if (declaredType == Integer.class || declaredType == int.class) {
067 effectiveType = Integer.class;
068 valueType = ValueType.INTEGER;
069 } else if (declaredType == Boolean.class || declaredType == boolean.class) {
070 effectiveType = Boolean.class;
071 valueType = ValueType.BOOLEAN;
072 } else if (Enum.class.isAssignableFrom(declaredType)) {
073 effectiveType = declaredType;
074 valueType = ValueType.ENUM;
075 } else {
076 effectiveType = declaredType;
077 valueType = factory.get(declaredType);
078 if (valueType == null) {
079 throw new IllegalValueTypeException("Type " + declaredType.getName() + " is not handled at the moment");
080 }
081 }
082
083 //
084 return new ParameterType(multiplicity, declaredType, effectiveType, valueType);
085 }
086
087 /** . */
088 private final Multiplicity multiplicity;
089
090 /** . */
091 private final Class<?> declaredType;
092
093 /** . */
094 private final Class<V> effectiveType;
095
096 /** . */
097 private final ValueType<V> valueType;
098
099 ParameterType(Multiplicity multiplicity, Class<?> declaredType, Class<V> effectiveType, ValueType<V> valueType) {
100 this.multiplicity = multiplicity;
101 this.declaredType = declaredType;
102 this.effectiveType = effectiveType;
103 this.valueType = valueType;
104 }
105
106 public Object parse(String s) throws Exception {
107 return valueType.parse(effectiveType, s);
108 }
109
110 public Multiplicity getMultiplicity() {
111 return multiplicity;
112 }
113
114 public Class<?> getDeclaredType() {
115 return declaredType;
116 }
117
118 public Class<V> getEffectiveType() {
119 return effectiveType;
120 }
121
122 public ValueType<V> getValueType() {
123 return valueType;
124 }
125 }