/** * <p>Joins the elements of the provided {@code Iterable} into * a single String containing the provided elements.</p> * * <p>No delimiter is added before or after the list. Null objects or empty * strings within the iteration are represented by empty strings.</p> * * @param iterable the {@code Iterable} providing the values to join together, may be null * @param separator the separator character to use * @return the joined String, {@code null} if null iterator input */ public static String join(final Iterable<?> iterable, final char separator) { if (iterable == null) { return null; } return join(iterable.iterator(), separator); }
@Test public void testJoin() { Assert.assertEquals(null, Strings.join((Iterable<?>) null, '.')); Assert.assertEquals(null, Strings.join((Iterator<?>) null, '.')); Assert.assertEquals("", Strings.join((Arrays.asList()), '.')); Assert.assertEquals("a", Strings.join(Arrays.asList("a"), '.')); Assert.assertEquals("a.b", Strings.join(Arrays.asList("a", "b"), '.')); Assert.assertEquals("a.b.c", Strings.join(Arrays.asList("a", "b", "c"), '.')); Assert.assertEquals("", Strings.join(Arrays.asList((String) null), ':')); Assert.assertEquals(":", Strings.join(Arrays.asList(null, null), ':')); Assert.assertEquals("a:", Strings.join(Arrays.asList("a", null), ':')); Assert.assertEquals(":b", Strings.join(Arrays.asList(null, "b"), ':')); }
/** * <p>Joins the elements of the provided {@code Iterable} into * a single String containing the provided elements.</p> * * <p>No delimiter is added before or after the list. Null objects or empty * strings within the iteration are represented by empty strings.</p> * * @param iterable the {@code Iterable} providing the values to join together, may be null * @param separator the separator character to use * @return the joined String, {@code null} if null iterator input */ public static String join(final Iterable<?> iterable, final char separator) { if (iterable == null) { return null; } return join(iterable.iterator(), separator); }
@Override public String toString() { return "Singer{" + "singerId='" + this.singerId + '\'' + ", firstName='" + this.firstName + '\'' + ", lastName='" + this.lastName + '\'' + ", albums=" + this.albums + ", firstBand=" + this.firstBand + ", bands=" + ((this.bands == null) ? "" : Strings.join(this.bands.stream().map((x) -> x.getName()) .collect(Collectors.toList()), ',')) + ", personalInstruments=" + ((this.personalInstruments == null) ? "" : Strings.join(this.personalInstruments.stream() .map((x) -> x.getType()).collect(Collectors.toList()), ',')) + '}'; } }
@Override public void format(final LogEvent event, final StringBuilder toAppendTo) { final List<String> ndc = event.getContextStack().asList(); toAppendTo.append(Strings.join(ndc, ' ')); } }
if (!ndc.isEmpty()) { buf.append("<log4j:NDC><![CDATA["); Transform.appendEscapingCData(buf, Strings.join(ndc, ' ')); buf.append("]]></log4j:NDC>\r\n");
/** Logs the Peer connections for each node. */ public void logPeerConnections() { final List<String> connStr = new ArrayList<>(); for (final TestNode node : nodes) { for (final PeerConnection peer : node.network.getPeers()) { final String localString = node.shortId() + "@" + peer.getLocalAddress(); final String peerString = shortId(peer.getPeer().getNodeId()) + "@" + peer.getRemoteAddress(); connStr.add("Connection: " + localString + " to " + peerString); } } LOG.info("TestNodeList Connections:\n" + join(connStr, '\n')); }