diff --git a/core/src/main/java/com/google/net/stubby/MethodDescriptor.java b/core/src/main/java/com/google/net/stubby/MethodDescriptor.java index 377ece024d..d5671fa4ed 100644 --- a/core/src/main/java/com/google/net/stubby/MethodDescriptor.java +++ b/core/src/main/java/com/google/net/stubby/MethodDescriptor.java @@ -1,15 +1,14 @@ package com.google.net.stubby; -import com.google.common.base.Function; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import javax.inject.Provider; @@ -27,20 +26,12 @@ public class MethodDescriptor { UNKNOWN } - private static final Function,String> HEADER_SNAPSHOT = - new Function, String>() { - @Override - public String apply(@Nullable Provider headerProvider) { - return headerProvider == null ? null : headerProvider.get(); - } - }; - private final Type type; private final String name; private final Marshaller requestMarshaller; private final Marshaller responseMarshaller; private final long timeoutMicros; - private final ImmutableMap> headers; + private final Map> headers; public static MethodDescriptor create( Type type, String name, long timeout, TimeUnit timeoutUnit, @@ -48,20 +39,20 @@ public class MethodDescriptor { Marshaller responseMarshaller) { return new MethodDescriptor( type, name, timeoutUnit.toMicros(timeout), requestMarshaller, responseMarshaller, - ImmutableMap.>of()); + Collections.>emptyMap()); } private MethodDescriptor(Type type, String name, long timeoutMicros, Marshaller requestMarshaller, Marshaller responseMarshaller, - ImmutableMap> headers) { + Map> headers) { this.type = Preconditions.checkNotNull(type); this.name = name; Preconditions.checkArgument(timeoutMicros > 0); this.timeoutMicros = timeoutMicros; this.requestMarshaller = requestMarshaller; this.responseMarshaller = responseMarshaller; - this.headers = headers; + this.headers = Collections.unmodifiableMap(headers); } /** @@ -90,9 +81,13 @@ public class MethodDescriptor { */ public Map getHeaders() { if (headers.isEmpty()) { - return ImmutableMap.of(); + return Collections.emptyMap(); } - return ImmutableMap.copyOf(Maps.transformValues(headers, HEADER_SNAPSHOT)); + Map snapshot = new HashMap(); + for (Entry> entry : headers.entrySet()) { + snapshot.put(entry.getKey(), entry.getValue().get()); + } + return Collections.unmodifiableMap(snapshot); } /** @@ -122,20 +117,20 @@ public class MethodDescriptor { */ public MethodDescriptor withHeader(String headerName, Provider headerValueProvider) { + Map> newHeaders = new HashMap>(headers); + newHeaders.put(headerName, headerValueProvider); return new MethodDescriptor(type, name, timeoutMicros, - requestMarshaller, responseMarshaller, - ImmutableMap.>builder(). - putAll(headers).put(headerName, headerValueProvider).build()); + requestMarshaller, responseMarshaller, newHeaders); } /** * Creates a new descriptor with additional bound headers. */ public MethodDescriptor withHeaders( - ImmutableMap> additionalHeaders) { + Map> additionalHeaders) { + Map> newHeaders = new HashMap>(headers); + newHeaders.putAll(additionalHeaders); return new MethodDescriptor(type, name, timeoutMicros, - requestMarshaller, responseMarshaller, - ImmutableMap.>builder(). - putAll(headers).putAll(additionalHeaders).build()); + requestMarshaller, responseMarshaller, newHeaders); } } diff --git a/core/src/main/java/com/google/net/stubby/newtransport/okhttp/OkHttpClientTransport.java b/core/src/main/java/com/google/net/stubby/newtransport/okhttp/OkHttpClientTransport.java index f90957d3ae..22350b6f7d 100644 --- a/core/src/main/java/com/google/net/stubby/newtransport/okhttp/OkHttpClientTransport.java +++ b/core/src/main/java/com/google/net/stubby/newtransport/okhttp/OkHttpClientTransport.java @@ -2,7 +2,6 @@ package com.google.net.stubby.newtransport.okhttp; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableMap; import com.google.common.io.ByteStreams; import com.google.common.util.concurrent.ListenableFuture; import com.google.net.stubby.MethodDescriptor; @@ -52,26 +51,37 @@ public class OkHttpClientTransport extends AbstractClientTransport { @VisibleForTesting static final int DEFAULT_INITIAL_WINDOW_SIZE = 64 * 1024; - private static final ImmutableMap ERROR_CODE_TO_STATUS = ImmutableMap - .builder() - .put(ErrorCode.NO_ERROR, Status.OK) - .put(ErrorCode.PROTOCOL_ERROR, new Status(Transport.Code.INTERNAL, "Protocol error")) - .put(ErrorCode.INVALID_STREAM, new Status(Transport.Code.INTERNAL, "Invalid stream")) - .put(ErrorCode.UNSUPPORTED_VERSION, - new Status(Transport.Code.INTERNAL, "Unsupported version")) - .put(ErrorCode.STREAM_IN_USE, new Status(Transport.Code.INTERNAL, "Stream in use")) - .put(ErrorCode.STREAM_ALREADY_CLOSED, - new Status(Transport.Code.INTERNAL, "Stream already closed")) - .put(ErrorCode.INTERNAL_ERROR, new Status(Transport.Code.INTERNAL, "Internal error")) - .put(ErrorCode.FLOW_CONTROL_ERROR, new Status(Transport.Code.INTERNAL, "Flow control error")) - .put(ErrorCode.STREAM_CLOSED, new Status(Transport.Code.INTERNAL, "Stream closed")) - .put(ErrorCode.FRAME_TOO_LARGE, new Status(Transport.Code.INTERNAL, "Frame too large")) - .put(ErrorCode.REFUSED_STREAM, new Status(Transport.Code.INTERNAL, "Refused stream")) - .put(ErrorCode.CANCEL, new Status(Transport.Code.CANCELLED, "Cancelled")) - .put(ErrorCode.COMPRESSION_ERROR, new Status(Transport.Code.INTERNAL, "Compression error")) - .put(ErrorCode.INVALID_CREDENTIALS, - new Status(Transport.Code.PERMISSION_DENIED, "Invalid credentials")) - .build(); + private static final Map ERROR_CODE_TO_STATUS; + static { + Map errorToStatus = new HashMap(); + errorToStatus.put(ErrorCode.NO_ERROR, Status.OK); + errorToStatus.put(ErrorCode.PROTOCOL_ERROR, + new Status(Transport.Code.INTERNAL, "Protocol error")); + errorToStatus.put(ErrorCode.INVALID_STREAM, + new Status(Transport.Code.INTERNAL, "Invalid stream")); + errorToStatus.put(ErrorCode.UNSUPPORTED_VERSION, + new Status(Transport.Code.INTERNAL, "Unsupported version")); + errorToStatus.put(ErrorCode.STREAM_IN_USE, + new Status(Transport.Code.INTERNAL, "Stream in use")); + errorToStatus.put(ErrorCode.STREAM_ALREADY_CLOSED, + new Status(Transport.Code.INTERNAL, "Stream already closed")); + errorToStatus.put(ErrorCode.INTERNAL_ERROR, + new Status(Transport.Code.INTERNAL, "Internal error")); + errorToStatus.put(ErrorCode.FLOW_CONTROL_ERROR, + new Status(Transport.Code.INTERNAL, "Flow control error")); + errorToStatus.put(ErrorCode.STREAM_CLOSED, + 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 int port;