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

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

package org.sfm.reflect.meta;

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

import org.sfm.reflect.asm.ConstructorDefinition;

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

	private final ArrayClassMeta arrayClassMeta;
	private final Map> properties = new HashMap>();
	private final Map> subPropertyFinders = new HashMap>();
	private int maxIndex = -1;
	
	
	public ArrayPropertyFinder(ArrayClassMeta arrayClassMeta) {
		this.arrayClassMeta = arrayClassMeta;
	}

	@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));
		
		maxIndex = Math.max(index,  maxIndex);

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

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

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

	@Override
	public Class getClassToInstantiate() {
		return arrayClassMeta.getType();
	}

	public Class getElementType() {
		return arrayClassMeta.getElementTarget();
	}
	
	public int getLength() {
		return maxIndex + 1;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy