Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraint hashcode equals fix #210

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/checkers/inference/ConstraintNormalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ public Set<Constraint> toConstraints() {
return constraints;
}

List<Constraint> ifExistsConstraints = new ArrayList<>();
List<Constraint> ifNotExistsConstraints = new ArrayList<>();
Set<Constraint> ifExistsConstraints = new HashSet<>();
Set<Constraint> ifNotExistsConstraints = new HashSet<>();

ifExistsConstraints.addAll(constraints);
for (final ExistentialNode existNode : ifExists.values()) {
Expand Down
2 changes: 1 addition & 1 deletion src/checkers/inference/model/ArithmeticConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public <S, T> T serialize(Serializer<S, T> serializer) {
public int hashCode() {
// We do not hash on annotation location as the result slot is unique for each annotation
// location
return HashCodeUtils.hash(operation, leftOperand, rightOperand, result);
return HashCodeUtils.hash(3079, operation, leftOperand, rightOperand, result);
}

@Override
Expand Down
25 changes: 9 additions & 16 deletions src/checkers/inference/model/CombineConstraint.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package checkers.inference.model;

import java.util.Arrays;

import org.checkerframework.dataflow.util.HashCodeUtils;
import org.checkerframework.javacutil.BugInCF;

/**
Expand Down Expand Up @@ -51,28 +53,19 @@ public Slot getResult() {

@Override
public int hashCode() {
int hc = 1;
hc += ((target == null) ? 0 : target.hashCode());
hc += ((decl == null) ? 0 : decl.hashCode());
hc += ((result == null) ? 0 : result.hashCode());
return hc;
return HashCodeUtils.hash(1543, target, decl, result);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CombineConstraint other = (CombineConstraint) obj;
if (target.equals(other.target) &&
decl.equals(other.decl) &&
result.equals(other.result)) {
return true;
} else {
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
CombineConstraint other = (CombineConstraint) obj;
return target.equals(other.target) && decl.equals(other.decl)
&& result.equals(other.result);
}
}
23 changes: 9 additions & 14 deletions src/checkers/inference/model/ComparableConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;

import org.checkerframework.dataflow.util.HashCodeUtils;
import org.checkerframework.framework.type.QualifierHierarchy;
import org.checkerframework.javacutil.BugInCF;

Expand Down Expand Up @@ -80,26 +81,20 @@ public Constraint make(Slot first, Slot second) {

@Override
public int hashCode() {
int result = 1;
result = result + ((first == null) ? 0 : first.hashCode());
result = result + ((second == null) ? 0 : second.hashCode());
return result;
// ComparableConstraint is insensitive to the order of the slots
return HashCodeUtils.hash(769, first.hashCode() + second.hashCode());
}

@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ComparableConstraint other = (ComparableConstraint) obj;
if ((first.equals(other.first) && second.equals(other.second))
|| (first.equals(other.second) && (second.equals(other.first)))) {
return true;
} else {
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ComparableConstraint other = (ComparableConstraint) obj;
return (first.equals(other.first) && second.equals(other.second))
|| (first.equals(other.second) && second.equals(other.first));
}
}
20 changes: 16 additions & 4 deletions src/checkers/inference/model/ConstraintManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public PreferenceConstraint createPreferenceConstraint(VariableSlot variable, Co
* Creates an {@link ExistentialConstraint} for the given slot and lists of constraints.
*/
public ExistentialConstraint createExistentialConstraint(Slot slot,
List<Constraint> ifExistsConstraints, List<Constraint> ifNotExistsConstraints) {
Set<Constraint> ifExistsConstraints, Set<Constraint> ifNotExistsConstraints) {
return ExistentialConstraint.create((VariableSlot) slot, ifExistsConstraints,
ifNotExistsConstraints, getCurrentLocation());
}
Expand Down Expand Up @@ -261,14 +261,26 @@ public void addCombineConstraint(Slot target, Slot decl, Slot result) {
* Creates and adds a {@link PreferenceConstraint} to the constraint set.
*/
public void addPreferenceConstraint(VariableSlot variable, ConstantSlot goal, int weight) {
add(createPreferenceConstraint(variable, goal, weight));
PreferenceConstraint pc = createPreferenceConstraint(variable, goal, weight);
if (constraints.contains(pc)) {
PreferenceConstraint existingPC = (PreferenceConstraint) constraints.stream()
.filter(c -> c.hashCode() == pc.hashCode()).findFirst().get();

if (existingPC.getWeight() != weight) {
throw new BugInCF(
"Constraint " + pc + " already exists in the constraint set with weight "
+ existingPC.getWeight() + ".");
}
} else {
add(pc);
}
}

/**
* Creates and adds a {@link ExistentialConstraint} to the constraint set.
*/
public void addExistentialConstraint(Slot slot, List<Constraint> ifExistsConstraints,
List<Constraint> ifNotExistsConstraints) {
public void addExistentialConstraint(Slot slot, Set<Constraint> ifExistsConstraints,
Set<Constraint> ifNotExistsConstraints) {
add(createExistentialConstraint(slot, ifExistsConstraints, ifNotExistsConstraints));
}

Expand Down
23 changes: 9 additions & 14 deletions src/checkers/inference/model/EqualityConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;

import org.checkerframework.dataflow.util.HashCodeUtils;
import org.checkerframework.javacutil.BugInCF;

/**
Expand Down Expand Up @@ -95,26 +96,20 @@ public Constraint make(Slot first, Slot second) {

@Override
public int hashCode() {
int result = 1;
result = result + ((first == null) ? 0 : first.hashCode());
result = result + ((second == null) ? 0 : second.hashCode());
return result;
// EqualityConstraint is insensitive to the order of the slots
return HashCodeUtils.hash(97, first.hashCode() + second.hashCode());
}

@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EqualityConstraint other = (EqualityConstraint) obj;
if ((first.equals(other.first) && second.equals(other.second))
|| (first.equals(other.second) && (second.equals(other.first)))) {
return true;
} else {
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
EqualityConstraint other = (EqualityConstraint) obj;
return (first.equals(other.first) && second.equals(other.second))
|| (first.equals(other.second) && second.equals(other.first));
}
}
44 changes: 33 additions & 11 deletions src/checkers/inference/model/ExistentialConstraint.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package checkers.inference.model;

import org.checkerframework.javacutil.PluginUtil;
import org.checkerframework.dataflow.util.HashCodeUtils;
import org.checkerframework.javacutil.BugInCF;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* An ExistentialConstraint indicates that solvers need to determine if a variable's annotation
Expand All @@ -27,22 +29,22 @@ public class ExistentialConstraint extends Constraint {
private final VariableSlot potentialVariable;

// The constraints to enforce if potentialVariable exists
private final List<Constraint> potentialConstraints;
private final Set<Constraint> potentialConstraints;

// the constraints to enforce if potentialVariable DOES NOT exist
private final List<Constraint> alternateConstraints;
private final Set<Constraint> alternateConstraints;

private ExistentialConstraint(VariableSlot potentialVariable,
List<Constraint> potentialConstraints,
List<Constraint> alternateConstraints, AnnotationLocation location) {
Set<Constraint> potentialConstraints,
Set<Constraint> alternateConstraints, AnnotationLocation location) {
super(combineSlots(potentialVariable, potentialConstraints, alternateConstraints), location);
this.potentialVariable = potentialVariable;
this.potentialConstraints = Collections.unmodifiableList(potentialConstraints);
this.alternateConstraints = Collections.unmodifiableList(alternateConstraints);
this.potentialConstraints = Collections.unmodifiableSet(potentialConstraints);
this.alternateConstraints = Collections.unmodifiableSet(alternateConstraints);
}

protected static ExistentialConstraint create(VariableSlot potentialVariable,
List<Constraint> potentialConstraints, List<Constraint> alternateConstraints,
Set<Constraint> potentialConstraints, Set<Constraint> alternateConstraints,
AnnotationLocation location) {
if (potentialVariable == null || potentialConstraints == null
|| alternateConstraints == null) {
Expand All @@ -57,10 +59,10 @@ protected static ExistentialConstraint create(VariableSlot potentialVariable,
}

@SafeVarargs
private static List<Slot> combineSlots(Slot potential, final List<Constraint> ... constraints) {
private static List<Slot> combineSlots(Slot potential, final Set<Constraint> ... constraints) {
final List<Slot> slots = new ArrayList<>();
slots.add(potential);
for (final List<Constraint> constraintList : constraints) {
for (final Set<Constraint> constraintList : constraints) {
for (final Constraint constraint : constraintList) {
slots.addAll(constraint.getSlots());
}
Expand All @@ -72,11 +74,11 @@ public VariableSlot getPotentialVariable() {
return potentialVariable;
}

public List<Constraint> potentialConstraints() {
public Set<Constraint> potentialConstraints() {
return potentialConstraints;
}

public List<Constraint> getAlternateConstraints() {
public Set<Constraint> getAlternateConstraints() {
return alternateConstraints;
}

Expand All @@ -85,6 +87,26 @@ public <S, T> T serialize(Serializer<S, T> serializer) {
return serializer.serialize(this);
}

@Override
public int hashCode() {
return HashCodeUtils.hash(389, potentialVariable, potentialConstraints,
alternateConstraints);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ExistentialConstraint other = (ExistentialConstraint) obj;
return potentialVariable.equals(other.potentialVariable)
&& potentialConstraints.equals(other.potentialConstraints)
&& alternateConstraints.equals(other.alternateConstraints);
}

@Override
public String toString() {
String tab = " ";
Expand Down
2 changes: 1 addition & 1 deletion src/checkers/inference/model/ImplicationConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public Constraint getConclusion() {

@Override
public int hashCode() {
return HashCodeUtils.hash(assumptions, conclusion);
return HashCodeUtils.hash(12289, assumptions, conclusion);
}

@Override
Expand Down
23 changes: 9 additions & 14 deletions src/checkers/inference/model/InequalityConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;

import org.checkerframework.dataflow.util.HashCodeUtils;
import org.checkerframework.javacutil.BugInCF;

public class InequalityConstraint extends Constraint implements BinaryConstraint {
Expand Down Expand Up @@ -73,26 +74,20 @@ public Constraint make(Slot first, Slot second) {

@Override
public int hashCode() {
int result = 1;
result = result + ((first == null) ? 0 : first.hashCode());
result = result + ((second == null) ? 0 : second.hashCode());
return result;
// InequalityConstraint is insensitive to the order of the slots
return HashCodeUtils.hash(193, first.hashCode() + second.hashCode());
}

@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
InequalityConstraint other = (InequalityConstraint) obj;
if ((first.equals(other.first) && second.equals(other.second))
|| (first.equals(other.second) && (second.equals(other.first)))) {
return true;
} else {
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
InequalityConstraint other = (InequalityConstraint) obj;
return (first.equals(other.first) && second.equals(other.second))
|| (first.equals(other.second) && second.equals(other.first));
}
}
34 changes: 7 additions & 27 deletions src/checkers/inference/model/PreferenceConstraint.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package checkers.inference.model;

import java.util.Arrays;

import org.checkerframework.dataflow.util.HashCodeUtils;
import org.checkerframework.javacutil.BugInCF;

/**
Expand Down Expand Up @@ -49,40 +51,18 @@ public int getWeight() {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((variable == null) ? 0 : variable.hashCode());
result = prime * result
+ ((goal == null) ? 0 : goal.hashCode());
result = prime * result + weight;
return result;
return HashCodeUtils.hash(6151, variable, goal);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PreferenceConstraint other = (PreferenceConstraint) obj;
if (variable == null) {
if (other.variable != null) {
return false;
}
} else if (!variable.equals(other.variable)) {
return false;
}

if (goal == null) {
if (other.goal != null) {
return false;
}
} else if (!goal.equals(other.goal)) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}

return weight == other.weight;
PreferenceConstraint other = (PreferenceConstraint) obj;
return variable.equals(other.variable) && goal.equals(other.goal);
}
}
Loading