org.srplib.conversion.FormatConverter Maven / Gradle / Ivy
package org.srplib.conversion;
import java.util.Collection;
import org.srplib.contract.Argument;
/**
* A converter encapsulating {@link String#format(String, Object...)} logic.
*
* Converter tries to interpret input value as an array using following rules:
*
* - if null then return null
* - if array then return array
* - if collection then convert to array
* - else return String.valueOf(value)
*
*
* @author Anton Pechinsky
*/
public class FormatConverter implements Converter {
private String pattern;
private FormatConverter(String pattern) {
Argument.checkNotNull(pattern, "String pattern must not be null!");
this.pattern = pattern;
}
@Override
public String convert(I input) {
return String.format(pattern, toArray(input));
}
private Object toArray(I input) {
Object result;
if (input == null) {
result = null;
}
else if (input.getClass().isArray()) {
result = input;
}
else if (input instanceof Collection) {
Collection> collection = (Collection>)input;
result = collection.toArray(new Object[collection.size()]);
}
else {
result = String.valueOf(input);
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy