Generics Bounded Type Parameter - java

I have this design, and I'm not sure why it doesn't work.
interface BaseType {}
interface TypeA extends BaseType {}
interface TypeB extends BaseType {}
interface Query<T extends BaseType> {
public String get();
}
interface Result<T extends BaseType> {
public String get();
}
interface Service<T extends BaseType> {
public Result<T> get(Query<T> query);
}
class SomeResult implements Result<TypeA> {
private String s;
public SomeResult(String s) { this.s = s; }
public String get() { return this.s; }
}
class SomeQuery implements Query<TypeA> {
public String get() { return "blah"; }
}
class SomeQuery2 implements Query<TypeA> {
public String get() { return "blah2"; }
}
class SomeService implements Service<TypeA> {
/** OK -- but notice the ambiguous parameter type */
/*
public SomeResult get(Query<TypeA> query) {
if (query instanceof SomeQuery) return new SomeResult(query.get());
else return null;
}
*/
/** NOT OK -- but this is the parameter I want to keep; notice SomeQuery IS-A Query<TypeA> */
public SomeResult get(SomeQuery query) { return new SomeResult(query.get()); };
/**
* Main.java:27: error: SomeService is not abstract and does not override abstract method get(Query<TypeA>) in Service
* class SomeService implements Service<TypeA> {
* ^
* 1 error
*/
}
public class Main {
public static void main(String args[]) {
SomeQuery someQuery = new SomeQuery();
SomeQuery2 someQuery2 = new SomeQuery2();
SomeService someService = new SomeService();
System.out.println(someService.get(someQuery).get());
}
}
I'm new to generics, and don't quite understand what contract I'm violating here. I want the service to be tightly bounded, and even if I can bound the return type, I cannot seem to do so for the parameter. Which means, I'll need to do an instanceof check inside the service to make sure I'm getting the right parameter. I want to avoid that. Any ideas?

You're allowed to make an overriding method's return type more specific due to return type covariance, but you can't change the method's parameters without changing its signature. That's why the compiler complains that you haven't implemented get(Query<TypeA>) when you change it to get(SomeQuery). You'll need to make Service more flexible in order to get what you want:
interface Service<T extends BaseType, Q extends Query<T>> {
public Result<T> get(Q query);
}
class SomeService implements Service<TypeA, SomeQuery> {
#Override
public SomeResult get(SomeQuery query) {
...
}
}
Also note that the narrowed return type doesn't matter when coding to interface: when SomeService is typed as Service<TypeA, SomeQuery>, get will still return Result<TypeA>. So you might consider making a similar change for the result type:
interface Service<T extends BaseType, Q extends Query<T>, R extends Result<T>> {
public R get(Q query);
}
class SomeService implements Service<TypeA, SomeQuery, SomeResult> {
#Override
public SomeResult get(SomeQuery query) {
...
}
}

Related

Sub class generics of class type in Typescript

I am trying to achieve something in Typescript. I will post the Java equivalent in hope that someone can help me with the Typescript version.
interface BaseChannel {
void send();
}
public class SampleChannel implements BaseChannel {
#Override
public void send() {
}
}
public class SampleDispatch {
Class<? extends BaseChannel>[] getChannels() {
Class<? extends BaseChannel>[] array = new Class[2];
array[0] = SampleChannel.class;
array[1] = BaseChannel.class;
return array;
}
}
Thanks.
Something like that
interface BaseChannel {
send();
}
class SampleChannel implements BaseChannel {
constructor() {}
send() {
// do stuff
}
}
class SampleDispatch {
getChannels<T extends BaseChannel>(obj: T): BaseChannel {
return new SampleChannel();
}
}
I added a generic type parameter but your example do not need any generic

Unable to MockUp a generic interface in JMockit

