aQute.bnd.build.model.conversions.CollectionFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of biz.aQute.bndlib Show documentation
Show all versions of biz.aQute.bndlib Show documentation
bndlib: A Swiss Army Knife for OSGi
package aQute.bnd.build.model.conversions;
import java.util.Collection;
public class CollectionFormatter implements Converter> {
private final String separator;
private final Converter itemFormatter;
private final String emptyOutput;
private final boolean leadingSpace;
private final String initial;
private final String suffix;
public CollectionFormatter(String separator) {
this(separator, (String) null);
}
public CollectionFormatter(String separator, String emptyOutput) {
this(separator, new DefaultFormatter(), emptyOutput);
}
public CollectionFormatter(String separator, Converter itemFormatter) {
this(separator, itemFormatter, null);
}
public CollectionFormatter(String separator, Converter itemFormatter, String emptyOutput) {
this(separator, itemFormatter, emptyOutput, false, "\\\n\t", "");
}
public CollectionFormatter(String separator, Converter itemFormatter, String emptyOutput,
String prefix, String suffix) {
this(separator, itemFormatter, emptyOutput, false, prefix, suffix);
}
/**
* @param separator Separator between items
* @param itemFormatter Formatter for each item
* @param emptyOutput Output to produce for empty inputs
* @param leadingSpace Whether to lead with a space before the first item
* @param prefix Prefix for the first item in lists containing more than one
* items.
* @param suffix Suffix to add at the end of the list
*/
public CollectionFormatter(String separator, Converter itemFormatter, String emptyOutput,
boolean leadingSpace, String prefix, String suffix) {
this.separator = separator;
this.itemFormatter = itemFormatter;
this.emptyOutput = emptyOutput;
this.leadingSpace = leadingSpace;
this.initial = prefix;
this.suffix = suffix;
}
public String convert(Collection< ? extends T> input) throws IllegalArgumentException {
String result = null;
if (input != null) {
if (input.isEmpty()) {
result = emptyOutput;
} else {
StringBuilder buffer = new StringBuilder();
if (leadingSpace)
buffer.append(' ');
if (input.size() == 1) {
T item = input.iterator().next();
buffer.append(itemFormatter.convert(item));
} else {
String del = initial == null ? "" : initial;
for (T item : input) {
buffer.append(del);
buffer.append(itemFormatter.convert(item));
del = separator;
}
}
if (suffix != null)
buffer.append(suffix);
result = buffer.toString();
}
}
return result;
}
@Override
public String error(String msg) {
return msg;
}
}