Skip to content

Commit

Permalink
add to javaslang
Browse files Browse the repository at this point in the history
  • Loading branch information
wonwoo committed Oct 16, 2018
1 parent db81f35 commit a80b18b
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 1 deletion.
6 changes: 6 additions & 0 deletions mpple-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<artifactId>joda-time</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.2</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ml.wonwoo.mapped.converter;

import io.vavr.collection.List;
import io.vavr.collection.Seq;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import ml.wonwoo.mapped.mapping.MappingInstance;
import ml.wonwoo.util.ClassUtils;
import net.jodah.typetools.TypeResolver;

public class JavasLangConverter implements MappedConverter {

private final MappingInstance mappingInstance;

public JavasLangConverter(MappingInstance mappingInstance) {
this.mappingInstance = mappingInstance;
}

@Override
public boolean supports(Class<?> target) {
return Seq.class.isAssignableFrom(target);
}

@SuppressWarnings("unchecked")
protected <T> Seq<T> collectionCreate(Class<T> clazz) {
if (clazz.isInterface()) {
return List.Nil.instance();
} else {
return (Seq<T>) ClassUtils.instantiateClass(clazz);
}
}

@Override
@SuppressWarnings("unchecked")
public Object convert(Class<?> rootClass, Object value, Class<?> target, Object context) {
try {
Method method = rootClass.getDeclaredMethod((String) context, target);
Type genericType = ClassUtils.getGenericType(method);
Class<?> type = TypeResolver.resolveRawArgument(genericType, rootClass);
Seq<Object> collection = collectionCreate((Class<Object>) target);
Seq<Object> list = (Seq) value;
for (Object obj : list) {
if (ClassUtils.isObject(type)) {
collection = collection.append(mappingInstance.map(obj, type));
} else {
collection = collection.append(obj);
}
}
return collection;
} catch (Exception e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ml.wonwoo.mapped.converter.ArrayConverter;
import ml.wonwoo.mapped.converter.CollectionConverter;
import ml.wonwoo.mapped.converter.EnumConverter;
import ml.wonwoo.mapped.converter.JavasLangConverter;
import ml.wonwoo.mapped.converter.MapConverter;
import ml.wonwoo.mapped.converter.MappedConverter;
import ml.wonwoo.mapped.converter.MappedConverterComposite;
Expand Down Expand Up @@ -48,6 +49,10 @@ private void defaultConverter() {
new ArrayConverter(this),
new EnumConverter()
);

if (ClassUtils.isJavasLang()) {
this.converter.addConverter(new JavasLangConverter(this));
}
}

public void addConverter(MappedConverter mappedConverter) {
Expand Down
5 changes: 4 additions & 1 deletion mpple-core/src/main/java/ml/wonwoo/util/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public abstract class ClassUtils {
javaType.put(CurrencyUnit.class, CurrencyUnit.class);
javaType.put(MonetaryAmount.class, MonetaryAmount.class);
}

wrapperType = Collections.unmodifiableMap(wrapper);
defaultJavaType = Collections.unmodifiableMap(javaType);

Expand All @@ -89,6 +88,10 @@ public static boolean isObject(Class<?> type) {
return true;
}

public static boolean isJavasLang() {
return isPresent("io.vavr.collection.Seq");
}

public static boolean isJava8() {
return isPresent("java.util.Optional");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ml.wonwoo.mapped.converter;


import static org.assertj.core.api.Assertions.assertThat;

import io.vavr.collection.List;
import io.vavr.collection.Seq;
import ml.wonwoo.mapped.mapping.MappingInstanceImpl;
import ml.wonwoo.model.Foo;
import org.junit.Test;

public class JavasLangConverterTests {


private final JavasLangConverter javasLangConverter
= new JavasLangConverter(new MappingInstanceImpl());

@Test
public void supports() {
assertThat(javasLangConverter.supports(List.class)).isTrue();
assertThat(javasLangConverter.supports(Seq.class)).isTrue();
assertThat(javasLangConverter.supports(Foo.class)).isFalse();
}

@Test
public void convertWrapperType() {
List<String> strs = List.of("test", "foo");
FooList fooList = new FooList();
fooList.setStrs(strs);
Object result = javasLangConverter.convert(FooList.class, strs, List.class, "setStrs");
List list = (List) result;
assertThat(list.get(0)).isEqualTo("test");
assertThat(list.get(1)).isEqualTo("foo");
}

@Test
public void convertObjectType() {
Foo foo = new Foo();
foo.setLastName("last");
foo.setFirstName("first");
List<Foo> foos = List.of(foo);
FooList fooList = new FooList();
fooList.setFoos(foos);
Object result = javasLangConverter.convert(FooList.class, foos, List.class, "setFoos");
List<Foo> list = (List) result;
assertThat(list.get(0).getLastName()).isEqualTo("last");
assertThat(list.get(0).getFirstName()).isEqualTo("first");
}


public static class FooList {

private List<String> strs;
private List<Foo> foos;

public List<String> getStrs() {
return strs;
}

public List<Foo> getFoos() {
return foos;
}

public void setStrs(List<String> strs) {
this.strs = strs;
}

public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
}
6 changes: 6 additions & 0 deletions mpple-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<artifactId>mpple-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.2</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ml.wonwoo.sample.domain;

import io.vavr.collection.List;
import java.util.Date;

public class JavasCustomer {

private Date date;

private List<OrderItem> items;

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public List<OrderItem> getItems() {
return items;
}

public void setItems(List<OrderItem> items) {
this.items = items;
}

@Override
public String toString() {
return "OrderDto{" +
"date=" + date +
", items=" + items +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ml.wonwoo.sample.dto;

import io.vavr.collection.List;
import java.util.Date;

public class JavasCustomerDto {

private Date date;

private List<OrderItemDto> items;

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public List<OrderItemDto> getItems() {
return items;
}

public void setItems(List<OrderItemDto> items) {
this.items = items;
}

@Override
public String toString() {
return "OrderDto{" +
"date=" + date +
", items=" + items +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ml.wonwoo.sample.mapper;

import ml.wonwoo.sample.domain.Customer;
import ml.wonwoo.sample.domain.JavasCustomer;
import ml.wonwoo.sample.dto.CustomerDto;
import ml.wonwoo.sample.dto.JavasCustomerDto;

public interface CustomerMapper {

CustomerDto customerDto(Customer customer);

JavasCustomerDto javasCustomerDto(JavasCustomer customer);
}
52 changes: 52 additions & 0 deletions mpple-sample/src/test/java/ml/wonwoo/sample/JavasLangTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ml.wonwoo.sample;

import static org.assertj.core.api.Assertions.assertThat;

import io.vavr.collection.List;
import java.util.Date;
import java.util.Iterator;
import ml.wonwoo.Mpple;
import ml.wonwoo.sample.domain.JavasCustomer;
import ml.wonwoo.sample.domain.OrderItem;
import ml.wonwoo.sample.dto.JavasCustomerDto;
import ml.wonwoo.sample.dto.OrderItemDto;
import ml.wonwoo.sample.mapper.CustomerMapper;
import org.junit.Test;

public class JavasLangTests {

@Test
public void default_sample() {
CustomerMapper mapper = Mpple.builder()
.target(CustomerMapper.class);
JavasCustomer customer = createCustomer();
JavasCustomerDto javasCustomerDto = mapper.javasCustomerDto(customer);
assertThatCustomerDto(javasCustomerDto);
}

private JavasCustomer createCustomer() {

OrderItem orderItem = new OrderItem();
orderItem.setName("iphone");
orderItem.setPrice(9999999);

OrderItem orderItem1 = new OrderItem();
orderItem1.setName("ipad");
orderItem1.setPrice(100000000);

JavasCustomer javasCustomer = new JavasCustomer();
javasCustomer.setDate(new Date());
javasCustomer.setItems(List.of(orderItem, orderItem1));
return javasCustomer;
}

private void assertThatCustomerDto(JavasCustomerDto customerDto) {
Iterator<OrderItemDto> iterator = customerDto.getItems().iterator();
OrderItemDto orderItemDto = iterator.next();
assertThat(orderItemDto.getName()).isEqualTo("iphone");
assertThat(orderItemDto.getPrice()).isEqualTo(9999999);
OrderItemDto orderItemDto1 = iterator.next();
assertThat(orderItemDto1.getName()).isEqualTo("ipad");
assertThat(orderItemDto1.getPrice()).isEqualTo(100000000);
}
}

0 comments on commit a80b18b

Please sign in to comment.