Remove usage of ImmutableXX collectors from calsses that GRpc Android version will care.

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=74880326
This commit is contained in:
simonma 2014-09-05 11:02:52 -07:00 committed by Eric Anderson
parent 18eb63bc16
commit 8f3e9eede7
2 changed files with 51 additions and 46 deletions

View File

@ -1,15 +1,14 @@
package com.google.net.stubby; package com.google.net.stubby;
import com.google.common.base.Function;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import javax.inject.Provider; import javax.inject.Provider;
@ -27,20 +26,12 @@ public class MethodDescriptor<RequestT, ResponseT> {
UNKNOWN UNKNOWN
} }
private static final Function<Provider<String>,String> HEADER_SNAPSHOT =
new Function<Provider<String>, String>() {
@Override
public String apply(@Nullable Provider<String> headerProvider) {
return headerProvider == null ? null : headerProvider.get();
}
};
private final Type type; private final Type type;
private final String name; private final String name;
private final Marshaller<RequestT> requestMarshaller; private final Marshaller<RequestT> requestMarshaller;
private final Marshaller<ResponseT> responseMarshaller; private final Marshaller<ResponseT> responseMarshaller;
private final long timeoutMicros; private final long timeoutMicros;
private final ImmutableMap<String, Provider<String>> headers; private final Map<String, Provider<String>> headers;
public static <RequestT, ResponseT> MethodDescriptor<RequestT, ResponseT> create( public static <RequestT, ResponseT> MethodDescriptor<RequestT, ResponseT> create(
Type type, String name, long timeout, TimeUnit timeoutUnit, Type type, String name, long timeout, TimeUnit timeoutUnit,
@ -48,20 +39,20 @@ public class MethodDescriptor<RequestT, ResponseT> {
Marshaller<ResponseT> responseMarshaller) { Marshaller<ResponseT> responseMarshaller) {
return new MethodDescriptor<RequestT, ResponseT>( return new MethodDescriptor<RequestT, ResponseT>(
type, name, timeoutUnit.toMicros(timeout), requestMarshaller, responseMarshaller, type, name, timeoutUnit.toMicros(timeout), requestMarshaller, responseMarshaller,
ImmutableMap.<String, Provider<String>>of()); Collections.<String, Provider<String>>emptyMap());
} }
private MethodDescriptor(Type type, String name, long timeoutMicros, private MethodDescriptor(Type type, String name, long timeoutMicros,
Marshaller<RequestT> requestMarshaller, Marshaller<RequestT> requestMarshaller,
Marshaller<ResponseT> responseMarshaller, Marshaller<ResponseT> responseMarshaller,
ImmutableMap<String, Provider<String>> headers) { Map<String, Provider<String>> headers) {
this.type = Preconditions.checkNotNull(type); this.type = Preconditions.checkNotNull(type);
this.name = name; this.name = name;
Preconditions.checkArgument(timeoutMicros > 0); Preconditions.checkArgument(timeoutMicros > 0);
this.timeoutMicros = timeoutMicros; this.timeoutMicros = timeoutMicros;
this.requestMarshaller = requestMarshaller; this.requestMarshaller = requestMarshaller;
this.responseMarshaller = responseMarshaller; this.responseMarshaller = responseMarshaller;
this.headers = headers; this.headers = Collections.unmodifiableMap(headers);
} }
/** /**
@ -90,9 +81,13 @@ public class MethodDescriptor<RequestT, ResponseT> {
*/ */
public Map<String, String> getHeaders() { public Map<String, String> getHeaders() {
if (headers.isEmpty()) { if (headers.isEmpty()) {
return ImmutableMap.of(); return Collections.emptyMap();
} }
return ImmutableMap.copyOf(Maps.transformValues(headers, HEADER_SNAPSHOT)); Map<String, String> snapshot = new HashMap<String, String>();
for (Entry<String, Provider<String>> entry : headers.entrySet()) {
snapshot.put(entry.getKey(), entry.getValue().get());
}
return Collections.unmodifiableMap(snapshot);
} }
/** /**
@ -122,20 +117,20 @@ public class MethodDescriptor<RequestT, ResponseT> {
*/ */
public MethodDescriptor<RequestT, ResponseT> withHeader(String headerName, public MethodDescriptor<RequestT, ResponseT> withHeader(String headerName,
Provider<String> headerValueProvider) { Provider<String> headerValueProvider) {
Map<String, Provider<String>> newHeaders = new HashMap<String, Provider<String>>(headers);
newHeaders.put(headerName, headerValueProvider);
return new MethodDescriptor<RequestT, ResponseT>(type, name, timeoutMicros, return new MethodDescriptor<RequestT, ResponseT>(type, name, timeoutMicros,
requestMarshaller, responseMarshaller, requestMarshaller, responseMarshaller, newHeaders);
ImmutableMap.<String, Provider<String>>builder().
putAll(headers).put(headerName, headerValueProvider).build());
} }
/** /**
* Creates a new descriptor with additional bound headers. * Creates a new descriptor with additional bound headers.
*/ */
public MethodDescriptor<RequestT, ResponseT> withHeaders( public MethodDescriptor<RequestT, ResponseT> withHeaders(
ImmutableMap<String, Provider<String>> additionalHeaders) { Map<String, Provider<String>> additionalHeaders) {
Map<String, Provider<String>> newHeaders = new HashMap<String, Provider<String>>(headers);
newHeaders.putAll(additionalHeaders);
return new MethodDescriptor<RequestT, ResponseT>(type, name, timeoutMicros, return new MethodDescriptor<RequestT, ResponseT>(type, name, timeoutMicros,
requestMarshaller, responseMarshaller, requestMarshaller, responseMarshaller, newHeaders);
ImmutableMap.<String, Provider<String>>builder().
putAll(headers).putAll(additionalHeaders).build());
} }
} }

View File

@ -2,7 +2,6 @@ package com.google.net.stubby.newtransport.okhttp;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.net.stubby.MethodDescriptor; import com.google.net.stubby.MethodDescriptor;
@ -52,26 +51,37 @@ public class OkHttpClientTransport extends AbstractClientTransport {
@VisibleForTesting @VisibleForTesting
static final int DEFAULT_INITIAL_WINDOW_SIZE = 64 * 1024; static final int DEFAULT_INITIAL_WINDOW_SIZE = 64 * 1024;
private static final ImmutableMap<ErrorCode, Status> ERROR_CODE_TO_STATUS = ImmutableMap private static final Map<ErrorCode, Status> ERROR_CODE_TO_STATUS;
.<ErrorCode, Status>builder() static {
.put(ErrorCode.NO_ERROR, Status.OK) Map<ErrorCode, Status> errorToStatus = new HashMap<ErrorCode, Status>();
.put(ErrorCode.PROTOCOL_ERROR, new Status(Transport.Code.INTERNAL, "Protocol error")) errorToStatus.put(ErrorCode.NO_ERROR, Status.OK);
.put(ErrorCode.INVALID_STREAM, new Status(Transport.Code.INTERNAL, "Invalid stream")) errorToStatus.put(ErrorCode.PROTOCOL_ERROR,
.put(ErrorCode.UNSUPPORTED_VERSION, new Status(Transport.Code.INTERNAL, "Protocol error"));
new Status(Transport.Code.INTERNAL, "Unsupported version")) errorToStatus.put(ErrorCode.INVALID_STREAM,
.put(ErrorCode.STREAM_IN_USE, new Status(Transport.Code.INTERNAL, "Stream in use")) new Status(Transport.Code.INTERNAL, "Invalid stream"));
.put(ErrorCode.STREAM_ALREADY_CLOSED, errorToStatus.put(ErrorCode.UNSUPPORTED_VERSION,
new Status(Transport.Code.INTERNAL, "Stream already closed")) new Status(Transport.Code.INTERNAL, "Unsupported version"));
.put(ErrorCode.INTERNAL_ERROR, new Status(Transport.Code.INTERNAL, "Internal error")) errorToStatus.put(ErrorCode.STREAM_IN_USE,
.put(ErrorCode.FLOW_CONTROL_ERROR, new Status(Transport.Code.INTERNAL, "Flow control error")) new Status(Transport.Code.INTERNAL, "Stream in use"));
.put(ErrorCode.STREAM_CLOSED, new Status(Transport.Code.INTERNAL, "Stream closed")) errorToStatus.put(ErrorCode.STREAM_ALREADY_CLOSED,
.put(ErrorCode.FRAME_TOO_LARGE, new Status(Transport.Code.INTERNAL, "Frame too large")) new Status(Transport.Code.INTERNAL, "Stream already closed"));
.put(ErrorCode.REFUSED_STREAM, new Status(Transport.Code.INTERNAL, "Refused stream")) errorToStatus.put(ErrorCode.INTERNAL_ERROR,
.put(ErrorCode.CANCEL, new Status(Transport.Code.CANCELLED, "Cancelled")) new Status(Transport.Code.INTERNAL, "Internal error"));
.put(ErrorCode.COMPRESSION_ERROR, new Status(Transport.Code.INTERNAL, "Compression error")) errorToStatus.put(ErrorCode.FLOW_CONTROL_ERROR,
.put(ErrorCode.INVALID_CREDENTIALS, new Status(Transport.Code.INTERNAL, "Flow control error"));
new Status(Transport.Code.PERMISSION_DENIED, "Invalid credentials")) errorToStatus.put(ErrorCode.STREAM_CLOSED,
.build(); new Status(Transport.Code.INTERNAL, "Stream closed"));
errorToStatus.put(ErrorCode.FRAME_TOO_LARGE,
new Status(Transport.Code.INTERNAL, "Frame too large"));
errorToStatus.put(ErrorCode.REFUSED_STREAM,
new Status(Transport.Code.INTERNAL, "Refused stream"));
errorToStatus.put(ErrorCode.CANCEL, new Status(Transport.Code.CANCELLED, "Cancelled"));
errorToStatus.put(ErrorCode.COMPRESSION_ERROR,
new Status(Transport.Code.INTERNAL, "Compression error"));
errorToStatus.put(ErrorCode.INVALID_CREDENTIALS,
new Status(Transport.Code.PERMISSION_DENIED, "Invalid credentials"));
ERROR_CODE_TO_STATUS = Collections.unmodifiableMap(errorToStatus);
}
private final String host; private final String host;
private final int port; private final int port;