Java - generics & wildcards & interface versus implementation - java

I have a question about Java generics. Say I have the following interface:
public static class Something<T> {
public void set(T t) {
}
}
public static interface Manager<T> {
public void add(final String key, final Something<T> o);
public Something<T> get(final String key);
}
An example of usage:
final Manager<Number> m = ...;
m.add("key", new Something<Number>());
m.get("key").set(new Integer(5));
I would also like to be able to add Something<Integer>, Something<Double>, ... to the a Manager<Number>. I would say I have to change the signature of the add-function:
public static interface Manager<T> {
public void add(final String key, final Something<? extends T> o);
public Something<T> get(final String key);
}
final Manager<Number> m = ...;
m.add("key", new Something<Integer>());
m.get("key").set(new Integer(5));
So far, so good. Let's look at a possible implementation of the manager:
public static class ManagerImplementation<T> implements Manager<T> {
protected HashMap<String, Something<T>> map = new HashMap<String, Something<T>>();
public void add(final String key, final Something<? extends T> o) {
map.put(key, o); // <--- here
}
public Something<T> get(final String key) {
return map.get(key);
}
}
This fails, since you cannot add a Something<? extends T> to a Map<X, Something<T>>. So let's change this:
public static class ManagerImplementation<T> implements Manager<T> {
protected HashMap<String, Something<? extends T>> map = new HashMap<String, Something<? extends T>>();
public void add(final String key, final Something<? extends T> o) {
map.put(key, o);
}
public Something<T> get(final String key) {
return map.get(key); // <--- here
}
}
This fails since map.get(key) returns Something<? extends T> while the get-function is required to return Something<T>, as defined in the interface.
What is the common approach to solve this?
Thanks!

Inside your class you are always using Something<? extends T>, thus in your public get method you must convert the inside world to the outside world format. E.g. you can simply cast the result of map.get(key) to Something<T>:
return (Something<T>) map.get(key); // <--- here

Related

How to initializing Class<T> at interface default method?

I have an interface:
public interface ITransformer<S,T>{
public void transform(S source,T target);
default String getTransformerName(){
Class<S> s;
Class<T> t;
return s.getName() + t.getName(); //*********
}
}
the error message the starred line:
The local variable s may not have been initialized
The local variable t may not have been initialized
I would like to use this method to return a string with [S.classname][T.classname] . Please let me know how to achieve this or is this impossible to do at interface ?
Update: Jan 12
My purpose of doing this is due to the fact that this class will be in framework and I want to reduce the human error as much as possible.. I am changing the code as follows:
public interface ITransformer<S,T>{
public void transform(S source,T target);
public FieldEntry<S, T> getTransformerName();
}
public class FieldEntry<S,T> implements Comparable<FieldEntry> {
private Class<S> s;
private Class<T> t;
public FieldEntry(Class<S> s,Class<T> t){
this.s = s;
this.t = t;
}
public String getEntryName(){
return s.getName() + t.getName();
}
#Override
public int compareTo(FieldEntry entry) {
if(entry == null) throw new IllegalArgumentException("The argument to compare cannot be null!");
return entry.getEntryName().compareTo(this.getEntryName());
}
}
In order to demonstrate why this can’t work, you may change your class to
public interface ITransformer<S,T>{
public void transform(S source,T target);
static <In,Out> ITransformer<In,Out> noOp() {
return (source,target) -> {};
}
static void main(String... arg) {
ITransformer<String,Integer> t1 = noOp();
ITransformer<Long,Thread> t2 = noOp();
System.out.println(t1 == (Object)t2);
}
}
Running this will print true. In other words, both functions are represented by the same instances, so there can’t be and property allowing to recognize their different type.
Generally, when two functions (lambda expressions or method references) exhibit the same behavior, a JVM may represent them by the same implementation type or even the same instance.
Even for non-interface classes, this doesn’t work due to Type Erasure. It only works when you have a reifiable (i.e. non-generic) type extending or implementing a generic type.
It's a little bit dangerous and I wouldn't used this in production (because you should cover in your code all possible use cases of your interface), but you can use reflection for it:
public interface ITransformer<S, T> {
public void transform(S source, T target);
default String getTransformerName() {
Type[] genericInterfaces = this.getClass().getGenericInterfaces();
ParameterizedType parameterizedType = null;
for (Type genericInterface : genericInterfaces) {
if (genericInterface instanceof ParameterizedType) {
ParameterizedType paramInterface = (ParameterizedType) genericInterface;
if (paramInterface.getRawType().equals(ITransformer.class)) {
parameterizedType = paramInterface;
break;
}
}
}
if (parameterizedType == null) {
throw new IllegalStateException("!");
}
return parameterizedType.getActualTypeArguments()[0].getTypeName() + parameterizedType.getActualTypeArguments()[1].getTypeName();
}
}
public class StringToIntegerTransfomer implements ITransformer<String, Integer> {
#Override
public void transform(String source, Integer target) {
}
}
public interface StringToNumberTransfomer<T extends Number> extends ITransformer<String, T> {
}
public class StringToLongTransfomer implements StringToNumberTransfomer<Long>, ITransformer<String, Long> {
#Override
public void transform(String source, Long target) {
}
}
#Test
public void test() {
ITransformer<String, Integer> intTransformer = new StringToIntegerTransfomer();
ITransformer<String, Long> longTransformer = new StringToLongTransfomer();
ITransformer<String, String> stringTransformer = new ITransformer<String, String>() {
#Override
public void transform(String source, String target) {
}
};
ITransformer<String, Double> doubleTransformer = new StringToNumberTransfomer<Double>() {
#Override
public void transform(String source, Double target) {
}
};
System.out.println(String.format("intTransformer: %s", intTransformer.getTransformerName()));
System.out.println(String.format("longTransformer: %s", longTransformer.getTransformerName()));
System.out.println(String.format("stringTransformer: %s", stringTransformer.getTransformerName()));
System.out.println(String.format("doubleTransformer: %s", doubleTransformer.getTransformerName()));
}
Output for this snippet:
intTransformer: java.lang.Stringjava.lang.Integer
longTransformer: java.lang.Stringjava.lang.Long
stringTransformer: java.lang.Stringjava.lang.String
java.lang.IllegalStateException: !
This code has one restriction, you should say implements ITransformer<S, T> for all implementations of ITransformer. That why I have got IllegalStateException for this line ITransformer<String, Double> doubleTransformer = new StringToNumberTransfomer<Double>(). But you can improve this code.
Better option is to use some base implementation of interface and pass source and target classes into constructor:
public interface ITransformer<S, T> {
void transform(S source, T target);
String getTransformerName();
}
public abstract class BaseITransformer<S, T> implements ITransformer<S, T> {
private final Class<S> sourceClass;
private final Class<T> targetClass;
public BaseITransformer(Class<S> sourceClass, Class<T> targetClass) {
this.sourceClass = sourceClass;
this.targetClass = targetClass;
}
public String getTransformerName() {
return sourceClass.getName() + targetClass.getName();
}
}
In Java it is impossible to get a Class<S>, unless you already know which class S is, or something else that knows which class S is gives you one.

Using generics for a class how can I convert the values from one type to another?

I built a simple document store, there are entities that have fields of different types, I have a Float, Int and String type. The entity contains an array list of values, if someone updates the schema of the entity I would like to be able to try to convert the values to the new type.
public interface FieldType<T> {
ArrayList<T> values;
}
public class FloatField implements FieldType<Float> {
}
public class StringField implements FieldType<String> {
}
I have thought about using a abstract class with methods as below
public abstract class Field<T> implements FieldType<T> {
abstract public <T> castFromString(String value);
abstract public <T> castFromFloat(Float value);
abstract public <T> castFromInt(Int value);
}
public class FloatField extends Field<Float> {
#override
public <Float> castFromString(String value){
Float castValue = null;
try {
castValue = Float.parseFloat(value);
} catch(Exception e){
}
return castValue;
}
}
I did not really like this solution as I would have to add a new abstract method each time I added an extra type to the system.
Any ideas how I could implement this better?
Maybe you could use the Function<T, R> interface?
public abstract class Field<T> implements FieldType<T> {
...
public <F> T convert(F value, Function<F, T> converter) {
try {
return converter.apply(value);
} catch(Exception e) {
return null;
}
}
...
}
And then specify the converter using a lambda expression or a method reference:
field.convert("1234", BigDecimal::new); //with a method reference
field.convert("1234", s -> new BigDecimal(s)) //with a lambda
This would replace all of your convertXXX methods by one since the return type is inferred from the passed Function.
EDIT:
If you want automatic converting, you would of course have to hard-code these since you wouldn't want to write conversion methods for all 4240 classes in the Java API. This gets messy, though. Maybe something like this in a static helper class or in FieldType itself?
public class WhereverYouWantThis {
private static HashMap<Class<?>, HashMap<Class<?>, Function<?, ?>>> converters = new HashMap<>();
static {
putConverter(String.class, Float.class, Float::parseFloat);
}
private static <T, R> void putConverter(Class<T> t, Class<R> r, Function<T, R> func) {
HashMap<Class<?>, Function<?, ?>> map = converters.get(t);
if(map == null) converters.put(t, map = new HashMap<>());
map.put(r, func);
}
public static <T, R> Function<T, R> getConverter(Class<T> t, Class<R> r) {
HashMap<Class<?>, Function<?, ?>> map = converters.get(t);
if(map == null) return null;
#SuppressWarnings("unchecked")
Function<T, R> func = (Function<T, R>) map.get(r);
return func;
}
public static <T, R> R convert(T o, Class<R> to) {
#SuppressWarnings("unchecked")
Function<T, R> func = (Function<T, R>) getConverter(o.getClass(), to);
return func == null ? null : func.apply(o);
}
}
I don't think you need generics for this. Instead, just try to create a Float from the input String and return null if there be a problem:
public Float castFromString(String value) {
Float castValue = null;
try {
castValue = Float.parseFloat(value);
} catch(Exception e){
// log here
}
return castValue;
}
The reason I don't think generics are needed is that the types involved in the conversion are named/known in your helper methods.

