All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.effektif.workflow.api.model.ValueConverter Maven / Gradle / Ivy

The newest version!
/* Copyright (c) 2014, Effektif GmbH.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */
package com.effektif.workflow.api.model;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.effektif.workflow.api.json.GenericType;


/**
 * @author Tom Baeyens
 */
public class ValueConverter {

  static Map> converters = new LinkedHashMap<>();
  
  public interface Converter {
    TARGET convert(SOURCE o);
  }
  
  static {
    addConverter(Number.class, new Converter() {
      public Integer convert(Number number) {
        return number.intValue();
      }
    }, Integer.class, int.class);
    addConverter(Number.class, new Converter() {
      public Long convert(Number number) {
        return number.longValue();
      }
    }, Long.class, long.class);
    addConverter(Number.class, new Converter() {
      public Double convert(Number number) {
        return number.doubleValue();
      }
    }, Double.class, double.class);
    addConverter(Number.class, new Converter() {
      public Float convert(Number number) {
        return number.floatValue();
      }
    }, Float.class, float.class);
    addConverter(Number.class, new Converter() {
      public Short convert(Number number) {
        return number.shortValue();
      }
    }, Short.class, short.class);
    addConverter(Number.class, new Converter() {
      public Byte convert(Number number) {
        return number.byteValue();
      }
    }, Byte.class, byte.class);
  }
  
  public static  void addConverter(Class sourceClass, Converter converter, Class... targetClasses) {
    Map sourceConverters = converters.get(sourceClass);
    if (sourceConverters==null) {
      sourceConverters = new HashMap<>();
      converters.put(sourceClass, sourceConverters);
    }
    for (Class clazz: targetClasses) {
      sourceConverters.put(clazz, converter);
    }
  }
  
  public static  T shoehorn(S foot, Class shoe) {
    if (foot==null) {
      return null;
    }
    if (foot.getClass()==shoe) {
      return (T) foot;
    }
    Converter converter = (Converter) findConverter(foot.getClass(), shoe);
    return converter.convert(foot);
  }

  public static  T shoehorn(S foot, Type shoe) {
    if (shoe instanceof Class) {
      return shoehorn(foot, (Class)shoe);
    }
    if (shoe instanceof GenericType) {
      GenericType genericType = (GenericType) shoe;
      if (genericType.getRawClass().isAssignableFrom(ArrayList.class)
          && foot instanceof Collection) {
        return (T) shoehorn((Collection)foot, (Class)genericType.getTypeArgs()[0]);
      }
    }
    throw new RuntimeException("don't know how to shoehorn "+foot+" into "+shoe);
  }

  public static  List shoehorn(Collection feet, Class shoe) {
    if (feet==null) {
      return null;
    }
    ArrayList shoehornedFeet = new ArrayList<>();
    for (S foot: feet) {
      shoehornedFeet.add(shoehorn(foot, shoe));
    }
    return shoehornedFeet;
  }

  public static  Converter findConverter(Class sourceClass, Class targetClass) {
    for (Class candidateSourceClass: converters.keySet()) {
      if (candidateSourceClass==sourceClass) {
        Converter converter = converters.get(candidateSourceClass).get(targetClass);
        if (converter!=null) {
          return converter;
        }
      }
    }
    Class superclass = sourceClass.getSuperclass();
    if (superclass!=null) {
      return findConverter(superclass, targetClass);
    }
    return null;
  }
}