mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-15 22:03:26 +00:00
Estimated hours taken: 2
Branches: main
Override Object#equals for Mercury enumerations in the Java grades
and document that this should be used for equality testing in Java
code. The problem with using == is that instances might not have
the same addresses if they are serialized and then deserialized.
MercuryEnum is now a class and not an interface.
compiler/ml_type_gen.m:
Inherit the MercuryEnum class in Mercury enumerations
instead of implementing the MercuryEnum interface.
compiler/mlds_to_java.m:
Don't output the MR_value member in Java classes that correspond to
Mercury enumerations. This member is now inherited from MercuryEnum.
Don't mangle classes in the Mercury runtime.
doc/reference_manual.texi:
Document that the equals method should be used for exported
Mercury enumerations.
java/runtime/MercuryEnum.java:
Make MercuryEnum a class and implement the equals method.
Also implement the hashcode method.
30 lines
750 B
Java
30 lines
750 B
Java
//
|
|
// Copyright (C) 2009 The University of Melbourne.
|
|
// This file may only be copied under the terms of the GNU Library General
|
|
// Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
//
|
|
// This is the superclass of all classes generated by the Java back-end
|
|
// which correspond to Mercury-defined enumeration types.
|
|
//
|
|
|
|
package jmercury.runtime;
|
|
|
|
public abstract class MercuryEnum {
|
|
public final int MR_value;
|
|
|
|
protected MercuryEnum(int val) {
|
|
MR_value = val;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
return o != null && o instanceof MercuryEnum &&
|
|
((MercuryEnum)o).MR_value == this.MR_value;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return MR_value;
|
|
}
|
|
}
|