Getting key from object for Map using object and interface

I have a Table class that implements a ForwardingMultimap of a certain type of object. I was wondering if it was over-doing it to create an interface that extracted the key from the object so it wouldn't be annoying for the caller to handle "Entry" objects when they call "values". Or would it just be better to have the caller put the object and key in themselves? If this is okay, would creating a separate class to handle each key like below be better, or should the caller implement it themselves ?
public class CustomObject {
public String propertyOne;
public int propertyTwo;
}
public interface ITableAggKey {
Object getKey(CustomObject customObj);
}
public class Table extends ForwardingMultimap<Object, CustomObject> {
Multimap m_map;
public Table(ITableAggKey aggKey){
m_map = HashMultimap.create();
m_aggKey = aggKey;
}
public boolean put(CustomObject obj) {
m_map.put(m_aggKey.getKey(obj), obj);
}
}
public class CustomObjectAggKeys {
public static final aggKeyOne = new ITableAggKey(){
#Overide
public Object getKey(CustomObject obj){
return obj.propertyOne;
}
};
public static final aggKeyOne = new ITableAggKey(){
#Overide
public Object getKey(CustomObject obj){
return obj.propertyTwo;
}
};
}
public class Table<K, T> extends ForwardingMultimap<K, T> {
Multimap<K, T> m_map;
Function<T, K> m_aggKey;
public Table(Function<T, K> aggKey){
m_map = HashMultimap.create();
m_aggKey = aggKey;
}
public boolean put(T obj) {
m_map.put(m_aggKey.apply(obj), obj);
}
}
public static void main(String[] args) {
Table<String, CustomObject> IndexOne = new Table<>(x -> x.propertyOne);
Table<Integer, CustomObject> IndexTwo = new Table<>(x -> x.propertyTwo);
}
If you cannot use Java8. Add Function interface.
public interface Function<T, K> {
K apply(T arg);
}
And
Table<String, CustomObject> indexOne = new Table<>(new Function<CustomObject, String>() {
#Override public String apply(CustomObject obj) {
return obj.propertyOne;
}
});

Java generics and interfaces, using types as method parameters

I have the following code:
public interface IDoWork<K extends AbstractKey,V extends AbstractClass> {
V obtain(K key, V value);
}
public class AbstractKey {
String id;
}
public class AbstractClass {
String name;
}
public class ConcreteA extends AbstractClass {
String attributeA;
}
public class DoWorkA implements IDoWork<KeyA, ConcreteA> {
private static final DoWorkA INSTANCE = new DoWorkA();
public static DoWorkA getInstance() {
return INSTANCE;
}
#Override
public ConcreteA obtain(KeyA k, ConcreteA v) {
//do something in real life
return null;
}
}
public class Main {
public static void main(String[] args){
KeyA a = new KeyA();
ConcreteA c = new ConcreteA();
IDoWork<? extends AbstractKey, ? extends AbstractClass> instance =
WorkFactory.getInstance().obtainInstance(a);
instance.obtain(a, c);
}
}
public class WorkFactory {
private static final WorkFactory INSTANCE = new WorkFactory();
public static WorkFactory getInstance() {
return INSTANCE;
}
public IDoWork<? extends AbstractKey, ? extends AbstractClass> obtainInstance(AbstractKey key){
if(key instanceof KeyA){
return DoWorkA.getInstance();
}
throw new IllegalArgumentException("Case not handled");
}
}
In the following code:
KeyA a = new KeyA();
ConcreteA c = new ConcreteA();
IDoWork<? extends AbstractKey, ? extends AbstractClass> instance = WorkFactory.getInstance().obtainInstance(a);
instance.obtain(a, c);
This line doesn't compile:
instance.obtain(a, c);
due to the nature of Java generics. I get the following error:
The method obtain(capture#3-of ? extends AbstractKey, capture#4-of ? extends AbstractClass) in the type IDoWork is not applicable for the arguments (KeyA, ConcreteA)
Is there another way to do it? If omit the type parameters in
IDoWork<? extends AbstractKey, ? extends AbstractClass> instance =...
If I remove the type parameters from IDoWork... it works but I get a warning. Is there a way to fix that.? I know that I can remove the type parameters from the interface and that will solve the problem but I find it handy to have the generics in order not to have to do casting + it brings clarity to the code since it's clear to see what each class uses. Depencendy injection is out of question because it's not available in the codebase I'm in.
I have something that works, but is not very optimal:
If you define your WorkFactory this way:
public class WorkFactory {
private static final WorkFactory INSTANCE = new WorkFactory();
public static WorkFactory getInstance() {
return INSTANCE;
}
#SuppressWarnings("unchecked")
public <K extends AbstractKey, V extends AbstractClass> IDoWork<K, V> obtainInstance(K key, V val) {
if (key instanceof KeyA) {
return (IDoWork<K, V>) DoWorkA.getInstance();
}
throw new IllegalArgumentException("Case not handled");
}
}
then your main method should work cleanly:
public static void main(String[] args){
KeyA a = new KeyA();
ConcreteA c = new ConcreteA();
IDoWork<KeyA, ConcreteA> instance = WorkFactory.getInstance().obtainInstance(a, c);
instance.obtain(a, c);
}
I have changed obtainInstance to take the key and the val, to infer the types properly.

Java Generics, how to avoid unchecked assignment warning when using class hierarchy?

I want to use a method using generic parameters and returning generic result on a class hierarchy.
edit: no SupressWarnings("unchecked") answer allowed :-)
Here is a sample code illustrating my problem:
import java.util.*;
public class GenericQuestion {
interface Function<F, R> {R apply(F data);}
static class Fruit {int id; String name; Fruit(int id, String name) {
this.id = id; this.name = name;}
}
static class Apple extends Fruit {
Apple(int id, String type) { super(id, type); }
}
static class Pear extends Fruit {
Pear(int id, String type) { super(id, type); }
}
public static void main(String[] args) {
List<Apple> apples = Arrays.asList(
new Apple(1,"Green"), new Apple(2,"Red")
);
List<Pear> pears = Arrays.asList(
new Pear(1,"Green"), new Pear(2,"Red")
);
Function fruitID = new Function<Fruit, Integer>() {
public Integer apply(Fruit data) {return data.id;}
};
Map<Integer, Apple> appleMap = mapValues(apples, fruitID);
Map<Integer, Pear> pearMap = mapValues(pears, fruitID);
}
public static <K,V> Map<K,V> mapValues(
List<V> values, Function<V,K> function) {
Map<K,V> map = new HashMap<K,V>();
for (V v : values) {
map.put(function.apply(v), v);
}
return map;
}
}
How to remove the generic exception from these calls:
Map<Integer, Apple> appleMap = mapValues(apples, fruitID);
Map<Integer, Pear> pearMap = mapValues(pears, fruitID);
Bonus question: how to remove the compilation error if I declare the fruitId Function this way:
Function<Fruit, Integer> fruitID = new Function<Fruit, Integer>() {public Integer apply(Fruit data) {return data.id;}};
I'm very confused about generics when it is dealing with hierarchy. Any pointer to a good resource about the usage of and will be greatly appreciated.
2 small changes:
public static void main(final String[] args){
// ... snip
// change nr 1: use a generic declaration
final Function<Fruit, Integer> fruitID =
new Function<Fruit, Integer>(){
#Override
public Integer apply(final Fruit data){
return data.id;
}
};
// ... snip
}
public static <K, V> Map<K, V> mapValues(final List<V> values,
// change nr. 2: use <? super V> instead of <V>
final Function<? super V, K> function){
// ... snip
}
For reference, read this:
The get-put principle

Categories