use of java.util.StringJoiner in project httpx by EricEdens.
the class IPv6 method canonicalizeIpv6.
private static String canonicalizeIpv6(short[] addr) {
int bestRunStart = -1;
int bestRunLength = -1;
int runStart = -1;
for (int i = 0; i < addr.length + 1; i++) {
if (i < addr.length && addr[i] == 0) {
if (runStart == -1) {
runStart = i;
}
} else if (runStart > -1) {
int len = i - runStart;
if (len > bestRunLength) {
bestRunLength = len;
bestRunStart = runStart;
}
runStart = -1;
}
}
if (bestRunLength < 2) {
StringJoiner joiner = new StringJoiner(":");
for (short s : addr) {
joiner.add(String.format("%x", s));
}
return joiner.toString();
}
StringBuilder sb = new StringBuilder();
for (int i = 0, addrLength = addr.length; i < addrLength; i++) {
if (i >= bestRunStart && i < bestRunStart + bestRunLength) {
if (i == bestRunStart) {
if (i == 0)
sb.append(':');
sb.append(':');
}
} else {
sb.append(String.format("%x", addr[i]));
if (i < addr.length - 1) {
sb.append(':');
}
}
}
return sb.toString();
}
use of java.util.StringJoiner in project jdk8u_jdk by JetBrains.
the class String method join.
/**
* Returns a new {@code String} composed of copies of the
* {@code CharSequence elements} joined together with a copy of the
* specified {@code delimiter}.
*
* <blockquote>For example,
* <pre>{@code
* List<String> strings = new LinkedList<>();
* strings.add("Java");strings.add("is");
* strings.add("cool");
* String message = String.join(" ", strings);
* //message returned is: "Java is cool"
*
* Set<String> strings = new LinkedHashSet<>();
* strings.add("Java"); strings.add("is");
* strings.add("very"); strings.add("cool");
* String message = String.join("-", strings);
* //message returned is: "Java-is-very-cool"
* }</pre></blockquote>
*
* Note that if an individual element is {@code null}, then {@code "null"} is added.
*
* @param delimiter a sequence of characters that is used to separate each
* of the {@code elements} in the resulting {@code String}
* @param elements an {@code Iterable} that will have its {@code elements}
* joined together.
*
* @return a new {@code String} that is composed from the {@code elements}
* argument
*
* @throws NullPointerException If {@code delimiter} or {@code elements}
* is {@code null}
*
* @see #join(CharSequence,CharSequence...)
* @see java.util.StringJoiner
* @since 1.8
*/
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs : elements) {
joiner.add(cs);
}
return joiner.toString();
}
use of java.util.StringJoiner in project oc-explorer by devgateway.
the class OCDSObjectExcelSheet method writeRowFlattenObject.
/**
* Function to flat export an array of objects. Example:
* [
* {
* x: aaa,
* y: bbb,
* },
* {
* x: ccc,
* y: ddd,
* }
* ]
*
* Will be printed as:
* | x | y |
* | aaa ; bbb | ccc ; ddd |
*
* @param objects
* @param row
*/
public void writeRowFlattenObject(final List<Object> objects, final Row row) {
final Iterator<Field> fields = OCDSObjectUtil.getFields(clazz);
while (fields.hasNext()) {
final Field field = fields.next();
final int fieldType = OCDSObjectUtil.getFieldType(field);
try {
switch(fieldType) {
case FieldType.BASIC_FIELD:
try {
// get the last 'free' cell
int coll = row.getLastCellNum() == -1 ? 0 : row.getLastCellNum();
StringJoiner flattenValue = new StringJoiner(" | ");
for (Object obj : objects) {
if (obj != null && PropertyUtils.getProperty(obj, field.getName()) != null) {
flattenValue.add(PropertyUtils.getProperty(obj, field.getName()).toString());
}
}
if (row.getRowNum() < 2) {
writeHeaderLabel(field, coll);
}
writeCell(flattenValue.toString(), row, coll);
} catch (NoSuchMethodException e) {
// do nothing
}
break;
case FieldType.OCDS_OBJECT_FIELD:
if (field.getType().equals(java.util.Set.class) || field.getType().equals(java.util.List.class)) {
LOGGER.error("Unsupported operation for field: '" + field.getName() + "'! You can not flatten an array of objects that contains other array of objects " + "that need to be printed in other sheet.");
} else {
final List<Object> newObjects = new ArrayList();
for (Object obj : objects) {
if (obj != null && PropertyUtils.getProperty(obj, field.getName()) != null && PropertyUtils.isReadable(obj, field.getName())) {
newObjects.add(PropertyUtils.getProperty(obj, field.getName()));
}
}
final OCDSObjectExcelSheet objectSheet = new OCDSObjectExcelSheet(this.workbook, OCDSObjectUtil.getFieldClass(field), excelSheetName, getHeaderPrefix() + field.getName());
objectSheet.writeRowFlattenObject(newObjects, row);
}
break;
case FieldType.OCDS_OBJECT_SEPARETE_SHEET_FIELD:
LOGGER.error("Unsupported operation for field: '" + field.getName() + "'! You can not flatten an array of objects that contains objects " + "that need to be printed in other sheet.");
break;
default:
LOGGER.error("Undefined field type");
break;
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
LOGGER.error(e);
}
}
}
Aggregations