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

org.simpleflatmapper.reflect.meta.MapElementPropertyMeta Maven / Gradle / Ivy

Go to download

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

The newest version!
package org.simpleflatmapper.reflect.meta;

import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.Setter;

import java.lang.reflect.Type;
import java.util.Map;

public class MapElementPropertyMeta, K, V> extends PropertyMeta {

	private final ClassMeta valueMetaData;
	private final K key;
	private final MapSetter setter;
	private final MapGetter getter;


	public MapElementPropertyMeta(PropertyNameMatcher propertyNameMatcher, Type ownerType, ReflectionService reflectService, ClassMeta valueMetaData, K key) {
		super(propertyNameMatcher.toString(), ownerType, reflectService);
		this.valueMetaData = valueMetaData;
		this.key = key;
		setter = new MapSetter(key);
		getter = new MapGetter(key);
	}

	public MapElementPropertyMeta(String name, Type ownerType, ReflectionService reflectService, ClassMeta valueMetaData, K key, MapSetter setter, MapGetter getter) {
		super(name, ownerType, reflectService);
		this.valueMetaData = valueMetaData;
		this.key = key;
		this.setter = setter;
		this.getter = getter;
	}

	@Override
	public Setter getSetter() {
        return setter;
	}

    @Override
    public Getter getGetter() {
        return getter;
    }

    @Override
	public Type getPropertyType() {
		return valueMetaData.getType();
	}


	@Override
	public String getPath() {
		return key + "." + getName();
	}

	@Override
	public PropertyMeta withReflectionService(ReflectionService reflectionService) {
		return new MapElementPropertyMeta(getName(), getOwnerType(), reflectionService, reflectionService.getClassMeta(valueMetaData.getType()), key, setter, getter);
	}

	@Override
	public PropertyMeta toNonMapped() {
		throw new UnsupportedOperationException();
	}

	public K getKey() {
		return key;
	}


	private static class MapSetter, K, V> implements Setter {
		private final K key;

		private MapSetter(K key) {
			this.key = key;
		}

		@Override
        public void set(T target, V value) throws Exception {
			target.put(key, value);
        }
	}

    private static class MapGetter, K, V> implements Getter {
        private final K key;

        private MapGetter(K key) {
            this.key = key;
        }

        @Override
        public V get(T target) throws Exception {
            return target.get(key);
        }
    }

    @Override
    public String toString() {
        return "MapElementPropertyMeta{"
				+"key=" + key +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy