Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
abuttaro committed Feb 11, 2018
1 parent 5e20726 commit cb053ac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,25 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import com.google.common.collect.ImmutableTable;

public class ConverterFactory {

private final ImmutableTable<Type,Type,Function<?,?>> converters;
private final Function<?,?> noOpConverter;

public ConverterFactory(ImmutableTable.Builder<Type,Type,Function<?,?>> table) {
this(table.build());
}

public ConverterFactory(ImmutableTable<Type,Type,Function<?,?>> table) {
this.converters = table;
this.noOpConverter = a -> a;
}
public interface ConverterFactory {

@SuppressWarnings({ "unchecked", "rawtypes" })
public <F, T> Function<F, T> getConverter(Type from, Type to) {
Function<?,?> converter = converters.get(from, to);
if ( converter == null ) {
if ( to instanceof Class && from instanceof Class
&& ((Class)to).isAssignableFrom((Class) from) ) {
return (Function) noOpConverter;
}
throw new UnsupportedOperationException("Converter does not exist from "+from+" to "+to);
}
return (Function) converter;
}
<F, T> Function<F, T> getConverter(Type from, Type to);

@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> Map<Type, Function<T,?>> getConverters(Class<T> from) {
return (Map) converters.row(from);
}
<T> Map<Type, Function<T, ?>> getConverters(Class<T> from);

@SuppressWarnings("unchecked")
public <F, T> T convert(F from, Type to) {
return (T) getConverter((Class<F>) from.getClass(), to).apply(from);
}
<F, T> T convert(F from, Type to);

public static final Builder builder() {
static Builder builder() {
return new Builder();
}

public static final Builder withDefaults() {
static Builder withDefaults() {
return builder().addDefaults();
}

public final static class Builder {
static class Builder {
private ImmutableTable.Builder<Type,Type,Function<?,?>> table = ImmutableTable.builder();

private Builder() {
Expand Down Expand Up @@ -123,7 +93,7 @@ public <T, R> void add(Class<T> from, Function<T, R> converter, Class<R> to, Typ
}

public ConverterFactory build() {
return new ConverterFactory(table);
return new DefaultConverterFactory(table);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.restup.bind.converter;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Function;
import com.google.common.collect.ImmutableTable;

class DefaultConverterFactory implements ConverterFactory {

private final ImmutableTable<Type,Type,Function<?,?>> converters;
private final Function<?,?> noOpConverter;

DefaultConverterFactory(ImmutableTable.Builder<Type, Type, Function<?, ?>> table) {
this(table.build());
}

DefaultConverterFactory(ImmutableTable<Type, Type, Function<?, ?>> table) {
this.converters = table;
this.noOpConverter = a -> a;
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <F, T> Function<F, T> getConverter(Type from, Type to) {
Function<?,?> converter = converters.get(from, to);
if ( converter == null ) {
if ( to instanceof Class && from instanceof Class
&& ((Class)to).isAssignableFrom((Class) from) ) {
return (Function) noOpConverter;
}
throw new UnsupportedOperationException("Converter does not exist from "+from+" to "+to);
}
return (Function) converter;
}

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> Map<Type, Function<T,?>> getConverters(Class<T> from) {
return (Map) converters.row(from);
}

@Override
@SuppressWarnings("unchecked")
public <F, T> T convert(F from, Type to) {
return (T) getConverter((Class<F>) from.getClass(), to).apply(from);
}

}

0 comments on commit cb053ac

Please sign in to comment.