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

com.github.leeonky.util.CollectionDataPropertyWriter Maven / Gradle / Ivy

The newest version!
package com.github.leeonky.util;

import java.lang.reflect.Array;
import java.util.List;
import java.util.function.BiConsumer;

import static java.lang.Integer.parseInt;

class CollectionDataPropertyWriter extends DataPropertyAccessor implements PropertyWriter {
    @SuppressWarnings("unchecked")
    private final BiConsumer SETTER = (bean, value) -> {
        Class type = getBeanType().getType();
        int index = parseInt(getName());
        if (type.isArray())
            Array.set(bean, index, value);
        else if (List.class.isAssignableFrom(type))
            ((List) bean).set(index, value);
        else
            throw new CannotSetElementByIndexException(type);
    };

    public CollectionDataPropertyWriter(BeanClass beanClass, String name, BeanClass type) {
        super(beanClass, name, type);
    }

    @Override
    public BiConsumer setter() {
        return SETTER;
    }
}