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

org.teamapps.data.extract.BeanPropertyExtractor Maven / Gradle / Ivy

There is a newer version: 0.9.194
Show newest version
/*-
 * ========================LICENSE_START=================================
 * TeamApps
 * ---
 * Copyright (C) 2014 - 2024 TeamApps.org
 * ---
 * 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.
 * =========================LICENSE_END==================================
 */
package org.teamapps.data.extract;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.teamapps.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BeanPropertyExtractor implements PropertyExtractor {

	private static final Logger LOGGER = LoggerFactory.getLogger(BeanPropertyExtractor.class);
	private static final Map valueExtractorsByClassAndPropertyName = new ConcurrentHashMap<>();

	private final Map> customExtractors = new HashMap<>(0);
	private final boolean fallbackToFields;

	public BeanPropertyExtractor() {
		this(false);
	}

	public BeanPropertyExtractor(boolean fallbackToFields) {
		this.fallbackToFields = fallbackToFields;
	}

	@Override
	public Object getValue(RECORD record, String propertyName) {
		ValueExtractor valueExtractor = getValueExtractor(record.getClass(), propertyName);
		return valueExtractor.extract(record);
	}

	protected ValueExtractor getValueExtractor(Class clazz, String propertyName) {
		ValueExtractor valueExtractor = customExtractors.get(propertyName);
		if (valueExtractor != null) {
			return valueExtractor;
		} else {
			return valueExtractorsByClassAndPropertyName.computeIfAbsent(
					new ClassAndPropertyName(clazz, propertyName, fallbackToFields),
					classAndPropertyName -> createValueExtractor(classAndPropertyName)
			);
		}
	}

	private ValueExtractor createValueExtractor(ClassAndPropertyName classAndPropertyName) {
		Method getter = ReflectionUtil.findGetter(classAndPropertyName.clazz, classAndPropertyName.propertyName);
		Method recordGetter = ReflectionUtil.findMethodByName(classAndPropertyName.clazz, classAndPropertyName.propertyName);
		if (getter != null) {
			return record -> ReflectionUtil.invokeMethod(record, getter);
		} else if (recordGetter != null) {
			return record -> ReflectionUtil.invokeMethod(record, recordGetter);
		} else if (fallbackToFields) {
			Field field = ReflectionUtil.findField(classAndPropertyName.clazz, classAndPropertyName.propertyName);
			if (field != null) {
				return record -> ReflectionUtil.readField(record, field, true);
			}
		}
		LOGGER.debug("Could not find getter " + (fallbackToFields ? "or field " : "") + "for property {} on class {}!", classAndPropertyName.propertyName, classAndPropertyName.getClass().getCanonicalName());
		return record -> null;
	}

	public BeanPropertyExtractor addProperty(String propertyName, ValueExtractor valueExtractor) {
		this.customExtractors.put(propertyName, valueExtractor);
		return this;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy