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

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

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

import cdc.util.args.AbstractFactory;
import cdc.util.args.Args;
import cdc.util.args.Factory;
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.AbstractSequenceConverter;
import cdc.util.lang.Checks;
import cdc.util.lang.Introspection;

/**
 * Converter of EnumMask to String.
 *
 * @author Damien Carbonne
 *
 * @param  The mask type.
 * @param  The value type.
 */
public class AbstractEnumMaskToString, V> extends AbstractSequenceConverter {
    private final Converter converter;

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

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

    @FunctionalInterface
    protected static interface Creator {
        public C create(String prefix,
                        String separator,
                        String suffix,
                        Converter converter);
    }

    protected static , M extends AbstractEnumMask, V> Factory factory(Class converterClass,
                                                                                                                        Class maskClass,
                                                                                                                        Class valueClass,
                                                                                                                        Creator creator) {
        return new AbstractFactory(converterClass,
                                      Args.builder()
                                          .setArg(SOURCE_CLASS, maskClass)
                                          .setArg(TARGET_CLASS, String.class)
                                          .build(),
                                      FPARAMS) {
            @Override
            protected C create(Args args,
                               FormalArgs fargs) {
                final String prefix = args.getValue(PREFIX, null);
                final String separator = args.getValue(SEPARATOR, null);
                final String suffix = args.getValue(SUFFIX, null);
                final Converter converter = Introspection.uncheckedCast(args.getValue(CONVERTER));
                Checks.isTrue(converter.isCompliantSourceClass(valueClass), "Non compliant converter");
                return creator.create(prefix, separator, suffix, converter);
            }
        };
    }

    protected AbstractEnumMaskToString(Class maskClass,
                                       String prefix,
                                       String separator,
                                       String suffix,
                                       Converter converter) {
        super(maskClass,
              String.class,
              prefix,
              separator,
              suffix);
        this.converter = converter;
    }

    public static , V> AbstractEnumMaskToString create(Class maskClass,
                                                                                              String prefix,
                                                                                              String separator,
                                                                                              String suffix,
                                                                                              Converter converter) {
        return new AbstractEnumMaskToString<>(maskClass, prefix, separator, suffix, converter);
    }

    public Converter getConverter() {
        return converter;
    }

    @Override
    public String apply(M mask) {
        if (mask == null) {
            return null;
        } else {
            final StringBuilder builder = new StringBuilder();
            builder.append(getPrefix());
            boolean first = true;
            for (final V value : mask.getEnumType().getValues()) {
                if (mask.isSet(value)) {
                    if (!first) {
                        builder.append(getSeparator());
                    } else {
                        first = false;
                    }
                    builder.append(converter.apply(value));
                }
            }
            builder.append(getSuffix());
            return builder.toString();
        }
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy