Skip to content
Advertisement

Tag: generics

Avoid cast in generic factories

Suppose I have a class structure looking like this: Now I have a class that uses those intervals and factories: Is this the right way of approaching the problem if a class needs to create instances from a type variable? Is there a way around the cast in the SomeClass constructor? The obvious things don’t work – having a wildcard

Factory pattern using generics

I would like to build a class that caches classes of type CachedObject using Map. Below is the factory class. I have a class that extends CacheableObject as below: When I try to create an instance of class X that extends CachedObject using the get method in the factory as below: (please note that cachedObjectFactory is autowired using Spring) I

Parameter to pass in for type Class

I am trying to make a generic Stack class as shown but am unsure of what to put in for type when instantiating since I thought of doing however I get the following error of so I thought of using but I am unsure of what to put inside of cast() since it won’t take now I am stuck. Answer

void with Generics in Java

I have a function that returns void Generic method Implementation of generic interface How can I use void in IRequestHandler<DeleteProductCommand, Void> so that I can map void from iProductService.delete(deleteProductCommand.id); Answer Option 1: Just return null: Option 2: Update the IProductService::delete method to return something meaningful, e.g. a boolean value like Collection::remove does:

Java Generics – Type must be from Class T “or” extend S

I am developing a ComboBox (more for context than actual significance), and I would like to know if the “or” operator exists in Java generics. For now, the declaration looks something like that: Where ComboBoxItem is an interface I created for items that can be used in the ComboBox. However, I’d also like to accept String as a type, which

Is it possible to return the Class of a generic type without any warning?

The question is pretty simple, take a look at the following code: In eclipse and maybe other ide, a warning is generated: So i did change the return type of the getApplicableClass function to: But the question now is, how can i return a Class<ChoiceBox<?>> without warning Answer This case is explicitly treated in the book Effective Java by Joshua

Advertisement