Skip to content

Commit

Permalink
Apply :spotlessApply
Browse files Browse the repository at this point in the history
  • Loading branch information
komamitsu committed Jan 20, 2024
1 parent ed71f06 commit 61a7c8b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,46 @@
import retrofit2.Converter;
import retrofit2.Retrofit;

public class MessagePackConverterFactory
extends Converter.Factory
{
public static MessagePackConverterFactory create()
{
return create(new ObjectMapper(new MessagePackFactory()));
public class MessagePackConverterFactory extends Converter.Factory {
public static MessagePackConverterFactory create() {
return create(new ObjectMapper(new MessagePackFactory()));
}

@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public static MessagePackConverterFactory create(ObjectMapper mapper) {
if (mapper == null) {
throw new IllegalArgumentException("'mapper' is null");
}

@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public static MessagePackConverterFactory create(ObjectMapper mapper)
{
if (mapper == null) {
throw new IllegalArgumentException("'mapper' is null");
}

if (!(mapper.getFactory() instanceof MessagePackFactory)) {
throw new IllegalArgumentException("'mapper' doesn't have MessagePackFactory");
}

return new MessagePackConverterFactory(mapper);
}

private final ObjectMapper mapper;

private MessagePackConverterFactory(ObjectMapper mapper)
{
this.mapper = mapper;
}

@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit)
{
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectWriter writer = mapper.writerFor(javaType);
return new MessagePackRequestBodyConverter<>(writer);
if (!(mapper.getFactory() instanceof MessagePackFactory)) {
throw new IllegalArgumentException("'mapper' doesn't have MessagePackFactory");
}

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type,
Annotation[] annotations, Retrofit retrofit)
{
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectReader reader = mapper.readerFor(javaType);
return new MessagePackResponseBodyConverter<>(reader);
}
return new MessagePackConverterFactory(mapper);
}

private final ObjectMapper mapper;

private MessagePackConverterFactory(ObjectMapper mapper) {
this.mapper = mapper;
}

@Override
public Converter<?, RequestBody> requestBodyConverter(
Type type,
Annotation[] parameterAnnotations,
Annotation[] methodAnnotations,
Retrofit retrofit) {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectWriter writer = mapper.writerFor(javaType);
return new MessagePackRequestBodyConverter<>(writer);
}

@Override
public Converter<ResponseBody, ?> responseBodyConverter(
Type type, Annotation[] annotations, Retrofit retrofit) {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectReader reader = mapper.readerFor(javaType);
return new MessagePackResponseBodyConverter<>(reader);
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package org.komamitsu.retrofit.converter.msgpack;

import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Converter;

import java.io.IOException;

class MessagePackRequestBodyConverter<T>
implements Converter<T, RequestBody>
{
private static final MediaType MEDIA_TYPE = MediaType.parse("application/x-msgpack; charset=UTF-8");
class MessagePackRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE =
MediaType.parse("application/x-msgpack; charset=UTF-8");

private final ObjectWriter adapter;
private final ObjectWriter adapter;

MessagePackRequestBodyConverter(ObjectWriter adapter)
{
this.adapter = adapter;
}
MessagePackRequestBodyConverter(ObjectWriter adapter) {
this.adapter = adapter;
}

@Override
public RequestBody convert(T value)
throws IOException
{
byte[] bytes = adapter.writeValueAsBytes(value);
return RequestBody.create(MEDIA_TYPE, bytes);
}
@Override
public RequestBody convert(T value) throws IOException {
byte[] bytes = adapter.writeValueAsBytes(value);
return RequestBody.create(MEDIA_TYPE, bytes);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package org.komamitsu.retrofit.converter.msgpack;

import com.fasterxml.jackson.databind.ObjectReader;
import okhttp3.ResponseBody;
import retrofit2.Converter;

import java.io.IOException;
import java.io.InputStream;
import okhttp3.ResponseBody;
import retrofit2.Converter;

class MessagePackResponseBodyConverter<T>
implements Converter<ResponseBody, T>
{
private final ObjectReader adapter;
class MessagePackResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final ObjectReader adapter;

MessagePackResponseBodyConverter(ObjectReader adapter)
{
this.adapter = adapter;
}
MessagePackResponseBodyConverter(ObjectReader adapter) {
this.adapter = adapter;
}

@Override
public T convert(ResponseBody value) throws IOException
{
try (InputStream in = value.byteStream()) {
return adapter.readValue(in);
}
@Override
public T convert(ResponseBody value) throws IOException {
try (InputStream in = value.byteStream()) {
return adapter.readValue(in);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.komamitsu.retrofit.converter.msgpack;

import com.fasterxml.jackson.annotation.JsonCreator;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
Expand All @@ -18,114 +20,96 @@
import retrofit2.http.Body;
import retrofit2.http.POST;

import java.io.IOException;
public class MessagePackConverterFactoryTest {
@Rule public final MockWebServer server = new MockWebServer();
private Service service;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
public static class Pojo {
private final int i;
private final float f;
private final String s;

public class MessagePackConverterFactoryTest
{
@Rule
public final MockWebServer server = new MockWebServer();
private Service service;

public static class Pojo
{
private final int i;
private final float f;
private final String s;

public Pojo(
@JsonProperty("i") int i,
@JsonProperty("f") float f,
@JsonProperty("s") String s)
{
this.i = i;
this.f = f;
this.s = s;
}

@JsonProperty("i")
public int getI()
{
return i;
}

@JsonProperty("f")
public float getF()
{
return f;
}

@JsonProperty("s")
public String getS()
{
return s;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Pojo pojo = (Pojo) o;

if (i != pojo.i) {
return false;
}
if (Float.compare(pojo.f, f) != 0) {
return false;
}
return s != null ? s.equals(pojo.s) : pojo.s == null;
}

@Override
public int hashCode()
{
int result = i;
result = 31 * result + (f != +0.0f ? Float.floatToIntBits(f) : 0);
result = 31 * result + (s != null ? s.hashCode() : 0);
return result;
}
public Pojo(@JsonProperty("i") int i, @JsonProperty("f") float f, @JsonProperty("s") String s) {
this.i = i;
this.f = f;
this.s = s;
}

interface Service
{
@POST("/")
Call<Pojo> postPojo(@Body Pojo pojo);
@JsonProperty("i")
public int getI() {
return i;
}

@Before
public void setUp()
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(MessagePackConverterFactory.create())
.build();
service = retrofit.create(Service.class);
@JsonProperty("f")
public float getF() {
return f;
}

@Test
public void requestBodyConverter()
throws IOException, InterruptedException
{
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());

Pojo requestPojo = new Pojo(42, (float) Math.PI, "Hello");
Pojo responsePojo = new Pojo(99, 1.23f, "World");
server.enqueue(new MockResponse().setBody(
new Buffer().write(objectMapper.writeValueAsBytes(responsePojo))));
@JsonProperty("s")
public String getS() {
return s;
}

Response<Pojo> response = service.postPojo(requestPojo).execute();
assertThat(response.body(), is(responsePojo));
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Pojo pojo = (Pojo) o;

if (i != pojo.i) {
return false;
}
if (Float.compare(pojo.f, f) != 0) {
return false;
}
return s != null ? s.equals(pojo.s) : pojo.s == null;
}

RecordedRequest recordedRequest = server.takeRequest();
Pojo recordedPojo = objectMapper.readValue(recordedRequest.getBody().readByteArray(), Pojo.class);
assertThat(recordedPojo, is(requestPojo));
@Override
public int hashCode() {
int result = i;
result = 31 * result + (f != +0.0f ? Float.floatToIntBits(f) : 0);
result = 31 * result + (s != null ? s.hashCode() : 0);
return result;
}
}

interface Service {
@POST("/")
Call<Pojo> postPojo(@Body Pojo pojo);
}

@Before
public void setUp() {
Retrofit retrofit =
new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(MessagePackConverterFactory.create())
.build();
service = retrofit.create(Service.class);
}

@Test
public void requestBodyConverter() throws IOException, InterruptedException {
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());

Pojo requestPojo = new Pojo(42, (float) Math.PI, "Hello");
Pojo responsePojo = new Pojo(99, 1.23f, "World");
server.enqueue(
new MockResponse()
.setBody(new Buffer().write(objectMapper.writeValueAsBytes(responsePojo))));

Response<Pojo> response = service.postPojo(requestPojo).execute();
assertThat(response.body(), is(responsePojo));

RecordedRequest recordedRequest = server.takeRequest();
Pojo recordedPojo =
objectMapper.readValue(recordedRequest.getBody().readByteArray(), Pojo.class);
assertThat(recordedPojo, is(requestPojo));
}
}

0 comments on commit 61a7c8b

Please sign in to comment.