org.solovyev.common.text.CollectionTransformations Maven / Gradle / Ivy
package org.solovyev.common.text;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.collections.CollectionsUtils;
import org.solovyev.common.collections.LoopData;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 8/7/12
* Time: 8:24 PM
*/
public class CollectionTransformations {
/**
* @param string - string where stored list of objects separated by delimiter
* @param delimiter - delimiter with which string will be split
* @param parser - object that will create objects of specified type
* @param - type of object
* @return list of objects, not null
*/
@NotNull
public static List split(@Nullable String string, @NotNull String delimiter, @NotNull Parser parser) {
final List result = new ArrayList();
if (!StringUtils.isEmpty(string)) {
@SuppressWarnings({"ConstantConditions"}) final String[] parts = string.split(delimiter);
if (!CollectionsUtils.isEmpty(parts)) {
for (String part : parts) {
result.add(parser.parseValue(part));
}
}
}
return result;
}
/**
* @param string - string where stored list of objects separated by delimiter
* @param delimiter - delimiter with which string will be split
* @return list of objects, not null
*/
@NotNull
public static List split(@Nullable String string, @NotNull String delimiter) {
return split(string, delimiter, new StringMapper());
}
@Nullable
public static String formatValue(@Nullable List values, @NotNull String delimiter, @NotNull org.solovyev.common.text.Formatter formatter) {
String result = null;
if (!CollectionsUtils.isEmpty(values)) {
final StringBuilder sb = new StringBuilder();
final LoopData ld = new LoopData(values);
for (T value : values) {
sb.append(formatter.formatValue(value));
if (!ld.isLastAndNext()) {
sb.append(delimiter);
}
}
result = sb.toString();
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy