I am trying to make a system that will allow me to take all the items in one array, and, with a factory object, convert them into another type of item, and fill a second array with those new objects, and return that. I want to do this all with generics so this can be done in a single-line static method call. However, I'm getting an error that I can't quite parse. Here is (I hope) all the relevant code:
Problem class has:
// ...
// "ArrayPP" is my take on ArrayList that has a lot of convenience methods.
// The constructor here uses varargs to initialize the array:
ArrayPP<String> myStrings = new ArrayPP<>("foo", "bar", "baz");
// MyCharSequenceImpl is just a class that implements CharSequence.
ArrayPP<MyCharSequenceImpl> myCSI = new ArrayPP<>();
// The error occurs on the following line:
myCSI = ArrayPP.transfer(myStrings, new CSToMyCSIFactoryDelegate());
// ...
ArrayPP
has:
public class ArrayPP<T> implements Iterable<T> {
// ...
// Iterable implementations
// ...
public static <From, To> ArrayPP<To> transfer(Iterable<From> origin, FactoryDelegate<From, To> factory)
{
ArrayPP<To> ret = new ArrayPP<>();
for (From f : origin) {
ret.add(factory.makeFromFactory(f));
}
return ret;
}
// ...
}
FactoryDelegate
has:
public interface FactoryDelegate<From, To> extends Factory<To> {
public To makeFromFactory(From basis);
}
Factory
has:
public interface Factory<To> {
public To makeFromFactory();
}
CSToMyCSIFactoryDelegate
has:
public class CSToMyCSIFactoryDelegate implements FactoryDelegate<CharSequence, MyCharSequenceImpl> {
@Override
public MyCharSequenceImpl makeFromFactory(CharSequence basis) {
// MyCharSequenceImpl has a constructor that converts any CharSequence
// into a new MyCharSequenceImpl.
return new MyCharSequenceImpl(basis);
}
@Override
public MyCharSequenceImpl makeFromFactory() {
return makeFromFactory("");
}
}
Here is the error I'm getting:
error: method transfer in class ArrayPP<T> cannot be applied to given types;
myCSI = ArrayPP.transfer(myStrings, new CSToMyCSIFactoryDelegate());
^
required: Iterable<From>,FactoryDelegate<From,To>
found: ArrayPP<String>,TransferTest.CSToMyCSIFactoryDelegate
reason: inferred type does not conform to equality constraint(s)
inferred: CharSequence
equality constraints(s): CharSequence,String
where From,To,T are type-variables:
From extends Object declared in method <From,To>transfer(Iterable<From>,FactoryDelegate<From,To>)
To extends Object declared in method <From,To>transfer(Iterable<From>,FactoryDelegate<From,To>)
T extends Object declared in class ArrayPP
The most confusing part of this error, to me, is that it says inferred type does not conform to equality constraint(s)
, and then that the inferred type is CharSequence
and the equality constraints are CharSequence,String
...
Why am I getting this error, and how can I fix my code?