String s = String.join("\n" , "It was the best of times, it was the worst of times," , "it was the age of wisdom, it was the age of foolishness," , "it was the epoch of belief, it was the epoch of incredulity," , "it was the season of Light, it was the season of Darkness," , "it was the spring of hope, it was the winter of despair," , "we had everything before us, we had nothing before us" );
String joinedString = String.join(" - ", "04", "05", "06"); // "04 - 05 - 06"
List<String> list = Arrays.asList("foo", "bar", "baz"); String joined = String.join(" and ", list); // "foo and bar and baz"
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"
private String toVarcharValue(Object value) { if (value instanceof Collection<?>) { return "[" + String.join(", ", ((Collection<?>) value).stream().map(this::toVarcharValue).collect(toList())) + "]"; } if (value instanceof Document) { return ((Document) value).toJson(); } return String.valueOf(value); }
String headerAsString(HttpHeaders headers, String headerName) { List<String> values = headers.getAll(headerName); return (values.size() == 0) ? "-" : String.join(",", values); } }
public static ResourceGroupIdTemplate fromSegments(List<ResourceGroupNameTemplate> segments) { return new ResourceGroupIdTemplate(String.join(".", segments.stream().map(ResourceGroupNameTemplate::toString).collect(Collectors.toList()))); }
public static void write(CandidateComponentsMetadata metadata, OutputStream out) throws IOException { Properties props = new Properties(); metadata.getItems().forEach(m -> props.put(m.getType(), String.join(",", m.getStereotypes()))); props.store(out, ""); }
private static Properties createProperties(String key, String stereotypes) { Properties properties = new Properties(); properties.put(key, String.join(",", stereotypes)); return properties; }
private ZipFunction(List<String> typeParameters) { super(new Signature("zip", FunctionKind.SCALAR, typeParameters.stream().map(Signature::typeVariable).collect(toImmutableList()), ImmutableList.of(), parseTypeSignature("array(row(" + join(",", typeParameters) + "))"), typeParameters.stream().map(name -> "array(" + name + ")").map(TypeSignature::parseTypeSignature).collect(toImmutableList()), false)); this.typeParameters = typeParameters; }
/** * Returns a list of objects with the same hash code, of size 2^power, counting calls to equals, * hashCode, and compareTo in counter. */ static List<CountsHashCodeAndEquals> createAdversarialInput(int power, CallsCounter counter) { String str1 = "Aa"; String str2 = "BB"; assertEquals(str1.hashCode(), str2.hashCode()); List<String> haveSameHashes2 = Arrays.asList(str1, str2); List<CountsHashCodeAndEquals> result = Lists.newArrayList( Lists.transform( Lists.cartesianProduct(Collections.nCopies(power, haveSameHashes2)), strs -> new CountsHashCodeAndEquals( String.join("", strs), () -> counter.hashCode++, () -> counter.equals++, () -> counter.compareTo++))); assertEquals( result.get(0).delegateString.hashCode(), result.get(result.size() - 1).delegateString.hashCode()); return result; }
/** * Returns a list of objects with the same hash code, of size 2^power, counting calls to equals, * hashCode, and compareTo in counter. */ static List<CountsHashCodeAndEquals> createAdversarialObjects(int power, CallsCounter counter) { String str1 = "Aa"; String str2 = "BB"; assertEquals(str1.hashCode(), str2.hashCode()); List<String> haveSameHashes2 = Arrays.asList(str1, str2); List<CountsHashCodeAndEquals> result = Lists.newArrayList( Lists.transform( Lists.cartesianProduct(Collections.nCopies(power, haveSameHashes2)), strs -> new CountsHashCodeAndEquals( String.join("", strs), () -> counter.hashCode++, () -> counter.equals++, () -> counter.compareTo++))); assertEquals( result.get(0).delegateString.hashCode(), result.get(result.size() - 1).delegateString.hashCode()); return result; }
/** * Returns a list of objects with the same hash code, of size 2^power, counting calls to equals, * hashCode, and compareTo in counter. */ static List<CountsHashCodeAndEquals> createAdversarialInput(int power, CallsCounter counter) { String str1 = "Aa"; String str2 = "BB"; assertEquals(str1.hashCode(), str2.hashCode()); List<String> haveSameHashes2 = Arrays.asList(str1, str2); List<CountsHashCodeAndEquals> result = Lists.newArrayList( Lists.transform( Lists.cartesianProduct(Collections.nCopies(power, haveSameHashes2)), strs -> new CountsHashCodeAndEquals( String.join("", strs), () -> counter.hashCode++, () -> counter.equals++, () -> counter.compareTo++))); assertEquals( result.get(0).delegateString.hashCode(), result.get(result.size() - 1).delegateString.hashCode()); return result; }
private void testWithQuotedParameters(String... mimeTypes) { String s = String.join(",", mimeTypes); List<MimeType> actual = MimeTypeUtils.parseMimeTypes(s); assertEquals(mimeTypes.length, actual.size()); for (int i=0; i < mimeTypes.length; i++) { assertEquals(mimeTypes[i], actual.get(i).toString()); } }
private void verifyWrittenData(Flux<DataBuffer> writeResult) throws IOException { StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("baz")) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(3)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobarbazqux", result); }
@Test public void writeWritableByteChannelCancel() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult, 1) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(Duration.ofSeconds(5)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foo", result); channel.close(); flux.subscribe(DataBufferUtils::release); }
@Test public void writeWritableByteChannelErrorInFlux() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException())); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectError() .verify(Duration.ofSeconds(5)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobar", result); channel.close(); }
@Test public void writeAsynchronousFileChannelErrorInFlux() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Mono.error(new RuntimeException())); AsynchronousFileChannel channel = AsynchronousFileChannel.open(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectError(RuntimeException.class) .verify(); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobar", result); channel.close(); }
@Test public void writeAsynchronousFileChannelCanceled() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar); AsynchronousFileChannel channel = AsynchronousFileChannel.open(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult, 1) .consumeNextWith(stringConsumer("foo")) .thenCancel() .verify(); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foo", result); channel.close(); flux.subscribe(DataBufferUtils::release); }