Module methanol

Class AdapterCodec


  • public final class AdapterCodec
    extends java.lang.Object
    A group of adapters (encoders & decoders), typically targeting different mapping schemes, that facilitates creating corresponding HttpRequest.BodyPublisher, HttpResponse.BodyHandler and HttpResponse.BodySubscriber implementations. The correct adapter is selected based on the object type and caller's BodyAdapter.Hints, typically containing the MediaType of the desired mapping format.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  AdapterCodec.Builder
      A builder of AdapterCodec instances.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<BodyAdapter.Decoder> decoders()
      Returns the list of decoders in this codec.
      <T> java.net.http.HttpResponse.BodyHandler<java.util.function.Supplier<T>> deferredHandlerOf​(TypeRef<T> typeRef, BodyAdapter.Hints hints)
      Returns a BodyHandler that lazily decodes the response body into an object of the given type.
      <T> java.net.http.HttpResponse.BodySubscriber<java.util.function.Supplier<T>> deferredSubscriberOf​(TypeRef<T> typeRef, BodyAdapter.Hints hints)
      Returns a HttpResponse.BodySubscriber that lazily decodes the response body into an object of the given type.
      java.util.List<BodyAdapter.Encoder> encoders()
      Returns the list of encoders in this codec.
      <T> java.net.http.HttpResponse.BodyHandler<T> handlerOf​(TypeRef<T> typeRef, BodyAdapter.Hints hints)
      Returns a HttpResponse.BodyHandler that decodes the response body into an object of the given type.
      static AdapterCodec installed()  
      static AdapterCodec.Builder newBuilder()
      Returns a new AdapterCodec.Builder.
      <T> java.net.http.HttpRequest.BodyPublisher publisherOf​(T value, BodyAdapter.Hints hints)
      Returns a HttpRequest.BodyPublisher that encodes the given object into a request body.
      <T> java.net.http.HttpRequest.BodyPublisher publisherOf​(T value, TypeRef<T> typeRef, BodyAdapter.Hints hints)
      Returns a HttpRequest.BodyPublisher that encodes the given object into a request body with respect to the given TypeRef.
      <T> java.net.http.HttpResponse.BodySubscriber<T> subscriberOf​(TypeRef<T> typeRef, BodyAdapter.Hints hints)
      Returns a HttpResponse.BodySubscriber that decodes the response body into an object of the given type.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • encoders

        public java.util.List<BodyAdapter.Encoder> encoders()
        Returns the list of encoders in this codec.
      • decoders

        public java.util.List<BodyAdapter.Decoder> decoders()
        Returns the list of decoders in this codec.
      • publisherOf

        public <T> java.net.http.HttpRequest.BodyPublisher publisherOf​(T value,
                                                                       BodyAdapter.Hints hints)
        Returns a HttpRequest.BodyPublisher that encodes the given object into a request body.
        Throws:
        java.lang.UnsupportedOperationException - if no encoder that supports encoding the given object or is compatible with the given hints' media type (if any) is found
      • publisherOf

        public <T> java.net.http.HttpRequest.BodyPublisher publisherOf​(T value,
                                                                       TypeRef<T> typeRef,
                                                                       BodyAdapter.Hints hints)
        Returns a HttpRequest.BodyPublisher that encodes the given object into a request body with respect to the given TypeRef.
        Throws:
        java.lang.UnsupportedOperationException - if no encoder that supports encoding the given object type or is compatible with the given hints' media type (if any) is found
        java.lang.IllegalArgumentException - if the given object is not an instance of the given TypeRef's raw type
      • subscriberOf

        public <T> java.net.http.HttpResponse.BodySubscriber<T> subscriberOf​(TypeRef<T> typeRef,
                                                                             BodyAdapter.Hints hints)
        Returns a HttpResponse.BodySubscriber that decodes the response body into an object of the given type.
        Throws:
        java.lang.UnsupportedOperationException - if no decoder that supports decoding to the given type or is compatible with the given hints' media type (if any) is found
      • deferredSubscriberOf

        public <T> java.net.http.HttpResponse.BodySubscriber<java.util.function.Supplier<T>> deferredSubscriberOf​(TypeRef<T> typeRef,
                                                                                                                  BodyAdapter.Hints hints)
        Returns a HttpResponse.BodySubscriber that lazily decodes the response body into an object of the given type.
        Throws:
        java.lang.UnsupportedOperationException - if no decoder that supports decoding to the given type or is compatible with the given hints' media type (if any) is found
      • handlerOf

        public <T> java.net.http.HttpResponse.BodyHandler<T> handlerOf​(TypeRef<T> typeRef,
                                                                       BodyAdapter.Hints hints)
        Returns a HttpResponse.BodyHandler that decodes the response body into an object of the given type. The decoder is selected based on the response body's media type as specified by the Content-Type header. If no such header exists, any decoder that supports decoding to the given type is selected.
        Throws:
        java.lang.UnsupportedOperationException - if no decoder that supports decoding to the given type is found
      • deferredHandlerOf

        public <T> java.net.http.HttpResponse.BodyHandler<java.util.function.Supplier<T>> deferredHandlerOf​(TypeRef<T> typeRef,
                                                                                                            BodyAdapter.Hints hints)
        Returns a BodyHandler that lazily decodes the response body into an object of the given type. The decoder is selected based on the response body's media type as specified by the Content-Type header. If no such header exists, any decoder that supports decoding to the given type is selected.
        Throws:
        java.lang.UnsupportedOperationException - if no decoder that supports decoding to the given type is found
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object