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

cdc.util.enums.MaskToString Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package cdc.util.enums;

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

import cdc.util.args.Args;
import cdc.util.args.FormalArg;
import cdc.util.args.FormalArgs;
import cdc.util.args.Necessity;
import cdc.util.converters.Converter;
import cdc.util.converters.defaults.AbstractSequenceToString;
import cdc.util.lang.Introspection;

/**
 * Converter from a mask to a string.
 *
 * @author Damien Carbonne
 *
 * @param  The mask type.
 * @param  The value type.
 */
public class MaskToString, V> extends AbstractSequenceToString {
    private final MaskSupport support;
    private final Converter converter;

    public static final FormalArg> SUPPORT =
            new FormalArg<>("support", Introspection.uncheckedCast(MaskSupport.class), Necessity.MANDATORY);
    public static final FormalArg> CONVERTER =
            new FormalArg<>("converter", Introspection.uncheckedCast(Converter.class), Necessity.MANDATORY);

    public static final FormalArgs FPARAMS =
            new FormalArgs(PREFIX,
                           SEPARATOR,
                           SUFFIX,
                           SUPPORT,
                           CONVERTER);

    public MaskToString(MaskSupport maskSupport,
                        String prefix,
                        String separator,
                        String suffix,
                        Converter converter) {
        super(maskSupport.getMaskClass(), prefix, separator, suffix);
        this.support = maskSupport;
        this.converter = converter;
    }

    public final MaskSupport getMaskSupport() {
        return support;
    }

    public final Converter getConverter() {
        return converter;
    }

    @Override
    public String apply(M mask) {
        // Create a sorted list in order of declarations of values in the supporting type.
        final List list = new ArrayList<>();
        for (final V value : support.getType().getValues()) {
            if (mask.getValues().contains(value)) {
                list.add(value);
            }
        }
        return toString(converter, list);
    }

    @Override
    public Args getParams() {
        return Args.builder()
                   .setArg(PREFIX, getPrefix())
                   .setArg(SEPARATOR, getSeparator())
                   .setArg(SUFFIX, getSuffix())
                   .setArg(SUPPORT, getMaskSupport())
                   .setArg(CONVERTER, getConverter())
                   .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy