Skip to content

Commit

Permalink
Renaming IterableResponse to IterableStream (#5002)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanGiles authored Aug 16, 2019
1 parent 2eec622 commit 56dc993
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package com.azure.core.http.rest;

import com.azure.core.util.IterableStream;

import java.util.stream.Stream;

/**
Expand All @@ -20,11 +22,11 @@
*
* {@codesnippet com.azure.core.http.rest.pagedIterable.iterableByPage.while}
*
* @param <T> The type of value contained in this {@link IterableResponse}.
* @param <T> The type of value contained in this {@link IterableStream}.
* @see PagedResponse
* @see IterableResponse
* @see IterableStream
*/
public class PagedIterable<T> extends IterableResponse<T> {
public class PagedIterable<T> extends IterableStream<T> {
private final PagedFlux<T> pagedFlux;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.http.rest;
package com.azure.core.util;

import reactor.core.publisher.Flux;

import java.util.Iterator;
import java.util.stream.Stream;

/**
* This class provides utility to iterate over values. All the values are preserved even if they are traversed multiple times.
* This class provides utility to iterate over values using standard 'for-each' style loops, or to convert them into a
* Stream and operate in that fashion. All the values are preserved even if they are traversed multiple times.
*
* <p><strong>Code sample using Stream</strong></p>
*
* {@codesnippet com.azure.core.http.rest.iterableResponse.stream}
* {@codesnippet com.azure.core.util.iterableStream.stream}
*
* <p><strong>Code sample using Iterator</strong></p>
*
* {@codesnippet com.azure.core.http.rest.iterableResponse.iterator.while}
* {@codesnippet com.azure.core.util.iterableStream.iterator.while}
*
* <p><strong>Code sample using Stream and filter</strong></p>
*
* {@codesnippet com.azure.core.http.rest.iterableResponse.stream.filter}
* {@codesnippet com.azure.core.util.iterableStream.stream.filter}
*
* @param <T> The type of value in this {@link Iterable}.
* @see Iterable
*/
public class IterableResponse<T> implements Iterable<T> {
public class IterableStream<T> implements Iterable<T> {
private final Flux<T> flux;

/**
* Creates instance given {@link Flux}.
* @param flux to iterate over
*/
public IterableResponse(Flux<T> flux) {
public IterableStream(Flux<T> flux) {
this.flux = flux;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.http.rest;
package com.azure.core.util;

import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpMethod;
Expand All @@ -18,9 +18,9 @@
import java.util.stream.IntStream;

/**
* Code snippets for {@link IterableResponse}
* Code snippets for {@link IterableStream}
*/
public class IterableResponseJavaDocCodeSnippets {
public class IterableStreamJavaDocCodeSnippets {

/**
* Iterate over {@link java.util.stream.Stream}
Expand All @@ -33,20 +33,20 @@ public void streamSnippet() throws MalformedURLException {

String deserializedHeaders = "header1,value1,header2,value2";

IterableResponse<PagedResponseBase<String, Integer>> myIterableResponse =
new IterableResponse<>(Flux.just(createPagedResponse(httpRequest, httpHeaders, deserializedHeaders, 1, 3)));
IterableStream<PagedResponseBase<String, Integer>> myIterableStream =
new IterableStream<>(Flux.just(createPagedResponse(httpRequest, httpHeaders, deserializedHeaders, 1, 3)));

// BEGIN: com.azure.core.http.rest.iterableResponse.stream
// BEGIN: com.azure.core.util.iterableStream.stream
// process the stream
myIterableResponse.stream().forEach(resp -> {
myIterableStream.stream().forEach(resp -> {
if (resp.statusCode() == HttpURLConnection.HTTP_OK) {
System.out.printf("Response headers are %s. Url %s%n", resp.deserializedHeaders(), resp.request().url());
resp.items().forEach(value -> {
System.out.printf("Response value is %d%n", value);
});
}
});
// END: com.azure.core.http.rest.iterableResponse.stream
// END: com.azure.core.util.iterableStream.stream
}

/**
Expand All @@ -60,12 +60,12 @@ public void iteratorwhileSnippet() throws MalformedURLException {

String deserializedHeaders = "header1,value1,header2,value2";

IterableResponse<PagedResponseBase<String, Integer>> myIterableResponse =
new IterableResponse<>(Flux.just(createPagedResponse(httpRequest, httpHeaders, deserializedHeaders, 1, 3)));
IterableStream<PagedResponseBase<String, Integer>> myIterableStream =
new IterableStream<>(Flux.just(createPagedResponse(httpRequest, httpHeaders, deserializedHeaders, 1, 3)));

// BEGIN: com.azure.core.http.rest.iterableResponse.iterator.while
// BEGIN: com.azure.core.util.iterableStream.iterator.while
// Iterate over iterator
Iterator<PagedResponseBase<String, Integer>> ite = myIterableResponse.iterator();
Iterator<PagedResponseBase<String, Integer>> ite = myIterableStream.iterator();
while (ite.hasNext()) {
PagedResponseBase<String, Integer> resp = ite.next();
if (resp.statusCode() == HttpURLConnection.HTTP_OK) {
Expand All @@ -75,7 +75,7 @@ public void iteratorwhileSnippet() throws MalformedURLException {
});
}
}
// END: com.azure.core.http.rest.iterableResponse.iterator.while
// END: com.azure.core.util.iterableStream.iterator.while
}

/**
Expand All @@ -89,20 +89,20 @@ public void iteratorStreamFilterSnippet() throws MalformedURLException {

String deserializedHeaders = "header1,value1,header2,value2";

IterableResponse<PagedResponseBase<String, Integer>> myIterableResponse =
new IterableResponse<>(Flux.just(createPagedResponse(httpRequest, httpHeaders, deserializedHeaders, 1, 3)));
IterableStream<PagedResponseBase<String, Integer>> myIterableStream =
new IterableStream<>(Flux.just(createPagedResponse(httpRequest, httpHeaders, deserializedHeaders, 1, 3)));

// BEGIN: com.azure.core.http.rest.iterableResponse.stream.filter
// BEGIN: com.azure.core.util.iterableStream.stream.filter
// process the stream
myIterableResponse.stream().filter(resp -> resp.statusCode() == HttpURLConnection.HTTP_OK)
myIterableStream.stream().filter(resp -> resp.statusCode() == HttpURLConnection.HTTP_OK)
.limit(10)
.forEach(resp -> {
System.out.printf("Response headers are %s. Url %s%n", resp.deserializedHeaders(), resp.request().url());
resp.items().forEach(value -> {
System.out.printf("Response value is %d%n", value);
});
});
// END: com.azure.core.http.rest.iterableResponse.stream.filter
// END: com.azure.core.util.iterableStream.stream.filter
}

private PagedResponseBase<String, Integer> createPagedResponse(HttpRequest httpRequest, HttpHeaders httpHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.stream.Collectors;

import com.azure.core.util.IterableStream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -30,7 +31,7 @@ public void setup() {
/*Ensure that if we call stream multiple times, it always returns same values and they are same as original list of values.*/
@Test
public void testIterableResponseStreamFromStart() {
IterableResponse<Integer> iterableResponse = getIntegerIterableResponse(2, 5);
IterableStream<Integer> iterableResponse = getIntegerIterableResponse(2, 5);
Assert.assertEquals(iterableResponse.stream().collect(Collectors.toList()).size(), iterableResponse.stream().collect(Collectors.toList()).size());

// ensure original list of values are same after calling iterator()
Expand All @@ -41,7 +42,7 @@ public void testIterableResponseStreamFromStart() {
/*Ensure that if we call iterator multiple times, it always returns same values and they are same as original list of values.*/
@Test
public void testIterableResponseIteratorFromStart() {
IterableResponse<Integer> iterableResponse = getIntegerIterableResponse(2, 5);
IterableStream<Integer> iterableResponse = getIntegerIterableResponse(2, 5);
List<Integer> actualNumberValues1 = new ArrayList<>();
List<Integer> actualNumberValues2 = new ArrayList<>();
iterableResponse.iterator().forEachRemaining(number -> actualNumberValues1.add(number));
Expand All @@ -53,8 +54,8 @@ public void testIterableResponseIteratorFromStart() {
iterableResponse.iterator().forEachRemaining(number -> Assert.assertTrue(originalIntegerList.contains(number)));
}

private IterableResponse<Integer> getIntegerIterableResponse(int startNumber, int noOfValues) {
private IterableStream<Integer> getIntegerIterableResponse(int startNumber, int noOfValues) {
Flux<Integer> integerFlux = Flux.range(startNumber, noOfValues);
return new IterableResponse<>(integerFlux);
return new IterableStream<>(integerFlux);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.azure.messaging.eventhubs;

import com.azure.core.amqp.RetryOptions;
import com.azure.core.http.rest.IterableResponse;
import com.azure.core.util.IterableStream;
import com.azure.core.implementation.annotation.ReturnType;
import com.azure.core.implementation.annotation.ServiceClient;
import com.azure.core.implementation.annotation.ServiceMethod;
Expand Down Expand Up @@ -67,8 +67,8 @@ public EventHubProperties getProperties() {
* @return The identifiers for all partitions of an Event Hub.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public IterableResponse<String> getPartitionIds() {
return new IterableResponse<>(client.getPartitionIds());
public IterableStream<String> getPartitionIds() {
return new IterableStream<>(client.getPartitionIds());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.azure.messaging.eventhubs;

import com.azure.core.http.rest.IterableResponse;
import com.azure.core.util.IterableStream;
import com.azure.messaging.eventhubs.models.EventHubConsumerOptions;
import com.azure.messaging.eventhubs.models.EventPosition;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -48,8 +48,8 @@ public class EventHubConsumer implements Closeable {
* @return A set of {@link EventData} that was received. The iterable contains up to {@code maximumMessageCount}
* events.
*/
public IterableResponse<EventData> receive(int maximumMessageCount) {
return new IterableResponse<>(Flux.empty());
public IterableStream<EventData> receive(int maximumMessageCount) {
return new IterableStream<>(Flux.empty());
}

/**
Expand All @@ -61,8 +61,8 @@ public IterableResponse<EventData> receive(int maximumMessageCount) {
* @return A set of {@link EventData} that was received. The iterable contains up to {@code maximumMessageCount}
* events.
*/
public IterableResponse<EventData> receive(int maximumMessageCount, Duration maximumWaitTime) {
return new IterableResponse<>(Flux.empty());
public IterableStream<EventData> receive(int maximumMessageCount, Duration maximumWaitTime) {
return new IterableStream<>(Flux.empty());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.azure.messaging.eventhubs;

import com.azure.core.http.rest.IterableResponse;
import com.azure.core.util.IterableStream;
import com.azure.core.util.logging.ClientLogger;
import com.azure.messaging.eventhubs.implementation.ApiTestBase;
import com.azure.messaging.eventhubs.implementation.ConnectionStringProperties;
Expand Down Expand Up @@ -52,7 +52,7 @@ protected void afterTest() {
@Test
public void getPartitionIds() {
// Act
final IterableResponse<String> response = client.getPartitionIds();
final IterableStream<String> response = client.getPartitionIds();

// Assert
Assert.assertNotNull(response);
Expand Down

0 comments on commit 56dc993

Please sign in to comment.