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.type;
021    
022    import org.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
024    
025    import java.util.Collections;
026    import java.util.Iterator;
027    import java.util.LinkedHashSet;
028    import java.util.ServiceConfigurationError;
029    import java.util.ServiceLoader;
030    
031    /**
032     * A factory for value types.
033     */
034    public class ValueTypeFactory {
035    
036      /** A value type factory instance that provides a predefined set of value types. */
037      public static final ValueTypeFactory DEFAULT = new ValueTypeFactory();
038    
039      /** The known types. */
040      private final ValueType<?>[] types;
041    
042      private ValueTypeFactory() {
043        this.types = new ValueType<?>[]{ ValueType.STRING, ValueType.INTEGER, ValueType.BOOLEAN,
044            ValueType.ENUM, ValueType.PROPERTIES, ValueType.OBJECT_NAME};
045      }
046    
047      /**
048       * Create a value type factory for the the default value types and the value types that the specified
049       * classloader will load.
050       *
051       * @param loader the loader
052       * @throws NullPointerException if the loader is null
053       */
054      public ValueTypeFactory(ClassLoader loader) throws NullPointerException {
055        if (loader == null) {
056          throw new NullPointerException("No null loader accepted");
057        }
058    
059        //
060        LinkedHashSet<ValueType> types = new LinkedHashSet<ValueType>();
061        Collections.addAll(types, DEFAULT.types);
062        Iterator<ValueType> sl = ServiceLoader.load(ValueType.class, loader).iterator();
063        while (sl.hasNext()) {
064          try {
065            ValueType type = sl.next();
066            types.add(type);
067          }
068          catch (ServiceConfigurationError e) {
069            // Log it
070            Logger logger = LoggerFactory.getLogger(ValueTypeFactory.class);
071            logger.warn("Could not load value type factory", e);
072          }
073        }
074    
075        //
076        this.types = types.toArray(new ValueType[types.size()]);
077      }
078    
079      public <T, S extends T> ValueType<T> get(Class<S> clazz) {
080        ValueType<?> bestType = null;
081        int bestDegree = Integer.MAX_VALUE;
082        for (ValueType<?> type : types) {
083          int degree = type.getDistance(clazz);
084          if (degree != -1 && degree < bestDegree) {
085            bestType = type;
086            bestDegree = degree;
087          }
088        }
089        return (ValueType<T>)bestType;
090      }
091    }