Skip to content

Commit 163efa3

Browse files
Ashok-Varmatemawi
authored andcommitted
cronet: Update to Java-8 API's and tighten the scopes
1 parent c703a1e commit 163efa3

File tree

5 files changed

+19
-37
lines changed

5 files changed

+19
-37
lines changed

cronet/src/main/java/io/grpc/cronet/CronetChannelBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static CronetChannelBuilder forAddress(String name, int port) {
8484

8585
private final CronetEngine cronetEngine;
8686
private final ManagedChannelImplBuilder managedChannelImplBuilder;
87-
private TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
87+
private final TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
8888

8989
private boolean alwaysUsePut = false;
9090

@@ -132,7 +132,7 @@ protected ManagedChannelBuilder<?> delegate() {
132132
* Sets the maximum message size allowed to be received on the channel. If not called,
133133
* defaults to {@link io.grpc.internal.GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE}.
134134
*/
135-
public final CronetChannelBuilder maxMessageSize(int maxMessageSize) {
135+
public CronetChannelBuilder maxMessageSize(int maxMessageSize) {
136136
checkArgument(maxMessageSize >= 0, "maxMessageSize must be >= 0");
137137
this.maxMessageSize = maxMessageSize;
138138
return this;
@@ -141,7 +141,7 @@ public final CronetChannelBuilder maxMessageSize(int maxMessageSize) {
141141
/**
142142
* Sets the Cronet channel to always use PUT instead of POST. Defaults to false.
143143
*/
144-
public final CronetChannelBuilder alwaysUsePut(boolean enable) {
144+
public CronetChannelBuilder alwaysUsePut(boolean enable) {
145145
this.alwaysUsePut = enable;
146146
return this;
147147
}
@@ -163,7 +163,7 @@ public final CronetChannelBuilder alwaysUsePut(boolean enable) {
163163
* application.
164164
* @return the builder to facilitate chaining.
165165
*/
166-
final CronetChannelBuilder setTrafficStatsTag(int tag) {
166+
CronetChannelBuilder setTrafficStatsTag(int tag) {
167167
trafficStatsTagSet = true;
168168
trafficStatsTag = tag;
169169
return this;
@@ -184,7 +184,7 @@ final CronetChannelBuilder setTrafficStatsTag(int tag) {
184184
* @param uid the UID to attribute socket traffic caused by this channel.
185185
* @return the builder to facilitate chaining.
186186
*/
187-
final CronetChannelBuilder setTrafficStatsUid(int uid) {
187+
CronetChannelBuilder setTrafficStatsUid(int uid) {
188188
trafficStatsUidSet = true;
189189
trafficStatsUid = uid;
190190
return this;
@@ -200,7 +200,7 @@ final CronetChannelBuilder setTrafficStatsUid(int uid) {
200200
*
201201
* @since 1.12.0
202202
*/
203-
public final CronetChannelBuilder scheduledExecutorService(
203+
public CronetChannelBuilder scheduledExecutorService(
204204
ScheduledExecutorService scheduledExecutorService) {
205205
this.scheduledExecutorService =
206206
checkNotNull(scheduledExecutorService, "scheduledExecutorService");

cronet/src/main/java/io/grpc/cronet/CronetClientStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public void cancel(Status reason) {
247247
class TransportState extends Http2ClientStreamTransportState {
248248
private final Object lock;
249249
@GuardedBy("lock")
250-
private Collection<PendingData> pendingData = new ArrayList<PendingData>();
250+
private final Collection<PendingData> pendingData = new ArrayList<>();
251251
@GuardedBy("lock")
252252
private boolean streamReady;
253253
@GuardedBy("lock")

cronet/src/main/java/io/grpc/cronet/CronetClientTransport.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ class CronetClientTransport implements ConnectionClientTransport {
5656
private final Object lock = new Object();
5757
@GuardedBy("lock")
5858
private final Set<CronetClientStream> streams = Collections.newSetFromMap(
59-
new IdentityHashMap<CronetClientStream, Boolean>());
59+
new IdentityHashMap<>());
6060
private final Executor executor;
6161
private final int maxMessageSize;
6262
private final boolean alwaysUsePut;
6363
private final TransportTracer transportTracer;
6464
private Attributes attrs;
6565
private final boolean useGetForSafeMethods;
6666
private final boolean usePutForIdempotentMethods;
67+
private final StreamBuilderFactory streamFactory;
6768
// Indicates the transport is in go-away state: no new streams will be processed,
6869
// but existing streams may continue.
6970
@GuardedBy("lock")
@@ -79,7 +80,6 @@ class CronetClientTransport implements ConnectionClientTransport {
7980
@GuardedBy("lock")
8081
// Whether this transport has started.
8182
private boolean started;
82-
private StreamBuilderFactory streamFactory;
8383

8484
CronetClientTransport(
8585
StreamBuilderFactory streamFactory,
@@ -205,9 +205,9 @@ public void shutdownNow(Status status) {
205205
// streams.remove()
206206
streamsCopy = new ArrayList<>(streams);
207207
}
208-
for (int i = 0; i < streamsCopy.size(); i++) {
208+
for (CronetClientStream cronetClientStream : streamsCopy) {
209209
// Avoid deadlock by calling into stream without lock held
210-
streamsCopy.get(i).cancel(status);
210+
cronetClientStream.cancel(status);
211211
}
212212
stopIfNecessary();
213213
}
@@ -255,7 +255,7 @@ public InternalLogId getLogId() {
255255
*/
256256
void stopIfNecessary() {
257257
synchronized (lock) {
258-
if (goAway && !stopped && streams.size() == 0) {
258+
if (goAway && !stopped && streams.isEmpty()) {
259259
stopped = true;
260260
} else {
261261
return;

cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ public final class CronetClientStreamTest {
8080
@Mock private BidirectionalStream.Builder builder;
8181
private final Object lock = new Object();
8282
private final TransportTracer transportTracer = TransportTracer.getDefaultFactory().create();
83-
private final Executor executor = new Executor() {
84-
@Override
85-
public void execute(Runnable r) {
86-
r.run();
87-
}
88-
};
83+
private final Executor executor = Runnable::run;
8984
CronetClientStream clientStream;
9085

9186
private MethodDescriptor.Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
@@ -171,7 +166,7 @@ public void write() {
171166
String[] requests = new String[5];
172167
WritableBuffer[] buffers = new WritableBuffer[5];
173168
for (int i = 0; i < 5; ++i) {
174-
requests[i] = new String("request" + String.valueOf(i));
169+
requests[i] = "request" + i;
175170
buffers[i] = allocator.allocate(requests[i].length());
176171
buffers[i].write(requests[i].getBytes(Charset.forName("UTF-8")), 0, requests[i].length());
177172
// The 3rd and 5th writeFrame calls have flush=true.
@@ -206,27 +201,19 @@ public void write() {
206201
}
207202

208203
private static List<Map.Entry<String, String>> responseHeader(String status) {
209-
Map<String, String> headers = new HashMap<String, String>();
204+
Map<String, String> headers = new HashMap<>();
210205
headers.put(":status", status);
211206
headers.put("content-type", "application/grpc");
212207
headers.put("test-key", "test-value");
213-
List<Map.Entry<String, String>> headerList = new ArrayList<Map.Entry<String, String>>(3);
214-
for (Map.Entry<String, String> entry : headers.entrySet()) {
215-
headerList.add(entry);
216-
}
217-
return headerList;
208+
return new ArrayList<>(headers.entrySet());
218209
}
219210

220211
private static List<Map.Entry<String, String>> trailers(int status) {
221-
Map<String, String> trailers = new HashMap<String, String>();
212+
Map<String, String> trailers = new HashMap<>();
222213
trailers.put("grpc-status", String.valueOf(status));
223214
trailers.put("content-type", "application/grpc");
224215
trailers.put("test-trailer-key", "test-trailer-value");
225-
List<Map.Entry<String, String>> trailerList = new ArrayList<Map.Entry<String, String>>(3);
226-
for (Map.Entry<String, String> entry : trailers.entrySet()) {
227-
trailerList.add(entry);
228-
}
229-
return trailerList;
216+
return new ArrayList<>(trailers.entrySet());
230217
}
231218

232219
private static ByteBuffer createMessageFrame(byte[] bytes) {

cronet/src/test/java/io/grpc/cronet/CronetClientTransportTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ public final class CronetClientTransportTest {
7171
private MethodDescriptor<Void, Void> descriptor = TestMethodDescriptors.voidMethod();
7272
@Mock private ManagedClientTransport.Listener clientTransportListener;
7373
@Mock private BidirectionalStream.Builder builder;
74-
private final Executor executor = new Executor() {
75-
@Override
76-
public void execute(Runnable r) {
77-
r.run();
78-
}
79-
};
74+
private final Executor executor = Runnable::run;
8075

8176
@Before
8277
public void setUp() {

0 commit comments

Comments
 (0)