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

io.virtdata.libbasics.shared.unary_string.FieldExtractor Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
package io.virtdata.libbasics.shared.unary_string;

import io.virtdata.annotations.Example;
import io.virtdata.annotations.ThreadSafeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.function.Function;

/**
 * Extracts out a set of fields from a delimited string, returning
 * a string with the same delimiter containing only the specified fields.
 */
@ThreadSafeMapper
public class FieldExtractor implements Function {

    private final static Logger logger = LoggerFactory.getLogger(FieldExtractor.class);

    private final String fields;
    private final String splitDelim;
    private final String printDelim;
    private final int maxIdx;
    private int[] indexes;
    private ThreadLocal tlsb = ThreadLocal.withInitial(StringBuilder::new);

    @Example({"FieldExtractor('|,2,16')","extract fields 2 and 16 from the input data with '|' as the delimiter"})
    public FieldExtractor(String fields) {
        this.fields = fields;

        String[] indexSpecs = fields.split(",");
        this.printDelim = indexSpecs[0];
        this.splitDelim = "\\" + indexSpecs[0];
        indexes = new int[indexSpecs.length-1];
        for (int i = 1; i <= indexes.length; i++) {
            indexes[i-1] = Integer.valueOf(indexSpecs[i].trim())-1;
        }
        maxIdx = Arrays.stream(indexes).max().orElse(-1);
    }

    private int[] initIndexes(String fields) {
        return indexes;
    }

    @Override
    public String apply(String s) {
        String[] words = s.split(splitDelim);
        if (words.length




© 2015 - 2024 Weber Informatics LLC | Privacy Policy