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