I want to mock a generic interface:
public interface IModel<T, S> {
public S classify(T entity);
}
This interface is sub-classed by 3 concrete classes: TextModel, ImageModel, ScoringModel. Each of these concrete classes have different T and S parameters.
I wrote a generic method that receives the concrete model class as an argument and generates a mocked version of the model:
private <T extends IModel<?, ?>> T mockModel(Class<T> modelClass) {
return new MockUp<T>() {
#Mock public Object classify(Object entity) { return null; }
}.getMockInstance();
}
I know that IModel::classify has generic types for both its input and output, but I haven't found a way to use the actual generic method within the mockup.
When calling this method I get an IllegalArgumentException:
java.lang.IllegalArgumentException: Value of type com.classificationmanager.model.$Impl_IModel incompatible with return type com.classificationmanager.model.TextModel of com.classificationmanager.model.TextModelFactory#createModel(com.classificationmanager.model.ModelDescriptor)
at com.classificationmanager.model.ModelFetcherTest$5.(ModelFetcherTest.java:110)
at com.classificationmanager.model.ModelFetcherTest.mockAllFactories(ModelFetcherTest.java:109) ....... (spared you the rest)
I thought that getting and returning an Object instead of T and S was the problem, but I get the same exception when removing the mocked method and just mocking the class:
private <T extends IModel<?, ?>> T mockModel(Class<T> modelClass) {
return new MockUp<T>() {
}.getMockInstance();
}
I could do a switch-case and return a concrete class but that would just be nasty.
Any workaround involving the Expectations API would also work for me.
10x
Maybe the following examples can help (although I still don't understand the question - probable case of the XY problem).
public final class ExampleTest {
public interface IModel<T, S> { S classify(T entity); }
static class TextModel implements IModel<Integer, String> {
#Override public String classify(Integer entity) { return "test"; }
}
static class ImageModel implements IModel<String, Image> {
#Override public Image classify(String entity) { return null; }
}
#Test
public void createNonMockedInstanceForAnyModelClass() {
IModel<Integer, String> m1 = mockModel(TextModel.class);
String s = m1.classify(123);
IModel<String, Image> m2 = mockModel(ImageModel.class);
Image img = m2.classify("test");
assertEquals("test", s);
assertNull(img);
}
<T extends IModel<?, ?>> T mockModel(Class<T> modelClass) {
// Or use newUninitializedInstance in case the model class doesn't
// have a no-args constructor.
return Deencapsulation.newInstance(modelClass);
}
#Test
public void mockAllModelImplementationClassesAndInstances(
#Capturing IModel<?, ?> anyModel
) {
IModel<Integer, String> m = new TextModel();
String s = m.classify(123);
assertNull(s);
}
}

Java Generics and Enum, loss of template parameters

I have a fairly complicated structure, and it is not working as intended. This is what I did:
public interface ResultServiceHolder {
<M, ID extends Serializable, BO extends BusinessObject<M, ID>> ResultService<M, ID, BO> getService();
}
public enum ResultTypes implements ResultServiceHolder {
RESULT_TYPE_ONE {
#Override
public ResultOneService getService() { //unchecked conversion?
return serviceInitializer.getResultOneService();
}
},
RESULT_TYPE_TWO {
#Override
public ResultTwoService getService() { //unchecked conversion?
return serviceInitializer.getResultTwoService();
}
},
RESULT_TYPE_THREE {
#Override
public ResultThreeService getService() { //unchecked conversion?
return serviceInitializer.getResultThreeService();
}
};
protected ServiceInitializer serviceInitializer;
protected void setServiceInitializer(ServiceInitializer serviceInitializer) {
this.serviceInitializer = serviceInitializer;
}
#Component
public static class ServiceInitializer {
#Autowired
private ResultOneService resultOneService;
#Autowired
private ResultTwoService resultTwoService;
#Autowired
private ResultThreeService resultThreeService;
#PostConstruct
public void init() {
for(ResultTypes resultType : ResultTypes.values()) {
resultType.setServiceInitializer(this);
}
}
//getters
}
}
The purpose was to generalize the call based on enums, and rather, just be able to iterate on the array of enums.
for(ResultServiceHolder resultServiceHolder : ResultTypes.values()) {
if(resultServiceHolder.equals(post.getPostResultTypeCode())) {
return resultServiceHolder.getService().createResultSearchCriteriaResponse(postId);
}
}
And this is working fine and dandy. However, if I'd say
ResultTypes.RESULT_TYPE_ONE.getService().getRepository()
Then it is a BaseRepository<Object, Serializable> rather than a BaseRepository<ResultTypeOne, Long>. The method resultTypeHolder.getService() gives back ResultService<M, ID, BO>, but in the end, it becomes Object andSerializable.
What am I doing wrong? How can I retain the generic parameter types?
I'd like to add that yes, I do realize the problem is somewhere with the unchecked casting. But the services are defined as
public interface ResultTypeOneService
extends ResultService<ResultTypeOne, Long, ResultTypeOneBO> {
}
And I don't know why the types are not inferred.
EDIT: Technically, it works if I explicitly infer them:
ResultTypes.RESULT_TYPE_ONE.<ResultTypeOne, Long, ResultTypeOneBO>getService().getRepository()
But it ought to be automatic, why is it not working automatically? Am I supposed to provide it with some kind of object that contains the type? Why is the return type not enough for that?
EDIT2: The superclass of the ResultTypeOne is
#SuppressWarnings("serial")
#EntityListeners(EntityListener.class)
#MappedSuperclass
public abstract class EntityBase implements Serializable {
But it is not mapped anywhere in the bounds.
EDIT3: A big thank you to #Radiodef! The theoretic solution ended up to be the following, and would work perfectly fine:
public interface ResultServiceHolder<M, ID extends Serializable, BO extends BusinessObject<M, ID>> {
ResultService<M, ID, BO> getService();
}
public abstract class ResultTypes<M, ID extends Serializable, BO extends BusinessObject<M, ID>>
implements ResultServiceHolder<M, ID, BO> {
public static ResultTypes<?, ?, ?>[] values() {
return new ResultTypes<?, ?, ?>[] {RESULT_ONE, RESULT_TWO, RESULT_THREE};
}
public static final ResultTypes<ResultOne, Long, ResultOneBO> RESULT_ONE = new ResultTypes<ResultOne, Long, ResultOneBO>("Result One") {
#Override
public ResultOneService getService() {
return serviceInitializer.resultOneService;
}
};
public static final ResultTypes<ResultTwo, Long, ResultTwoBO> RESULT_TWO = new ResultTypes<ResultTwo, Long, ResultTwoBO>("Result Two") {
#Override
public ResultTwoService getService() {
return serviceInitializer.resultTwoService;
}
};
public static final ResultTypes<ResultThree, Long, ResultThreeBO> RESULT_THREE = new ResultTypes<ResultThree, Long, ResultThreeBO>("Result Three") {
#Override
public ResultThreeService getService() {
return serviceInitializer.resultThreeService;
}
};
protected String name;
protected ServiceInitializer serviceInitializer;
private ResultTypes(String name) {
this.name = name;
}
protected void setServiceInitializer(ServiceInitializer serviceInitializer) {
this.serviceInitializer = serviceInitializer;
}
#Component
static class ServiceInitializer {
#Autowired
private ResultOneService resultOneService;
#Autowired
private ResultTwoService resultTwoService;
#Autowired
private ResultThreeService resultThreeService;
#PostConstruct
public void init() {
for (ResultTypes resultType : ResultTypes.values()) {
resultType.setServiceInitializer(this);
}
}
}
}
I think because of how lengthy the solution becomes, I'll stick with the enum approach, and just accept this loss of bounds. I lose more by having to add my own values() implementation than I gain from enforcing these bounds. However, this is an interesting theoretical exercise, and thank you again for your help.
Okay, first you need to understand why what you're doing is probably not what you think it's doing. Let's look at a simpler example.
interface Face {
<T> List<T> get();
}
What you have there is a generic method, get. A generic method's type parameter depends on what is supplied by the call site. So for example like this:
Face f = ...;
// this call site dictates T to be Number
List<Number> l = f.<Number>get();
When you override it like
class Impl implements Face {
#Override
public List<String> get() { return ...; }
}
This is something you are able to do (only because of erasure) but you probably shouldn't. It's only allowed for backwards compatibility to non-generic code. You should listen to the warning and not do it. Doing it means that for example I can still come along and dictate it to return something else:
Face f = new Impl();
// now I've caused heap pollution because you
// actually returned to me a List<String>
List<Number> l = f.<Number>get();
This is why there is an unchecked conversion.
What you probably meant is to use a generic interface declaration:
interface Face<T> {
List<T> get();
}
Now the argument to T depends on the type of the object reference.
Face<Number> f = ...;
// get must return List<Number>
List<Number> l = f.get();
We can implement it like
class Impl implements Face<String> {
#Override
public List<String> get() { return ...; }
}
Additionally, you cannot access covariant return types on an enum. When you override methods on an enum constant, its class is anonymous. An anonymous class has no name and cannot be referred to. Therefore the programmer cannot know its covariant return type to use it. Furthermore, an enum cannot declare generic type parameters. So what you are wanting to do is simply impossible with enum.
You can use a class with public static final instances to simulate a generic enum:
public abstract class SimEnum<T> implements Face<T> {
public static final SimEnum<Number> A = new SimEnum<Number>() {
#Override
public List<Number> get() { return ...; }
};
public static final SimEnum<String> B = new SimEnum<String>() {
#Override
public List<String> get() { return ...; }
};
private SimEnum() {}
public static SumEnum<?>[] values() {
return new SimEnum<?>[] { A, B };
}
}
Otherwise you need to drastically change your idea.
Maybe use an interface/abstract class instead of an enum?
Enums cannot have type parameters but classes and interfaces can.
For example...
Interfaces
Entity.java
The "thing" interface...
import java.io.Serializable;
public interface Entity<K extends Serializable> {
// TODO: Put entity type things here!
// for example, things like "K getId();"
// You may want an abstract base class for this interface that all Entitys extend
}
Repository.java
Does CRUD stuff with things...
import java.io.Serializable;
public interface Repository<K extends Serializable, V extends Entity<K>> {
V getValue(K key);
// Other CRUD stuff
}
Service.java
A Service is responsible for doing stuff with things...
public interface Service<K, V> {
// Could have an abstract service class that has a repository and implements this for you...
V get(K key);
// Other "generic service" type stuff
}
Solid Classes
Entity1.java
Solid base class with String key...
public class Entity1 implements Entity<String> {
// TODO implement Entity stuff...
}
Entity2.java
Solid base class with Integer key...
public class Entity2 implements Entity<Integer> {
// TODO implement methods...
}
Entity1Service.java
Solid Entity1 Service
public class Entity1Service implements Service<String, Entity1> {
// Would not have to implement this if you extended an abstract base Service class
#Override
public Entity1 get(String key) {
return null;
}
}
Entity2Service.java
Solid Entity2 Service
public class Entity2Service implements Service<Integer, Entity2> {
// Wouldn't need this if you had abstract Service class either...
#Override
public Entity2 get(Integer key) {
return null;
}
}
ServiceHolder.java
Not an enum, but an interface - you could add methods to set the "service" from spring or something here...
import java.io.Serializable;
public abstract class ServiceHolder<K extends Serializable, V, S extends Service<K, V>> {
public static final ServiceHolder<String, Entity1, Entity1Service> ENTITY_1_SERVICE = new ServiceHolder<String, Entity1, Entity1Service>() {};
public static final ServiceHolder<Integer, Entity2, Entity2Service> ENTITY_2_SERVICE = new ServiceHolder<Integer, Entity2, Entity2Service>() {};
private S service;
private ServiceHolder() {
}
public S getService() {
return service;
}
public void setService(S service) {
this.service = service;
}
}
The interesting bit
I think this is the sort of thing you wanted, please let me know if I misunderstood...
public class PleaseCompile {
public static void main(String[] args) {
Entity1 solid1 = ServiceHolder.ENTITY_1_SERVICE.getService().get("[KEY]");
Entity2 solid2 = ServiceHolder.ENTITY_2_SERVICE.getService().get(42);
...
}
}
Hope this helps...
You cannot do what you want to do.
List<String> and List<Integer> face type erasure at runtime.
And so do your enum-mapped getService() functions.
Everything related to types for generics is validated at compile-time.

Create generic factory with generic subtype object in Java

I have written a converter structure in Java for which I'd like to create a factory.
My basic idea is that I pass in a class of a valid type and receive a valid subtype converter but fail with the generics and inheritance part.
This is my interface:
interface ItemRequestConverterFactory {
public <IR extends ItemRequest> ItemRequestConverter<IR> newInstance(Class<IR> clazz);
}
and the implementation:
public class DefaultItemRequestConverterFactory implements ItemRequestConverterFactory {
#Override
public <IR extends ItemRequest> ItemRequestConverter<IR> newInstance(Class<IR> clazz) {
if (clazz.equals(CreatePartRequestConverter.class))
return new CreatePartRequestConverter();
return null;
}
}
Unfortunately, Eclipse says: "Type mismatch: cannot convert from CreatePartRequestConverter to ItemRequestConverter".
Where is the problem in my generic signature?
You seem to have a mismatch between the type parameters and the types that they should actually represent: You are passing a Class<IR> to the newInstance method. This is a class that represents an ItemRequest. However, you are comparing this class instance to a class that is probably an implementation of the ItemRequestConverter interface.
Although this compiles when you add an appropriate type parameter to the CreatePartRequestConverter class, it's probably not what you want to achieve:
interface ItemRequestConverter<IR extends ItemRequest>{}
interface ItemRequest{}
interface ItemRequestConverterFactory
{
public <IR extends ItemRequest> ItemRequestConverter<IR> newInstance(Class<IR> itemRequestClass);
}
class CreatePartRequestConverter<IR extends ItemRequest> implements ItemRequestConverter<IR>
{
}
class DefaultItemRequestConverterFactory implements ItemRequestConverterFactory
{
#Override
public <IR extends ItemRequest> ItemRequestConverter<IR> newInstance(Class<IR> itemRequestClass)
{
// Does not make sense: Comparing ItemRequest class with something
// that is probably an implementation of ItemRequestConverter
if (itemRequestClass.equals(CreatePartRequestConverter.class))
{
return new CreatePartRequestConverter<IR>();
}
return null;
}
}
Depending on what you actually want to achieve, you could pass the ItemRequestConverter class to the factory method (and parameterize the method accordingly) :
interface ItemRequestConverter<IR extends ItemRequest>{}
interface ItemRequest{}
interface ItemRequestConverterFactory
{
public <IRC extends ItemRequestConverter<?>> ItemRequestConverter<?> newInstance(Class<IRC> itemRequestConverterClass);
}
class CreatePartRequestConverter implements ItemRequestConverter<ItemRequest>
{
}
class DefaultItemRequestConverterFactory implements ItemRequestConverterFactory
{
#Override
public <IRC extends ItemRequestConverter<?>> ItemRequestConverter<?> newInstance(
Class<IRC> itemRequestConverterClass)
{
if (itemRequestConverterClass.equals(CreatePartRequestConverter.class))
{
return new CreatePartRequestConverter();
}
return null;
}
}
If you also need the information about the ItemRequest class, the method signatures may start to become a little bit nasty, and one would have to analyze in more detail where and how this type information should be provided or used.
EDIT for the comment:
I think that this is not possible in a (compile-time) type-checked way. The problem here is that at the point where you create and return the new ItemRequestConverter, you can not say for sure that it is an ItemRequestConverter whose type parameter is IR. Or the other way around: You can't detect at compile-time that the type parameter of CreatePartRequestConverter (namely, CreatePartRequest) is the same as IR.
Based on the code snippets discussed so far, I think that it should be possible to simply cast in this case. The responsibility of making sure that the cast is valid is then left to the one who implements the "runtime-type-check":
if (itemRequestClass.equals(CreatePartRequest.class))
{
CreatePartRequestConverter result = new CreatePartRequestConverter();
return (ItemRequestConverter<IR>) result;
}
because nothing prevents you from writing
// !!!
if (itemRequestClass.equals(SomeCompletelyDifferentRequest.class))
{
CreatePartRequestConverter result = new CreatePartRequestConverter();
return (ItemRequestConverter<IR>) result;
}
So it should be valid to do this:
interface ItemRequestConverter<IR extends ItemRequest>{}
interface ItemRequest{}
interface ItemRequestConverterFactory
{
public <IR extends ItemRequest> ItemRequestConverter<IR>
newInstance(Class<IR> itemRequestClass);
}
class CreatePartRequest implements ItemRequest {}
class CreatePartRequestConverter
implements ItemRequestConverter<CreatePartRequest> {}
class DefaultItemRequestConverterFactory implements ItemRequestConverterFactory
{
#Override
public <IR extends ItemRequest> ItemRequestConverter<IR> newInstance(
Class<IR> itemRequestClass)
{
if (itemRequestClass.equals(CreatePartRequest.class))
{
CreatePartRequestConverter result = new CreatePartRequestConverter();
return (ItemRequestConverter<IR>) result;
}
return null;
}
}
public class GenericFactoryTest
{
public static void main(String[] args)
{
ItemRequestConverterFactory factory = null;
ItemRequestConverter<CreatePartRequest> converter =
factory.newInstance(CreatePartRequest.class);
}
}

What method signature is appropiate returning a generic object?

What should be the signature of a method that takes a generic object and returns another generic object, one that either is the same or a sub class of the original class? That is, if the method takes some generic class A, the returned object is guaranteed to be either A or B such that B extends A (directly or indirectly)?
The code below exemplifies what I'm trying to do, in the function getList():
package com.company;
import java.util.ArrayList;
public class Main {
private Main(){
List<String> stringList = new GenericMessageListCreator.getList(StringGenericMessage.class);
}
private class GenericMessageListCreator() {
public List<GenericMessage<T1>> getList(Class<T1 extends GenericMessage> clazz) {
return new ArrayList<T1>();
}
}
private class GenericMessage<T> {
public GenericMessage(){};
private T internalValue;
public void setValue(T value) {
this.internalValue = value;
}
public void echoValue() {
System.out.println("I contain " + internalValue);
}
}
private class StringMessage extends GenericMessage<String>{}
private class IntegerMessage extends GenericMessage<Integer>{}
}
Example aside, in actuality I'm writing a registry of classes that are used for Commands in a command pattern. When I get an object by its class I want to fetch the appropriate Command and pass the object to it.
I think you are looking for this signature:
public <T1 extends GenericMessage> List<GenericMessage<T1>> getList(Class<T1> clazz) {
return new ArrayList<T1>();
}
You'll find more info about generic methods here.
EDIT
Based on what I understand from your sample code, I would go for something like (I corrected some syntax errors in your code):
private class GenericMessageListCreator {
public <U, V extends GenericMessage<U>> List<U> getList(Class<V> clazz){
return new ArrayList<U>();
}
}
private class GenericMessage<T> {
public GenericMessage(){};
private T internalValue;
public void setValue(T value)
{
this.internalValue = value;
}
public void echoValue() {
System.out.println("I contain " + internalValue);
}
}
private class StringMessage extends GenericMessage<String>{}
private class IntegerMessage extends GenericMessage<Integer>{}
Thus, you'll be able to create a List<String from `StringMessage like this:
List<String> stringList = new GenericMessageListCreator().getList(StringMessage.class);
I'm not even sure which method you want to have this behavious on, but I've assuming it's getList():
private class GenericMessageListCreator() {
public <T extends GenericMessage<?>> List<T> getList(Class<T> clazz) {
return new ArrayList<T>();
}
}

Categories