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

com.github.fengxxc.read.CellMappers Maven / Gradle / Ivy

package com.github.fengxxc.read;

import com.github.fengxxc.Relay;
import com.github.fengxxc.BaseMappers;
import com.github.fengxxc.model.Point;
import com.github.fengxxc.util.AsFunction;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fengxxc
 */
public class CellMappers implements BaseMappers, T> {
    private List> mappers = new ArrayList<>();
    private CellMapper current = null;

    public CellMappers() {
    }

    private void start(CellMapper cellMapper) {
        if (current != null) {
            mappers.add(current);
        }
        current = cellMapper;
    }

    @Override
    public CellMappers cell(String cellRef) {
        final CellMapper mapper = CellMapper.of(cellRef);
        start(mapper);
        return this;
    }

    @Override
    public CellMappers cell(Point point) {
        final CellMapper mapper = CellMapper.of(point);
        start(mapper);
        return this;
    }

    private void assertCurrentNull() {
        if (current == null) {
            throw new NullPointerException("Cannot assemble a null CellMapper, you should call 'cell' method first.");
        }
    }

    @Override
    public  Relay, CellMapper, T, R> prop(AsFunction func) {
        assertCurrentNull();

        // Java泛型擦除可真是操蛋
        // renew
        CellMapper reCurrent = CellMapper.reOf(current);
        reCurrent.as(func);
        Relay, CellMapper, T, R> wrap = new Relay, CellMapper, T, R>(this, reCurrent);

        current = reCurrent;
        return wrap;
    }

    @Override
    public CellMappers prop(String property) {
        assertCurrentNull();
        current.as(property);
        return this;
    }

    @Override
    public List> getMappers() {
        this.end();
        return mappers;
    }

    public CellMappers end() {
        if (current != null) {
            this.mappers.add(current);
        }
        return this;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy