Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.skife.config;
  
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 class Bully
 {
    
All explicit type conversions that config magic knows about. Every new bully will know about those.
 
     private static final List<Coercible<?>> TYPE_COERCIBLES;

    
Catchall converters. These will be run if no specific type coercer was found.
 
     private static final List<Coercible<?>> DEFAULT_COERCIBLES;
 
     static {
         final List<Coercible<?>> typeCoercibles = new ArrayList<Coercible<?>>();
         final List<Coercible<?>> defaultCoercibles = new ArrayList<Coercible<?>>();
 
         typeCoercibles.add(.);
         typeCoercibles.add(.);
         typeCoercibles.add(.);
         typeCoercibles.add(.);
         typeCoercibles.add(.);
         typeCoercibles.add(.);
         typeCoercibles.add(.);
         typeCoercibles.add(.);
 
         // Look Brian, now it groks URIs. ;-)
         typeCoercibles.add(.);
 
         defaultCoercibles.add(.);
         defaultCoercibles.add(.);
         defaultCoercibles.add(.);
         defaultCoercibles.add(.);
 
          = Collections.unmodifiableList(typeCoercibles);
          = Collections.unmodifiableList(defaultCoercibles);
     }

    
The instance specific mappings from a given type to its coercer. This needs to be two-level because the catchall converters will generate specific instances of their coercers based on the type.
 
     private final Map<Class<?>, Coercer<?>> mappings = new HashMap<Class<?>, Coercer<?>>();

    
All the coercibles that this instance knows about. This list can be extended with user mappings.
 
     private final List<Coercible<?>> coercibles = new ArrayList<Coercible<?>>();
 
     public Bully()
     {
         .addAll();
     }

    
Adds a new Coercible to the list of known coercibles. This also resets the current mappings in this bully.
 
     public void addCoercible(final Coercible<?> coercible)
     {
         .add(coercible);
         .clear();
     }
 
     public synchronized Object coerce(Type typeString valueSeparator separator) {
         if (type instanceof Class) {
             Class<?> clazz = (Class<?>)type;
 
             if (clazz.isArray()) {
                 return coerceArray(clazz.getComponentType(), valueseparator);
             }
             else if (Class.class.equals(clazz)) {
                 return coerceClass(typenullvalue);
             }
             else {
                 return coerce(clazzvalue);
             }
         }
         else if (type instanceof ParameterizedType) {
             ParameterizedType parameterizedType = (ParameterizedType)type;
             Type rawType = parameterizedType.getRawType();
 
             if (rawType instanceof Class<?>) {
                 Type[] args = parameterizedType.getActualTypeArguments();
 
                 if (args != null && args.length == 1) {
                     if (args[0] instanceof Class<?>) {
                         return coerceCollection((Class<?>)rawType, (Class<?>)args[0], valueseparator);
                     }
                    else if (args[0] instanceof WildcardType) {
                        return coerceClass(type, (WildcardType)args[0], value);
                    }
                }
            }
        }
        throw new IllegalStateException(String.format("Don't know how to handle a '%s' type for value '%s'"typevalue));
    }
    private boolean isAssignableFrom(Type targetTypeClass<?> assignedClass) {
        if (targetType instanceof Class) {
            return ((Class<?>)targetType).isAssignableFrom(assignedClass);
        }
        else if (targetType instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType)targetType;
            // Class<? extends Foo>
            for (Type upperBoundType : wildcardType.getUpperBounds()) {
                if (!Object.class.equals(upperBoundType)) {
                    if ((upperBoundType instanceof Class<?>) && !((Class<?>)upperBoundType).isAssignableFrom(assignedClass)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    private Class<?> coerceClass(Type typeWildcardType wildcardTypeString value) {
        if (value == null) {
            return null;
        }
        else {
            try {
                Class<?> clazz = Class.forName(value);
                if (!isAssignableFrom(wildcardTypeclazz)) {
                    throw new IllegalArgumentException("Specified class " + clazz + " is not compatible with required type " + type);
                }
                return clazz;
            }
            catch (Exception ex) {
                throw new IllegalArgumentException(ex);
            }
        }
    }
    private Object coerceArray(Class<?> elemTypeString valueSeparator separator) {
        if (value == null) {
            return null;
        }
        else if (value.length() == 0) {
            return Array.newInstance(elemType, 0);
        }
        else {
            String[] tokens = value.split(separator == null ? . : separator.value());
            Object targetArray = Array.newInstance(elemTypetokens.length);
            for (int idx = 0; idx < tokens.lengthidx++) {
                Array.set(targetArrayidxcoerce(elemTypetokens[idx]));
            }
            return targetArray;
        }
    }
    @SuppressWarnings({ "rawtypes""unchecked" })
    private Object coerceCollection(Class<?> containerTypeClass<?> elemTypeString valueSeparator separator) {
        if (value == null) {
            return null;
        }
        else {
            Collection result = null;
            if (Set.class.equals(containerType)) {
                result = new LinkedHashSet();
            }
            else if (Collection.class.equals(containerType) || List.class.equals(containerType)) {
                result = new ArrayList();
            }
            else if (Collection.class.isAssignableFrom(containerType)) {
                try {
                    final Constructor<?> ctor = containerType.getConstructor();
                    if (ctor != null) {
                        result = (Collection)ctor.newInstance();
                    }
                }
                catch (Exception ex) {
                    // handled below
                }
            }
            if (result == null) {
                throw new IllegalStateException(String.format("Don't know how to handle a '%s' container type for value '%s'"containerTypevalue));
            }
            if (value.length() > 0) {
                for (String token : value.split(separator == null ? . : separator.value())) {
                    result.add(coerce(elemTypetoken));
                }
            }
            return result;
        }
    }
    private Object coerce(Class<?> clazzString value) {
        Coercer<?> coercer = getCoercerFor(clazz);
        if (coercer == null) {
            coercer = getCoercerFor(clazz);
            if (coercer == null) {
                throw new IllegalStateException(String.format("Don't know how to handle a '%s' type for value '%s'"clazzvalue));
            }
        }
        return coercer.coerce(value);
    }
    private Coercer<?> getCoercerFor(final List<Coercible<?>> coerciblesfinal Class<?> type)
    {
        Coercer<?> typeCoercer = .get(type);
        if (typeCoercer == null) {
            for (Coercible<?> coercible : coercibles) {
                final Coercer<?> coercer = coercible.accept(type);
                if (coercer != null) {
                    .put(typecoercer);
                    typeCoercer = coercer;
                    break;
                }
            }
        }
        return typeCoercer;
    }
New to GrepCode? Check out our FAQ X