views:

984

answers:

11

I have a bunch of class who implement a common interface : Command.

And this bunch of class goes to a Map.

To get the Map working correctly, I need to each class who implements Command to override the Object.equals(Object other) method.

it's fine.

But i whould like to force the overriding of equals. => Have a compilation error when something who implement command dont override equals.

It's that possible ?

Edit : BTW , i will also need to forcing the override of hashcode...

A: 

Since equals() is inherited from Object I doubt you can't really force that, since for every type there is an automatic inherited implementation of equals() available.

Joey
+8  A: 

Can you extends your objects from an abstract XObject rather than java.lang.Object ?

public abstract class XObject
 extends Object
{
@Override
public abstract boolean equals(Object o);
}
Pierre
I'm amazed, but this actually works.
Jorn
There's no guarantee that the implementations of Command will extend this class, though.
skaffman
This was what I was going to post originally :-)
instanceofTom
Ah ... but you could declare your other APIs to require an instance of (a subclass of) your abstract class, rather than just a Command instance. A bit ugly, but if you REALLY want to FORCE people to override equals, that will do it.
Stephen C
+20  A: 

No, you can't. What you can do, however, is use an abstract base class instead of an interface, and make equals() abstract:

abstract class Command {
   // put other methods from Command interface here

   public abstract boolean equals(Object other);
   public abstract int hashCode();
}

Subclasses of Command must then provide their own equals and hashCode methods.

It's generally bad practice to force API users to extend a base class but it may be justified in this case. Also, if you make Command an abstract base class instead of an interface, rather than introducing an artificial base class in addition to the Command interface, then there's no risk of your API users getting it wrong.

skaffman
You might want to add in an abstract `hashCode`, just for completeness.
McDowell
Quite right... edited accordingly
skaffman
work like a charm, and indeed, plus i already had a abstract base class.... so, it's not a big refactoring. Thanks
Antoine Claval
After over a decade of Java as a first language, I just learned something new.
Andrew Duffy
She may be predictable and crusty, but the old girl can still surprise us every now and again :)
skaffman
+1  A: 

This would only be possible if Command was an interface, or an abstract class, where equals(..) is a method declared as abstract.

The problem is that Object, which is the superclass of all objects, already defines this method.

If you wish to indicate that this is a problem (at run-time), you could throw an exception, to force users of your API to override it. But it is not possible at compile time, at least to my knowledge.

Try to work around it, by having an API-specific method, e.g. CommandEquals. The other option is (as mentioned) extend another class which defines an Equals abstract method.

_NT
It only works for abstract classes (as described by Pierre), not for interfaces. You don't have to implement the equals method declared in the interface, because it's already present in your object.
Jorn
Read my second sentence again.
_NT
A: 

I don't think it is possible to force overriding of equals as it comes from the Object class.

On a related note, note that you need to override 'hashCode' method from the Object class when ever you override equals. This becomes espeically important if you are going to use instances of your classes as keys of a Map. Check this article : http://www.artima.com/lejava/articles/equality.html which provides some hints about how to override equals in a correct manner

sateesh
A: 

As the other answers have already explained, no you cannot force the kind of thing that you are trying to.

One thing that might work 'enough' is to define a second interface, call this one MappableCommand.

public interface MappableCommand 
{

}

In your documentation for this interface indicate that a class should only implement this (empty) interface if the class designer has considered the requirements you stated.

Then you can set the Value type of your map to MappableCommand, and only MappableCommands will be able to be added to the map.

This is similar to the logic behind why one needs to implement the (blank) interface Serializable for classes that can be serialized by Java's default serialization mechanisms.

If this doesn't work, then you may have to settle for throwing a run time error;

Fake Edit:

If you wanted to make this requirement more obvious you could define the new interface this way

public interface MappableCommand 
{

    public void iOverrodeTheEqualsMethod();

    public void seriouslyIPromiseThatIOverrodeIt();

}
instanceofTom
A: 

You can create boolean myEquals() in interface Command, and create Adapter like this:

class MyAdapter{
  Command c;
  boolean equals(Object x) {
    return c.myEquals((Command)x);
  }
}

Then you just use map.put(key, new MyAdapter(command)) instead of map.put(key, command)

Nikita Prokopov
A: 

Here's a variation of some of the other proposed solutions:

public abstract class CommandOverridingEquals implements Command {
    public abstract boolean equals(Object other);
    public abstract int hashcode();
}

Map<String, CommandOverridingEquals> map = 
    new HashMap<String, CommandOverridingEquals>();

Or if you really want to be sure, use a checked hashmap; e.g.

Map<String, CommandOverridingEquals> map = Collections.checkedMap(
    new HashMap<String, Command>(),
    String.class, CommandOverridingEquals.class);

But no matter what you do you cannot stop someone doing this:

public class AntiFascistCommand extends CommandOverridingEquals {
    public boolean equals(Object other) { return super.equals(other); }
    public int hashcode() { return super.hashcode(); }
    ...
}

I tend to think that this kind of thing will cause trouble down the track. For example, suppose that I have a bunch of existing command classes that extend some other base class, and (incidentally) override equals and hashcode in the prescribed way. Problem is, I cannot use those classes. Instead, I'm forced to reimplement them or write a bunch of wrappers.

IMO, it is a bad idea to try to force the developer into a particular implementation pattern. It would be better to put some strong warnings into the Javadocs and rely on the developers to do the right thing.

Stephen C
Sure, but if the developper miss the .equals override. It will be a mess in several map. And hard to figure out. I take the risk.
Antoine Claval
A: 

interface A{ public boolean equal2(Object obj); }

abstract class B implements A {

@Override
public boolean equals(Object obj) {
    return equal2(obj);
}

}

class C extends B {

public boolean equal2(Object obj) {
    throw new UnsupportedOperationException("Not supported yet.");
}

}

davideconsonni
so C must ovverride a method used for equals.
davideconsonni
A: 

Well if you want a runtime check you can do something like:

    interface Foo{

}
class A implements Foo {

}
class B implements Foo {
 @Override
 public boolean equals(Object obj) {
  return super.equals(obj);
 }
}

public static void main(String[] args) {
 Class<A> clazzA = A.class;
 Class<B> clazzB = B.class;

 Class<Object> objectClass = Object.class;
 try {
  Method methodFromObject = objectClass.getMethod("equals",Object.class);
  Method methodFromA = clazzA.getMethod("equals",Object.class);
  Method methodFromB = clazzB.getMethod("equals",Object.class);
  System.out.println("Object == A" + methodFromObject.equals(methodFromA));
  System.out.println("Object == B" + methodFromObject.equals(methodFromB));
 } catch (SecurityException e) {
  e.printStackTrace();
 } catch (NoSuchMethodException e) {
  e.printStackTrace();
 }
}

Will print true for the first one and false for the second. If you want it compile time looks like the only option is to create an annotation and use annotation processing tool to check that all annotated classes overrides equals.

Nikolay Ivanov
A: 

Is it possible for you to provide your own java.util.comparator to the map in question?

DerMike