com.palantir.conjure.java.undertow.runtime.LazilyInitializedEncoding Maven / Gradle / Ivy
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.conjure.java.undertow.runtime;
import com.google.common.base.Suppliers;
import com.palantir.conjure.java.undertow.lib.Endpoint;
import com.palantir.conjure.java.undertow.lib.TypeMarker;
import com.palantir.logsafe.Preconditions;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.function.Supplier;
/**
* Wrapper around an {@link Encoding} which allows both {@link Serializer} and {@link Deserializer} instances
* to be created when they're first used. This avoids the up-front cost of creating jackson ObjectReader
* and ObjectWriter
instances for each request and response body type, for each encoding on service
* startup when many endpoints are only used with one encoding based on the clients that make requests.
* Note that this results in the first request to a given endpoint being more expensive than it would be
* otherwise, though this is already the case to an extent before the JIT compiler can optimize the path.
*/
final class LazilyInitializedEncoding implements Encoding {
private final Encoding delegate;
LazilyInitializedEncoding(Encoding delegate) {
this.delegate = Preconditions.checkNotNull(delegate, "Encoding is required");
}
@Override
public Serializer serializer(TypeMarker type) {
return new LazilyInitializedSerializer<>(() -> delegate.serializer(type));
}
@Override
public Serializer serializer(TypeMarker type, Endpoint endpoint) {
return new LazilyInitializedSerializer<>(() -> delegate.serializer(type, endpoint));
}
@Override
public Deserializer deserializer(TypeMarker type) {
return new LazilyInitializedDeserializer<>(() -> delegate.deserializer(type));
}
@Override
public Deserializer deserializer(TypeMarker type, Endpoint endpoint) {
return new LazilyInitializedDeserializer<>(() -> delegate.deserializer(type, endpoint));
}
@Override
public String getContentType() {
return delegate.getContentType();
}
@Override
public boolean supportsContentType(String contentType) {
return delegate.supportsContentType(contentType);
}
@Override
public String toString() {
return "LazilyInitializedEncoding{delegate=" + delegate + '}';
}
private static final class LazilyInitializedSerializer implements Serializer {
private final Supplier> delegate;
LazilyInitializedSerializer(Supplier> delegate) {
this.delegate = Suppliers.memoize(delegate::get);
}
@Override
public void serialize(T value, OutputStream output) throws IOException {
delegate.get().serialize(value, output);
}
@Override
public String toString() {
return "LazilyInitializedSerializer{" + delegate + '}';
}
}
private static final class LazilyInitializedDeserializer implements Deserializer {
private final Supplier> delegate;
LazilyInitializedDeserializer(Supplier> delegate) {
this.delegate = Suppliers.memoize(delegate::get);
}
@Override
public T deserialize(InputStream input) throws IOException {
return delegate.get().deserialize(input);
}
@Override
public String toString() {
return "LazilyInitializedDeserializer{" + delegate + '}';
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy