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

com.syntaxphoenix.syntaxapi.data.DataAdapter Maven / Gradle / Ivy

The newest version!
package com.syntaxphoenix.syntaxapi.data;

import java.util.function.Function;

public abstract class DataAdapter {

    private final Function builder;
    private final Function extractor;

    private final Class primitiveType;
    private final Class resultType;

    public DataAdapter(Class primitiveType, Class resultType, Function builder, Function extractor) {
        this.primitiveType = primitiveType;
        this.resultType = resultType;
        this.builder = builder;
        this.extractor = extractor;
    }

    public abstract Class getBaseType();

    protected I extract(B input) {
        return resultType.isInstance(input) ? extractor.apply(resultType.cast(input)) : null;
    }

    protected R build(Object input) {
        return primitiveType.isInstance(input) ? builder.apply(primitiveType.cast(input)) : null;
    }

    public boolean isInstance(B base) {
        return resultType.isInstance(base);
    }

}