Created
March 28, 2019 05:33
-
-
Save hakanai/5e5cf729dc0c1752dfaba0187a81df5a to your computer and use it in GitHub Desktop.
Example usage of Guava's `TypeToInstanceMap` for maps of non-trivial types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Converter<T> | |
{ | |
String toString(T thing); | |
T fromString(String string); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.reflect.MutableTypeToInstanceMap; | |
import com.google.common.reflect.TypeParameter; | |
import com.google.common.reflect.TypeToInstanceMap; | |
import com.google.common.reflect.TypeToken; | |
// Suppress nags about TypeToken - we don't intend to serialise this class anyway. | |
@SuppressWarnings("serial") | |
public class ConverterRegistry | |
{ | |
private final TypeToInstanceMap<Converter<?>> converters = new MutableTypeToInstanceMap<>(); | |
public ConverterRegistry() | |
{ | |
converters.putInstance(new TypeToken<Converter<String>>(){}, new StringConverter()); | |
converters.putInstance(new TypeToken<Converter<Integer>>(){}, new IntegerConverter()); | |
} | |
public <T> Converter<T> getConverter(Class<T> clazz) | |
{ | |
return converters.getInstance(new TypeToken<Converter<T>>(){}.where(new TypeParameter<T>(){}, clazz)); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IntegerConverter implements Converter<Integer> | |
{ | |
@Override | |
public String toString(Integer thing) | |
{ | |
return Integer.toString(thing); | |
} | |
@Override | |
public Integer fromString(String string) | |
{ | |
return Integer.parseInt(string); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StringConverter implements Converter<String> | |
{ | |
@Override | |
public String toString(String thing) | |
{ | |
return thing; | |
} | |
@Override | |
public String fromString(String string) | |
{ | |
return string; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UsageExample | |
{ | |
public static void main(String[] args) | |
{ | |
ConverterRegistry registry = new ConverterRegistry(); | |
Converter<String> stringConverter = registry.getConverter(String.class); | |
System.out.println(stringConverter); | |
Converter<Integer> integerConverter = registry.getConverter(Integer.class); | |
System.out.println(integerConverter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment