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

org.sfm.reflect.meta.ArrayElementPropertyMeta 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.Getter;
import org.sfm.reflect.ReflectionService;
import org.sfm.reflect.Setter;

import java.lang.reflect.Type;

public class ArrayElementPropertyMeta extends PropertyMeta {

	private final int index;
	private final ArrayClassMeta arrayMetaData;
	public ArrayElementPropertyMeta(String name,  String column, ReflectionService reflectService, int index, ArrayClassMeta arrayMetaData) {
		super(name, column, reflectService);
        if (index < 0) throw new IllegalArgumentException("Invalid array index " + index);
		this.index = index;
		this.arrayMetaData = arrayMetaData;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Setter newSetter() {
        return (Setter) new IndexArraySetter(index);
	}

    @SuppressWarnings("unchecked")
    @Override
    protected Getter newGetter() {
        return (Getter) new IndexArrayGetter(index);
    }

    @Override
	public Type getType() {
		return arrayMetaData.getElementTarget();
	}

	public int getIndex() {
		return index;
	}

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


	private static class IndexArraySetter implements Setter {
		private final int index;

		private IndexArraySetter(int index) {
			this.index = index;
		}

		@Override
        public void set(E[] target, E value) throws Exception {
			target[index] = value;
        }
	}

    private static class IndexArrayGetter implements Getter {
        private final int index;

        private IndexArrayGetter(int index) {
            this.index = index;
        }

        @Override
        public E get(E[] target) throws Exception {
            return target[index];
        }
    }

    @Override
    public String toString() {
        return "ArrayElementPropertyMeta{" +
                "index=" + index +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy