All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cdc.util.enums.AbstractEnumMaskToString Maven / Gradle / Ivy
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 super V, String> 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 super S, String> 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 super V, String> 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 super V, String> 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 super V, String> converter) {
return new AbstractEnumMaskToString<>(maskClass, prefix, separator, suffix, converter);
}
public Converter super V, String> 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();
}
}