0

I'm sorry if this question has been asked before, but I don't really know what to search for.

Anyway, I'm making a math package, and many of the classes extend Function:

package CustomMath;

@SuppressWarnings("rawtypes")
public abstract class Function <T extends Function> {

    public abstract Function getDerivative();

    public abstract String toString();

    public abstract Function simplify();

    public abstract boolean equals(T comparison);

}

I want to compare functions to see if they're equal. If they're from the same class, I want to use its specific compare method, but if they're of different classes, I want to return false. Here is one of the classes I have currently:

package CustomMath;

public class Product extends Function <Product> {

public Function multiplicand1;
public Function multiplicand2;

public Product(Function multiplicand1, Function multiplicand2)
{
    this.multiplicand1 = multiplicand1;
    this.multiplicand2 = multiplicand2;
}

public Function getDerivative() {
    return new Sum(new Product(multiplicand1, multiplicand2.getDerivative()), new Product(multiplicand2, multiplicand1.getDerivative()));
}

public String toString() {
    if(multiplicand1.equals(new RationalLong(-1, 1)))
        return String.format("-(%s)", multiplicand2.toString());
    return String.format("(%s)*(%s)", multiplicand1.toString(), multiplicand2.toString());
}

public Function simplify() {
    multiplicand1 = multiplicand1.simplify();
    multiplicand2 = multiplicand2.simplify();
    if(multiplicand1.equals(new One()))
        return multiplicand2;
    if(multiplicand2.equals(new One()))
        return multiplicand1;
    if(multiplicand1.equals(new Zero()) || multiplicand2.equals(new Zero()))
        return new Zero();
    if(multiplicand2.equals(new RationalLong(-1, 1))) //if one of the multiplicands is -1, make it first, so that we can print "-" instead of "-1"
    {
        if(!multiplicand1.equals(new RationalLong(-1, 1))) // if they're both -1, don't bother switching
        {
            Function temp = multiplicand1;
            multiplicand1 = multiplicand2;
            multiplicand2 = temp;
        }
    }
    return this;
}

public boolean equals(Product comparison) {
    if((multiplicand1.equals(comparison.multiplicand1) && multiplicand2.equals(comparison.multiplicand2)) || 
            (multiplicand1.equals(comparison.multiplicand2) && multiplicand2.equals(comparison.multiplicand1)))
        return true;
    return false;
}

}

How can I do this?

  • 1
    you should account for comparing an object to null, to avoid getting `NPE` – user1406062 Oct 06 '12 at 23:00
  • this sorta looks like case of the http://stackoverflow.com/questions/tagged/crtp - you may want to look at related questions involving java like http://stackoverflow.com/questions/2165613/java-generic-type also see: http://en.wikipedia.org/wiki/Talk%3ACuriously_recurring_template_pattern – Ray Tayek Oct 06 '12 at 23:11
  • you probably just want `public abstract class Function ` – newacct Jan 08 '14 at 19:59

2 Answers2

1

With generic you have the guarantee that the equals method is only apply with the type 'T', in this case 'Product'. You can't passe another class type.

Another possibility would be in classe Function define:

public abstract boolean equals(Function comparison);

And in classe Product the object comparison whith a comparison instanceof Product

rjmateus
  • 26
  • 3
1

Override Object.equals(Object) method. You don't need to use generics here. Its body will look something like this

if (other instanceof Product) {
    Product product = (Product) other;
    // Do your magic here
}

return false;
Erik Ghonyan
  • 427
  • 4
  • 12