Comparison of "T extends Number" for equality

I have a matrix class that takes a generic object that extends a number.

For instance:

public class Matrix<T extends Number>

I am trying to compare two matrices that have the same values:

Matrix:
row=[0] 273 455 
row=[1] 243 235 
row=[2] 244 205 
row=[3] 102 160 

and

Matrix:
row=[0] 273 455 
row=[1] 243 235 
row=[2] 244 205 
row=[3] 102 160 

In the Matrix class, I have an equals method that looks like this:

public boolean equals(Object obj) {
    if (obj == null)
        return false;
    if (!(obj instanceof Matrix))
        return false;

    Matrix<T> m = (Matrix<T>) obj;
    if (this.rows != m.rows)
        return false;
    if (this.cols != m.cols)
        return false;
    for (int i=0; i<matrix.length; i++) {
        T t1 = matrix[i];
        T t2 = m.matrix[i];
        if (!t1.equals(t2))
            return false;
    }
    return true;
}

This line does not work:

t1.equals(t2)

even if both numbers are equal. e.g. 273 and 273

When I debug the equals method, it fails because it accepts Longs numbers:

This is from the Java SDK Long.class:

public boolean equals(Object obj) {
if (obj instanceof Long) {
    return value == ((Long)obj).longValue();
}
return false;
}

Essentially, it fails because obj is not an instance of Long.

I can easily change the equals method:

        if (t1.longValue()!=t2.longValue())
            return false;

But I'm wondering how to verify equality in this situation and why the equals method on generic T assumes that it is long.

EDIT:

'' Matrix generic type Integer '', ( Long).

:

    Matrix<Integer> matrix1 = new Matrix<Integer>(4, 3);
    matrix1.set(0, 0, 14);
    matrix1.set(0, 1, 9);
    matrix1.set(0, 2, 3);
    matrix1.set(1, 0, 2);
    matrix1.set(1, 1, 11);
    matrix1.set(1, 2, 15);
    matrix1.set(2, 0, 0);
    matrix1.set(2, 1, 12);
    matrix1.set(2, 2, 17);
    matrix1.set(3, 0, 5);
    matrix1.set(3, 1, 2);
    matrix1.set(3, 2, 3);

    Matrix<Integer> matrix2 = new Matrix<Integer>(3, 2);
    matrix2.set(0, 0, 12);
    matrix2.set(0, 1, 25);
    matrix2.set(1, 0, 9);
    matrix2.set(1, 1, 10);
    matrix2.set(2, 0, 8);
    matrix2.set(2, 1, 5);

    Matrix<Integer> result1 = new Matrix<Integer>(4,2);
    result1.set(0, 0, 273);
    result1.set(0, 1, 455);
    result1.set(1, 0, 243);
    result1.set(1, 1, 235);
    result1.set(2, 0, 244);
    result1.set(2, 1, 205);
    result1.set(3, 0, 102);
    result1.set(3, 1, 160);

    Matrix<Integer> matrix3 = matrix1.multiply(matrix2);
    if (!matrix3.equals(result1)) {
        System.err.println("Matrix multiplication error. matrix3="+matrix3+" result1"+result1);
        return false;
    }

Here - equals(). equals().

+4
4

, Long.equals, Matrix<Integer>, , Long. :

public class Matrix<T extends Number> {

    private T[] matrix = null;

:

    this.matrix = (T[]) new Number[rows * cols];

, , null. multiply,

Long result = 0l;
for (int i = 0; i < cols; i++) {
     Long l = row[i].longValue() * column[i].longValue();
     result += l;
}
output.set(r, c, (T) result);

set

public void set(int row, int col, T value) {
    matrix[getIndex(row, col)] = value;
}

, , (T) , . , Long Integer :

Long x = 3L;
Integer y = 4;
x = (Long)y;     // illegal
y = (Integer)x;  // illegal

- (T), , . ( , , , - Number, .) , . , Matrix<Integer>, Long Integer. , , Long matrix. ( , , , T, - .) , matrix[i].equals, a Long matrix, Long.equals.

, , Number, . T , , , Long, .

+3

, .

, :

public static boolean valueEquals(Number n1, Number n2) {
    return n1.longValue() == n2.longValue() && n1.doubleValue() == n2.doubleValue();
}

, BigDecimal, BigInteger , /. , longValue doubleValue, , longValue, 1L 1.1D (- Double.longValue()), doubleValue, , (, 2 ^ 53 2 ^ 63, , , ).

+2

, Long, , .

" [...], equals T , ": . t1.equals(t2), t1 , Long.equals(Object). , .

, "" equals, , . : ? Number.

+1

... equals T , .

: , Matrix<Long>, t1 Long ( Long ) Long.equals() .

Integer.equals():

Matrix<Integer> m1 = ...;
Matrix<Long> m2 = ...;

m1.equals( m2 ); 

m1 Integer, t1.equals(t2) Integer.equals(Long).

, , , ?

, compareTo() ( , , BigDecimal , 2.0 2.00, equals() .

, T extends Number & Comparable<T> (. , : java.lang.Number Comparable?), Long.compareTo(Integer) .

, , ( , t1.longValue() t1.doubleValue()) Comparator<Number>, compareTo(Number lhs, Number rhs) . ( Comparators : http://www.jidesoft.com/javadoc/com/jidesoft/comparator/NumberComparator.html).

If you want to support large numbers, such as BigIntegerand BigDecimal, you can also consider using BigDecimaland create an instance for each value. This should lead to some flexibility, but also require some performance. (Disclaimer: This is just an unverified idea, so don’t just take it as it is, it is just designed to contribute to your own thinking process).

-1
source

Source: https://habr.com/ru/post/1544352/


All Articles