5

Is there in java a way to write generic interface what points out to the implementor of such interface in more convenient way?

For example I've write interface:

public interface Updatable<T> {
    void updateData(T t);
}

I want to point out that only instance of implementor can be passed in updateData(T t) method.

So, i have to write something like this:

public class Office implements Updatable<Office> {

@Override
    public void updateData(Office office) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
...
...
...
}

But seems it is a little ugly. Do you have better variants?

Oleksandr_DJ
  • 1,454
  • 2
  • 14
  • 26
  • 5
    That's how `Comparable>` does it. Why shouldn't you? – Sotirios Delimanolis Apr 15 '13 at 18:41
  • 1
    Yep, Comparable does it as I've pointed out in my example. But I want better way if possible. – Oleksandr_DJ Apr 15 '13 at 18:44
  • 3
    I don't see the problem. This is the purpose of Generics, to specify a type. And that is what you're doing. It's not ugly. Check out [this article](http://www.ibm.com/developerworks/java/library/j-genericdao/index.html) and how they are using Generics for a DAO. – Sotirios Delimanolis Apr 15 '13 at 18:46
  • Related: [Is there a way to refer to the current type with a type variable?](http://stackoverflow.com/questions/7354740/is-there-a-way-to-refer-to-the-current-type-with-a-type-variable). – Paul Bellora Apr 15 '13 at 21:11

3 Answers3

3

People have been asking for the "This" type, mostly for fluent APIs. It's probably never gonna make it. But I think it's ok to just establish a convention - name the type variable This to represent the type of this. So readers see This and know exactly what it is supposed to be; misuse is unlikely

public interface Updatable<This> {
    void updateData(This t);
}

// any Foo implementing Updatable must supply This=Foo

public class Office implements Updatable<Office> 

Since This must be a subtype of Updatable<This>, people often express that constraint

public interface Updatable<This extends Updatable<This>> 

I think it is not necessary, and should not be done. The naming convention of This is good enough.

That constraint is not strict enough either to prevent misuse. For example

public interface Foo<T extends Foo<T>>

public class Foo1 implements Foo<Foo1>  // good, intended use

public class Foo2 implements Foo<Foo1>  // compiles! but not intended
ZhongYu
  • 19,446
  • 5
  • 33
  • 61
3

This is fine and is the least ugly way and clearest way to do it.

Generics is only there to ensure type safety, i.e. avoid casts. Your Updatable interface is perfectly type-safe already. So why change anything?

I want to point out that only instance of implementor can be passed in updateData(T t) method.

The key question is: Why do you want to do that? I don't see any type-safety reason from looking at the interface itself why this is needed.

If the Office class requires that the parameter to its updateData method be Office, fine. But that would be a requirement specific to the Office class, and thus Office should be responsible (and it is in your code) for implementing the interface with the appropriate type parameter.

newacct
  • 119,665
  • 29
  • 163
  • 224
1

I would write

public interface Updatable<T extends Updatable<?>> {
    void updateData(T t);
}

No less ugly, but more explicit that T has to implement the interface.

Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61