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

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

package org.sfm.reflect.meta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

@SuppressWarnings({ "unchecked", "rawtypes" })
public class ListPropertyFinder implements PropertyFinder> {

	private final ListClassMeta listClassMeta;
	private final Map> properties = new HashMap>();
	private final Map> subPropertyFinders = new HashMap>();
	private static final List constructors;
	
	static {
		constructors = new ArrayList();
		try {
			constructors.add(new ConstructorDefinition(ArrayList.class.getConstructor(), new ConstructorParameter[] {}));
		} catch(Exception e) {
			throw new Error(e.getMessage(), e);
		}
	}

	
	public ListPropertyFinder(ListClassMeta listClassMeta) {
		this.listClassMeta = listClassMeta;
	}

	@Override
	public PropertyMeta, ?> findProperty(PropertyNameMatcher propertyNameMatcher) {
		
		String propertyName = propertyNameMatcher.getColumn();
		
		int listIndexStart = propertyNameMatcher.getFrom();
		while(listIndexStart < propertyName.length() &&  !Character.isDigit(propertyName.charAt(listIndexStart))) {
			listIndexStart++;
		}
		
		int listIndexEnd = listIndexStart;
		while(listIndexEnd < propertyName.length() &&  Character.isDigit(propertyName.charAt(listIndexEnd))) {
			listIndexEnd++;
		}
		if (listIndexStart == listIndexEnd) {
			return null;
		}
		
		int index = Integer.parseInt(propertyName.substring(listIndexStart, listIndexEnd));

		ListElementPropertyMeta prop = properties.get(index);
		if (prop == null) {
			prop = new ListElementPropertyMeta(String.valueOf(index), String.valueOf(index),listClassMeta.getReflectionService(), index, listClassMeta);
			properties.put(index, prop);
		}
		
		if (listIndexEnd == propertyName.length()) {
			return prop;
		}
		
		String subPropName = propertyName.substring(listIndexEnd);
		
		PropertyFinder propertyFinder = subPropertyFinders.get(subPropName);
		
		if (propertyFinder == null) {
			propertyFinder = listClassMeta.getElementClassMeta().newPropertyFinder();
			subPropertyFinders.put(subPropName, propertyFinder);
		}
		
		PropertyMeta subProp = propertyFinder.findProperty(subPropName);

		
		if (subProp != null) {
			return new SubPropertyMeta(listClassMeta.getReflectionService(), prop, subProp);
		}
		
		return null;
	}
	
	@Override
	public PropertyMeta, ?> findProperty(String propertyName) {
		return findProperty(new PropertyNameMatcher(propertyName));
	}

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

	@Override
	public Class> getClassToInstantiate() {
		Class type = ArrayList.class;
		return type;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy