001 /*
002 * Copyright (C) 2010 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.spi.Completer;
023 import org.crsh.cmdline.spi.Value;
024
025 import java.lang.reflect.Constructor;
026
027 /**
028 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
029 * @version $Revision$
030 */
031 public abstract class SimpleValueType<T> {
032
033 /** . */
034 public static final SimpleValueType<String> STRING = new SimpleValueType<String>(String.class, EmptyCompleter.class) {
035 @Override
036 public <S extends String> String parse(Class<S> type, String s) {
037 return s;
038 }
039 };
040
041 /** . */
042 public static final SimpleValueType<Integer> INTEGER = new SimpleValueType<Integer>(Integer.class, EmptyCompleter.class) {
043 @Override
044 public <S extends Integer> Integer parse(Class<S> type, String s) {
045 return Integer.parseInt(s);
046 }
047 };
048
049 /** . */
050 public static final SimpleValueType<Boolean> BOOLEAN = new SimpleValueType<Boolean>(Boolean.class, EmptyCompleter.class) {
051 @Override
052 public <S extends Boolean> Boolean parse(Class<S> type, String s) {
053 return Boolean.parseBoolean(s);
054 }
055 };
056
057 /** . */
058 public static final SimpleValueType<Enum> ENUM = new SimpleValueType<Enum>(Enum.class, EnumCompleter.class) {
059 @Override
060 public <S extends Enum> Enum parse(Class<S> type, String s) {
061 return Enum.valueOf(type, s);
062 }
063 };
064
065 /** . */
066 public static final SimpleValueType<Value> VALUE = new SimpleValueType<Value>(Value.class, EmptyCompleter.class) {
067 @Override
068 public <S extends Value> Value parse(Class<S> type, String s) throws Exception {
069 Constructor<S> ctor = type.getConstructor(String.class);
070 return ctor.newInstance(s);
071 }
072 };
073
074 /** . */
075 private static final SimpleValueType<?>[] types = { STRING, INTEGER, BOOLEAN, ENUM, VALUE};
076
077 public static SimpleValueType<?> get(Class<?> clazz) {
078 SimpleValueType<?> bestType = null;
079 int bestDegree = Integer.MAX_VALUE;
080 for (SimpleValueType<?> type : types) {
081 int degree = type.getRelativeDegree(clazz);
082 if (degree != -1 && degree < bestDegree) {
083 bestType = type;
084 bestDegree = degree;
085 }
086 }
087 return bestType;
088 }
089
090 /** . */
091 private final Class<T> javaType;
092
093 /** . */
094 private final Class<? extends Completer> completer;
095
096 private SimpleValueType(Class<T> javaType, Class<? extends Completer> completer) {
097 if (javaType == null) {
098 throw new NullPointerException();
099 }
100
101 //
102 this.completer = completer;
103 this.javaType = javaType;
104 }
105
106 public int getRelativeDegree(Class<?> clazz) {
107 if (javaType == clazz) {
108 return 0;
109 } else if (javaType.isAssignableFrom(clazz)) {
110 int degree = 0;
111 for (Class<?> current = clazz;current != javaType;current = current.getSuperclass()) {
112 degree++;
113 }
114 return degree;
115 } else {
116 return -1;
117 }
118 }
119
120 public Class<? extends Completer> getCompleter() {
121 return completer;
122 }
123
124 public Class<T> getJavaType() {
125 return javaType;
126 }
127
128 public abstract <S extends T> T parse(Class<S> type, String s) throws Exception;
129
130 }