tarsiswt / metricsink Goto Github PK
View Code? Open in Web Editor NEWDumps metrics into an .xls file
Dumps metrics into an .xls file
Hello,
I'd like to draw your attention to the current implementation of the AbstractFlowSet implementation and its subclasses wrt to the equals/hashCode contract. I've encountered several issues:
ArraySparseSet ars1 = new ArraySparseSet();
ars1.add("a");
ars1.add("b");
ArraySparseSet ars2 = new ArraySparseSet();
ars2.add("b");
ars2.add("a");
System.out.println(ars1.equals(ars2)); // true
System.out.println(ars1.hashCode() == ars2.hashCode()); // false; violates the hashCode contract
AbstractFlowSet also accepts intertype equality, but fails to comply to the hashCode contract:
ArrayPackedSet aps = new ArrayPackedSet(new CollectionFlowUniverse<String>(Arrays.asList("a","b")));
aps.add("b");
aps.add("a");
System.out.println(ars1.equals(aps)); // true
System.out.println(ars1.hashCode() == aps.hashCode()); // false; violates the hashCode contract
This happens because the hashCode is calculated based on the iteration order, whereas the equals checks for equality independent of order.
One way to fix this could be to relax the hashCode implementation below:
public int hashCode() {
final int PRIME = 31;
int result = 1;
Iterator iter = iterator();
while(iter.hasNext()) {
Object o = iter.next();
result = PRIME * result + o.hashCode();
}
return result;
}
to something like:
public int hashCode() {
int result = 1;
Iterator iter = iterator();
while(iter.hasNext()) {
Object o = iter.next();
result += o.hashCode();
}
return result;
}
ToppedSet ts1 = new ToppedSet(ars1);
System.out.println(ts1.equals(ars1)); // false
System.out.println(ars1.equals(ts1)); // true; violates the simmetry clause of the equals contract.
class MyFlowSet extends ArraySparseSet {
private int myState = 0;
public void setState(int state) {
myState = state;
}
@Override
public boolean equals(Object otherFlow) {
if (otherFlow instanceof MyFlowSet) {
MyFlowSet other = (MyFlowSet) otherFlow;
if (other.numElements != this.numElements || other.myState != this.myState)
return false;
for(int i = 0; i < this.numElements; i++)
if(!other.contains(this.elements[i]))
return false;
return true;
}
return super.equals(otherFlow);
}
@Override
public List toList() {
return super.toList();
}
}
Because intertype equality is allowed, the following case can emerge:
MyFlowSet mfs1 = new MyFlowSet();
mfs1.add("a");
mfs1.add("b");
MyFlowSet mfs2 = new MyFlowSet();
mfs2.add("a");
mfs2.add("b");
mfs2.setState(1);
System.out.println(mfs1.equals(ars1)); // true
System.out.println(mfs2.equals(ars1)); // true
System.out.println(mfs1.equals(mfs2)); // false; violates the transitivity clause of the equals contract
I believe that many of these issues could be avoided by separating the object equality from the "content equality", maybe using the EquivTo interface and adjusting the fixed point algorithm, or by disallowing intertype equality. Both of these alternatives will most likely break backwards compatibility wth existing code.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.