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

org.sfm.reflect.meta.SingletonPropertyFinder Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.reflect.meta;

import org.sfm.reflect.ConstructorDefinition;
import org.sfm.reflect.ConstructorParameter;
import org.sfm.reflect.TypeHelper;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class SingletonPropertyFinder implements PropertyFinder {
	private static final HashMap, Integer> coefficients = new HashMap, Integer>();
	static {
		coefficients.put(double.class, 128);
		coefficients.put(Double.class, 127);
		coefficients.put(float.class, 126);
		coefficients.put(Float.class, 125);
		coefficients.put(long.class, 124);
		coefficients.put(Long.class, 123);
		coefficients.put(int.class, 122);
		coefficients.put(Integer.class, 121);
		coefficients.put(short.class, 120);
		coefficients.put(Short.class, 119);
		coefficients.put(byte.class, 118);
		coefficients.put(Byte.class, 117);
		coefficients.put(char.class, 116);
		coefficients.put(Character.class, 115);
		coefficients.put(String.class, 114);
		coefficients.put(Date.class, 113);
	}

	private final PropertyFinder propertyFinder;

	private final List selectedParameters = new ArrayList();

	public SingletonPropertyFinder(ClassMeta classMeta) {
		this.propertyFinder = classMeta.newPropertyFinder();
	}


	@Override
	public  PropertyMeta findProperty(PropertyNameMatcher propertyNameMatcher) {

		PropertyMeta property = propertyFinder.findProperty(propertyNameMatcher);

		if (property == null && selectedParameters.isEmpty()) {
			ConstructorDefinition constructorDefinition = readerFriendlyConstructor( propertyFinder.getEligibleConstructorDefinitions());

			if (constructorDefinition != null) {
				ConstructorParameter param = constructorDefinition.getParameters()[selectedParameters.size()];
				selectedParameters.add(param.getName());
				return propertyFinder.findConstructor(constructorDefinition);
			}
		}
		return property;
	}

	protected ConstructorDefinition readerFriendlyConstructor(List> eligibleConstructorDefinitions) {
		ConstructorDefinition selected = null;

		for(ConstructorDefinition def : eligibleConstructorDefinitions) {
			if (def.getParameters().length == 1) {
				if (selected == null
						|| prefersFirstType(def.getParameters()[0].getType(), selected.getParameters()[0].getType()))
					selected = def;
			}
		}

		return selected;
	}

	private boolean prefersFirstType(Type type1, Type type2) {
		return getCoefficient(type1) > getCoefficient(type2);
	}

	private int getCoefficient(Type t) {
		if (coefficients.containsKey(TypeHelper.toClass(t))) {
			return coefficients.get(TypeHelper.toClass(t));
		} else {
			return -1;
		}
	}


	@Override
	public List> getEligibleConstructorDefinitions() {
		return propertyFinder.getEligibleConstructorDefinitions();
	}

    @Override
    public  ConstructorPropertyMeta findConstructor(ConstructorDefinition constructorDefinition) {
        return propertyFinder.findConstructor(constructorDefinition);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy