Search code examples
javasumbinary-operators

How can I get a Binary Operator from a Class?


Is there a way to get the Binary Operator related to a class? I have this method that has two parameters:

    public <T extends Number> T[] sumArray(T[] arrayToAdd, BinaryOperator<T> add){
        T[] arrayToReturn =  arrayToAdd.clone();
        for (int i = 0; i < arrayToAdd.length; i++) {
            arrayToReturn[i] = arrayToAdd[i];
            for (int j = 0; j < i; j++) {
                arrayToReturn[i] = add.apply(arrayToReturn[i], arrayToAdd[j]);
            }
        }
        return arrayToReturn;
    }

However, I'd like to do something like this:

    public <T extends Number> T[] sumArray(T[] arrayToAdd){
        BinaryOperator add = arrayToAdd.getClass().getBinaryOperator(sum);
        T[] arrayToReturn =  arrayToAdd.clone();
        for (int i = 0; i < arrayToAdd.length; i++) {
            arrayToReturn[i] = arrayToAdd[i];
            for (int j = 0; j < i; j++) {
                arrayToReturn[i] = add.apply(arrayToReturn[i], arrayToAdd[j]);
            }
        }
        return arrayToReturn;
    }
  • Important: The method .getBinaryOperator(sum) doesn't exists.

PD: I'm also having problems with the first method when calling it from another class: api.sumArray(intArray, Integer::sum); The error says: 'Object is not convertible to int'

------------------edit------------------

Minimal Reproducible Example

public class guide6Main<T extends Comparable<T>, N extends Number> {
    public static void main(String[] args) {
        ApiArray api = new ApiArray();
        Integer[] intArray = {1, 2, 3, 4};
        api.exercise6(intArray, Integer::sum);
     }
}
public class ApiArray<T extends Comparable<T>, N extends Number> {
    public  <N extends Number> N[] exercise6(N[] arrayToAdd, BinaryOperator<N> add){
        N[] arrayToReturn =  arrayToAdd.clone();
        for (int i = 0; i < arrayToAdd.length; i++) {
            arrayToReturn[i] = arrayToAdd[i];
            for (int j = 0; j < i; j++) {
                arrayToReturn[i] =add.apply(arrayToReturn[i],arrayToAdd[j] );
            }
        }
        return arrayToReturn;
    }
}

This is the error:

D:\Documentos\Austral\Álgerba III\src\Guia6\Team2Solution\guide6Main.java:11:49
java: incompatible types: invalid method reference
    incompatible types: java.lang.Object cannot be converted to int

P.D.2: Also, arrayToAdd cannot contain a mixture of types, I mean, it could, but not in this case (Is the minimal reproducible example okay? I've never done something like that)


Solution

  • Answering purely about the error "incompatible types: java.lang.Object cannot be converted to int":

    public class guide6Main<T extends Comparable<T>, N extends Number> {
        public static void main(String[] args) {
            ApiArray api = new ApiArray();
            Integer[] intArray = {1, 2, 3, 4};
            api.exercise6(intArray, Integer::sum);
         }
    }
    
    public class ApiArray<T extends Comparable<T>, N extends Number> {
        public  <N extends Number> N[] exercise6(N[] arrayToAdd, BinaryOperator<N> add){
            // ...
        }
    }
    

    ApiArray is a generic type, but ApiArray api is a raw-typed variable (it has no type parameters).

    Raw types erase all generics, even those unrelated to the omitted type parameters.

    Either declare api using wildcard types:

        ApiArray<?, ?> api = new ApiArray<>();
    

    (Or, obviously, put in non-wildcard types there, if there are types you can usefully add).

    If they're not necessary, remove the type parameters from the class.