cdc.mf.checks.FormatUtils Maven / Gradle / Ivy
The newest version!
package cdc.mf.checks;
import java.util.List;
import java.util.function.Function;
import cdc.mf.model.MfCardinality;
public final class FormatUtils {
private FormatUtils() {
}
/**
* Return a choice among values.
*
* - V1
*
- V1, or V2
*
- V1, V2, ..., or Vn
*
*
* @param The value type.
* @param values The values.
* @param toString The value to string converter.
* @return A string listing values.
*/
public static String choose(List values,
Function toString) {
final StringBuilder builder = new StringBuilder();
for (int index = 0; index < values.size(); index++) {
if (index > 0) {
if (index == values.size() - 1) {
builder.append(", or ");
} else {
builder.append(", ");
}
}
builder.append(toString.apply(values.get(index)));
}
return builder.toString();
}
public static String chooseCardinality(List values) {
return choose(values, v -> "'" + v.toExpandedString() + "'");
}
public static String chooseBound(List bounds) {
return choose(bounds, v -> "'" + MfCardinality.boundToString(v) + "'");
}
}