java generics: accepting an interface in Class<T> - java

Let's say I have ClasseA implements I, how do I do something like:
public class Classe<T> {
void function(Class<T> param) {
...
}
}
Classe<I> c=new Classe();
c.function(ClasseA.class);
Since ClasseA implements the interface I, I would like function to accept either a class literal of type ClasseA or a class implementing I.

Something like this should work:
public Class Classe<T extends I> {
void function(Class<T> param) {
...
}
}

There is a good tutorial about generics available here: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
A possible solution to your problem:
public class Snippet {
static class Classe<T> {
public void function(Class<? extends T> param) {
System.out.println(param);
}
}
interface I {
}
class ClasseA implements I {
}
public static void main(String[] args) {
Classe<I> c = new Classe();
c.function(ClasseA.class);
}
}

You can specify constraints on the type this way: Classe<T extends I>

Related

How to create generic abstract method that accepts different enums?

How can I create abstract generic method doSomething() that accepts different enums? Enum1 and Enum2, Enum3 and so on?
public abstract class NumerOne {
public abstract void doSomething();
}
public class NumberTwo extends NumberOne {
#Override
public void doSomething (Enum1 enum1) {
enum1.createSomething();
}
The most appropriate way to accept a handful of Enum types, but not accept any enum (<T extends Enum<T>) or (even worse) Object would be to create an interface and have all the enums that you want to accept implement that interface:
interface CreatorOfSomething {
// I have no idea what type should be returned here,
// as you don't use this value in your example.
// But I'm pretty sure it can't be void, so I'll go with Integer.
// You can have this parameterised as <T> at the interface level.
Integer createSomething();
}
enum Enum1 implements CreatorOfSomething {
A, B, C;
#Override
public Integer createSomething() {
return ordinal();
}
}
enum Enum2 implements CreatorOfSomething {
X { // you can override the method for individual constants
#Override
public Integer createSomething() {
// .....
}
},
Y { ....
}
Then your method would look like:
public void doSomething(CreatorOfSomething creator) {
creator.createSomething();
}
The code you posted does not even compile. Nor could we run it. Next time please provide an SSCCE in which you address your question.
Here's the solution for the problem you have:
abstract class NumberOne {
public abstract <T extends Enum<T>> void doSomething(T pEnum);
}
enum Enum1 {
A, B
}
enum Enum2 {
C, D
}
public class NumberTwo extends NumberOne {
#Override public <T extends Enum<T>> void doSomething(final T pEnum) {
System.out.println("Value: " + pEnum);
}
public static void main(final String[] args) {
final NumberTwo n2 = new NumberTwo();
n2.doSomething(Enum1.A);
n2.doSomething(Enum2.D);
}
}

How get Instance using Generic approach

I am trying to build one page assembler using generic approach. Following is piece of code.
In this code IEntity is marker interface for DB entities.
public abstract class PageHrefBuilder implements HrefBuilder<IEntity, PageLinks> {
#Override
public PageLinks buildLinks(IEntity entity) {
return null;
}
}
public interface HrefBuilder<E extends IEntity, L extends Links> {
public L buildLinks(E dto);
}
So we have one interface says can build links using IEntity type of class and return Links type of value. So I want to write some common code in abstract class and abstract class does not know what type entity it suppose to deal with.
For example entity can UserEntity, OrderEntity and so on.
So my question is how in abstract class I can get the class instance to build links with using instanceof or if else approach.
Could someone help me on this.
You can make your buildLinks method to take Class<T> parameter instead of the object you have to pass.
so it will be
public L buildLinks(Class<E> dto);
then in your abstract class
#Override
public PageLinks buildLinks(Class<IEntity> dto) {
return dto.newInstance();
}
Hope this answer could help you.
This got a bit over-engineered but it works and all generics are proper. Hopefully it shows you how you can build your solution:
public class GenericsSample {
public abstract class Links {
String data;
public Links(String data) {
this.data = data;
}
}
public class UserLinks extends Links {
public UserLinks(String data) {
super(data);
}
}
public class PageLinks extends Links {
public PageLinks(String data) {
super(data);
}
}
public abstract class IEntity<L extends Links> {
public abstract L buildLinks();
}
public class UserEntity extends IEntity<UserLinks> {
#Override
public UserLinks buildLinks() {
return new UserLinks("From UserEntity");
}
}
public class PageEntity extends IEntity<PageLinks> {
#Override
public PageLinks buildLinks() {
return new PageLinks("From PageEntity");
}
}
public interface HrefBuilderInterface<E extends IEntity<L>, L extends Links> {
public L buildLinks(E dto);
}
public class HrefBuilder<E extends IEntity<L>, L extends Links> implements HrefBuilderInterface<E, L> {
#Override
public L buildLinks(E entity) {
return entity.buildLinks();
}
}
public static void main(String[] args) {
new GenericsSample().go();
}
private void go() {
System.out.println(new HrefBuilder<UserEntity, UserLinks>().buildLinks(new UserEntity()).data);
System.out.println(new HrefBuilder<PageEntity, PageLinks>().buildLinks(new PageEntity()).data);
}
}
Also note that thanks to this setup the following is not valid:
System.out.println(new HrefBuilder<PageEntity, PageLinks>().buildLinks(new UserEntity()).data);
You can declare abstract method in abstract class to get new instance of Links interface:
abstract class PageHrefBuilder<E extends IEntity, L extends Links> implements HrefBuilder<E, L> {
#Override
public L buildLinks(E dto) {
L links = newLinks();
// ...
return links;
}
protected abstract L newLinks();
}
and implement getting new instance in realization.

Java generics static type inference

I do have an abstract class with an delegation interface defined:
public abstract class MyAbstractClass extends AsyncLoader {
public interface MyAbstractClassDelegate<M> {
//The parameter in this method should be the concrete subtype of MyAbstractClass
public M performThisCall(MyAbstractClass concreteSubclassOfAbstractClass);
}
private MyAbstractClassLoaderDelegate delegate;
...
}
The Problem is, I do not want the delegate parameter to be MyAbstractClass, instead it should be the concrete subclass. Why? Because the implementation of the delegate needs the concrete subclass for further handling and I don't want to cast it...
I know I could define an Interface in each subclass, but it'll look the same in every subclass except for the parameter type
EDIT
Here is the perfect solution solving exactly what I wanted. Great thanks!
public abstract class MyAbstractClass {
public interface MyAbstractClassDelegate<M, Subtype extends MyAbstractClass> {
public M myMethod(Subtype t);
}
}
Is this possible with java 6 and if yes - how?
My solution would be:
public final class Example<T extends Example<T>> {
public interface Interface<M, Subtype extends Interface<M, Subtype>> {
public M myMethod(Subtype t);
}
}
You have no access to the generic from the outer class inside the interface (because the interface is static) so you have to declare it again.
If you use your interface you get something like this:
private static class Impl1 implements Interface<String, Impl1> {
#Override
public String myMethod(final Impl1 t) {
return null;
}
}
I don't know if it will help but here is my complete example:
public final class Example<M, T extends Example.Delegate<M, T>> {
public interface Delegate<M, Subtype extends Delegate<M, Subtype>> {
public M myMethod(Subtype t);
}
private T delegate;
private static class Impl1 implements Delegate<String, Impl1> {
#Override
public String myMethod(final Impl1 t) {
return null;
}
}
public static void main(String[] args) {
Example<String, Impl1> example = new Example<>();
example.delegate = new Impl1();
example.delegate.myMethod(example.delegate); //works but whout?
}
}
What you could do is to give your abstract class a type parameter with the concrete subclass, similar to the way Java's Enum does it.
Something along the lines of this:
public abstract class MyAbstractClass<S extends MyAbstractClass<S>> extends AsyncLoader {
public interface MyAbstractClassDelegate<M, S> {
public M performThisCall(S concreteSubclassOfAbstractClass);
}
...

Is it possible to "overload" a generic class by using different type list?

What I want is actually something like this:
public class Foo<T> {
// ...
}
public class Foo<T, S> {
// ...
}
Note that the name of the classes are the same, but the length of type list is different. The code above doesn't work in Java, but I hope it shows my intent. Is it possible to do similar thing in Java?
Example:
public class Foo<T> {
public Integer call(T input) {
// ...
}
}
public class Foo<T, S> {
public S call(T input) {
// ...
}
}
You don't override classes, you override methods. Classes might be subclassed.
This is possible:
public class Zoo<T, S, U> extends Foo<T>
{
// ...
}
I guess this is what you are trying to do:
Class foo1.Foo:
package foo1;
public class Foo<T, S> {
public S call(T input) {
// ...
}
}
Class foo2.Foo:
package foo2;
public class Foo<T> extends foo1.Foo<T, Integer> {
public Integer call(T input) {
// ...
}
}
When you are defining a class as
public class Foo<T> {
// ...
}
Technically we are telling the compiler to replace the Generic with a Object as T is unbounded.
When we define the class as
public class Foo<T, S> {
// ...}
Compiler will not know how to resolve this.

How to define/implement this interface with generics in a simpler way?

I'm working in a Genetic Algorithm and I want it as abstract as possible to be able to reuse the GA. I defined and implemented a Population Interface, and well it works, but I'm sure that's not the best way to do it. I don't have great experience with Java Generics. Is there an easier way of defining and implementing the Population interface (e.g. maybe avoid a cast conversion? avoid a new list in getChromosomes() ?)
public interface Population
{
void addChromosomes(List<? extends Chromosome> chromosomes);
List<Chromosome> getChromosomes();
// More code here ...
}
public class TSPPopulation implements Population
{
private List<TSPChromosome> chromosomes;
#Override
public void addChromosomes(List<? extends Chromosome> chromosomes) {
for (Chromosome chromosome : chromosomes) {
this.chromosomes.add((TSPChromosome) chromosome);
}
}
#Override
public List<Chromosome> getChromosomes() {
List<Chromosome> newList = new ArrayList<Chromosome>();
for (TSPChromosome chromosome : chromosomes) {
newList.add(chromosome);
}
return newList;
}
}
Use a Bounded Wildcard in your interface:
public interface Population<T extends Chromosome>{
void addChromosomes(List<T> chromosomes);
List<T> getChromosomes();
}
public class TSPPopulation implements Population<TSPChromosome>
{
private List<TSPChromosome> chromosomes;
#Override
public void addChromosomes(List<TSPChromosome> chromosomes) {
...
}
#Override
public List<TSPChromosome> getChromosomes() {
...
}
}
The simplest solution is extending a list (then use addAll(...) to add a list of Chromosoms to the list):
class Population<T extends Chromosome> extends ArrayList<T> {
}
But if you want the same structure I would make Population into a generic list class. That way both add... and get... methods can be handled in the generic base class. If you do want to override any other feature you just extend Population (class TSPPopulation extends Population<TSPChromosome>.
Usage:
public static void main(String... args) {
Population<TSPChromosome> tspPopulation = new Population<TSPChromosome>();
...
}
Implementation:
class Population<T extends Chromosome> {
private List<T> chromosomes = new ArrayList<T>();
public void addChromosomes(List<T> chromosomes) {
this.chromosomes.addAll(chromosomes);
}
public List<T> getChromosomes() {
return new ArrayList<T>(this.chromosomes);
}
}
It would be much safer if you made the Population generic itself:
public interface Population<T extends Chromosome> {
void addChromosomes(List<T> chromosomes);
List<T> getChromosomes();
}
public class TspPopulation implements Population<TspChromosome>{
#Override
public void addChromosomes(List<TspChromosome> chromosomes){
//
}
#Override
public List<TspChromosome> getChromosomes(){
//
}
}
That way you would not need any casting in client code.
I know GAs, and I would question whether your Population implementation actually needs to know which kind of Chromosome you put in. Do you really have different Population implementations depending on the Chromosome subclass? Or what you really want is to make sure you have the same subclass of Chromosome in a Population? In this last case, you can define the Population interface as others suggested, and the make a generic implementation (or skip the interface altogether):
public class PopulationImpl implements Population<T extends Chromosome> {
private List<T> chromosomes;
#Override
public void addChromosomes(List<T> chromosomes) {
this.chromosomes.addAll(chromosomes);
}
#Override
public List<T> getChromosomes() {
return new ArrayList<T>(chromosomes);
}
}
Be careful not to put too many generics, or you will end up with generics hell, or tons of casts which will make generics more annoying than useful.
Yes, for instance:
public interface Population<T extends Chromosome>
{
void addChromosomes(List<T> chromosomes);
List<T> getChromosomes();
// More code here ...
}
public class TSPPopulation implements Population<TSPChromosome>
{
private List<TSPChromosome> chromosomes;
#Override
public void addChromosomes(List<TSPChromosome> chromosomes) {
this.chromosomes.addAll(chromosomes);
}
#Override
public List<TSPChromosome> getChromosomes() {
return new ArrayList<TSPChromosome>(chromosomes);
}
}

Categories