hashCode and equals

This commit is contained in:
honfika@gmail.com
2015-07-12 18:14:26 +02:00
parent 904d1f64f1
commit 9a0a54bebb
24 changed files with 160 additions and 128 deletions

View File

@@ -27,13 +27,14 @@ import java.util.Objects;
*/
public class DottedChain {
public List<String> parts = new ArrayList<>();
public final List<String> parts;
public DottedChain(List<String> parts) {
this.parts = new ArrayList<>(parts);
}
public DottedChain(String... parts) {
this.parts = new ArrayList<>();
for (int i = 0; i < parts.length; i++) {
this.parts.add(parts[i]);
}
@@ -81,7 +82,7 @@ public class DottedChain {
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + Objects.hashCode(this.parts);
hash = 89 * hash + Objects.hashCode(parts);
return hash;
}
@@ -97,7 +98,7 @@ public class DottedChain {
return false;
}
final DottedChain other = (DottedChain) obj;
if (!Objects.equals(this.parts, other.parts)) {
if (!Objects.equals(parts, other.parts)) {
return false;
}
return true;

View File

@@ -28,14 +28,21 @@ import java.util.List;
public class GraphPart implements Serializable {
public static final int TYPE_NONE = 0;
public static final int TYPE_LOOP_HEADER = 1;
public static final int TYPE_PRELOOP = 3;
public static final int TYPE_REENTRY = 2;
public boolean traversed = false;
public int DFSP_pos = 0;
public GraphPart iloop_header;
public int type = TYPE_NONE;
public boolean irreducible = false;
public int start = 0;
@@ -315,10 +322,10 @@ public class GraphPart implements Serializable {
return false;
}
final GraphPart other = (GraphPart) obj;
if (this.start != other.start) {
if (start != other.start) {
return false;
}
if (this.end != other.end) {
if (end != other.end) {
return false;
}
return true;

View File

@@ -19,6 +19,7 @@ package com.jpexs.decompiler.graph;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
*
@@ -30,7 +31,7 @@ public class GraphPath implements Serializable {
private final List<Integer> vals = new ArrayList<>();
public String rootName = "";
public final String rootName;
public GraphPath(String rootName, List<Integer> keys, List<Integer> vals) {
this.rootName = rootName;
@@ -39,10 +40,15 @@ public class GraphPath implements Serializable {
}
public GraphPath(List<Integer> keys, List<Integer> vals) {
rootName = "";
this.keys.addAll(keys);
this.vals.addAll(vals);
}
public GraphPath() {
rootName = "";
}
public boolean startsWith(GraphPath p) {
if (p.length() > length()) {
return false;
@@ -52,10 +58,10 @@ public class GraphPath implements Serializable {
List<Integer> otherVals = new ArrayList<>(p.vals);
for (int i = 0; i < p.length(); i++) {
if (keys.get(i) != otherKeys.get(i)) {
if (!Objects.equals(keys.get(i), otherKeys.get(i))) {
return false;
}
if (vals.get(i) != otherVals.get(i)) {
if (!Objects.equals(vals.get(i), otherVals.get(i))) {
return false;
}
}
@@ -82,9 +88,6 @@ public class GraphPath implements Serializable {
this.rootName = rootName;
}
public GraphPath() {
}
public int length() {
return vals.size();
}
@@ -100,9 +103,9 @@ public class GraphPath implements Serializable {
@Override
public int hashCode() {
int hash = 5;
hash = 23 * hash + (this.keys != null ? this.keys.hashCode() : 0);
hash = 23 * hash + (this.vals != null ? this.vals.hashCode() : 0);
hash = 23 * hash + (this.rootName != null ? this.rootName.hashCode() : 0);
hash = 23 * hash + arrHashCode(keys);
hash = 23 * hash + arrHashCode(vals);
hash = 23 * hash + Objects.hashCode(rootName);
return hash;
}
@@ -115,34 +118,44 @@ public class GraphPath implements Serializable {
return false;
}
final GraphPath other = (GraphPath) obj;
if (this.rootName == null && other.rootName != null) {
return false;
}
if (this.rootName != null && other.rootName == null) {
if ((rootName == null) != (other.rootName == null)) {
return false;
}
if (this.rootName != null && other.rootName != null) {
if (!this.rootName.equals(other.rootName)) {
return false;
}
if (!Objects.equals(rootName, other.rootName)) {
return false;
}
if (!arrMatch(keys, other.keys)) {
return false;
}
if (!arrMatch(vals, other.vals)) {
return false;
}
return true;
}
private static int arrHashCode(List<Integer> arr) {
if (arr == null || arr.isEmpty()) {
return 0;
}
int hash = 5;
for (Integer i : arr) {
hash = 23 * hash + Objects.hashCode(i);
}
return hash;
}
private static boolean arrMatch(List<Integer> arr, List<Integer> arr2) {
if (arr.size() != arr2.size()) {
return false;
}
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) != arr2.get(i)) {
if (!Objects.equals(arr.get(i), arr2.get(i))) {
return false;
}
}

View File

@@ -36,7 +36,7 @@ public class Loop implements Serializable {
public List<Integer> breakCandidatesLevels = new ArrayList<>();
public long id;
public final long id;
public int leadsToMark;
@@ -73,10 +73,9 @@ public class Loop implements Serializable {
return false;
}
final Loop other = (Loop) obj;
if (this.id != other.id) {
if (id != other.id) {
return false;
}
return true;
}
}

View File

@@ -45,7 +45,7 @@ public class TypeFunctionItem extends GraphTargetItem {
@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.fullTypeName);
hash = 83 * hash + Objects.hashCode(fullTypeName);
return hash;
}
@@ -58,7 +58,7 @@ public class TypeFunctionItem extends GraphTargetItem {
return false;
}
final TypeFunctionItem other = (TypeFunctionItem) obj;
if (!Objects.equals(this.fullTypeName, other.fullTypeName)) {
if (!Objects.equals(fullTypeName, other.fullTypeName)) {
return false;
}
return true;

View File

@@ -40,7 +40,7 @@ public class TypeItem extends GraphTargetItem {
public static UnboundedTypeItem UNBOUNDED = new UnboundedTypeItem();
public DottedChain fullTypeName;
public final DottedChain fullTypeName;
public TypeItem(String s) {
this(s == null ? new DottedChain() : new DottedChain(s.split("\\.")));
@@ -58,7 +58,7 @@ public class TypeItem extends GraphTargetItem {
@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.fullTypeName);
hash = 83 * hash + Objects.hashCode(fullTypeName);
return hash;
}
@@ -71,7 +71,7 @@ public class TypeItem extends GraphTargetItem {
return false;
}
final TypeItem other = (TypeItem) obj;
if (!Objects.equals(this.fullTypeName, other.fullTypeName)) {
if (!Objects.equals(fullTypeName, other.fullTypeName)) {
return false;
}
return true;

View File

@@ -23,6 +23,7 @@ import com.jpexs.decompiler.graph.GraphSourceItemPos;
import com.jpexs.decompiler.graph.GraphTargetItem;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public abstract class BinaryOpItem extends GraphTargetItem implements BinaryOp {
@@ -31,7 +32,7 @@ public abstract class BinaryOpItem extends GraphTargetItem implements BinaryOp {
public GraphTargetItem rightSide;
protected String operator = "";
protected final String operator;
@Override
public GraphPart getFirstPart() {
@@ -107,9 +108,9 @@ public abstract class BinaryOpItem extends GraphTargetItem implements BinaryOp {
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + (this.leftSide != null ? this.leftSide.hashCode() : 0);
hash = 71 * hash + (this.rightSide != null ? this.rightSide.hashCode() : 0);
hash = 71 * hash + (this.operator != null ? this.operator.hashCode() : 0);
hash = 71 * hash + Objects.hashCode(leftSide);
hash = 71 * hash + Objects.hashCode(rightSide);
hash = 71 * hash + Objects.hashCode(operator);
return hash;
}
@@ -138,13 +139,13 @@ public abstract class BinaryOpItem extends GraphTargetItem implements BinaryOp {
return false;
}
final BinaryOpItem other = (BinaryOpItem) obj;
if (this.leftSide != other.leftSide && (this.leftSide == null || !this.leftSide.equals(other.leftSide))) {
if (!Objects.equals(leftSide, other.leftSide)) {
return false;
}
if (this.rightSide != other.rightSide && (this.rightSide == null || !this.rightSide.equals(other.rightSide))) {
if (!Objects.equals(rightSide, other.rightSide)) {
return false;
}
if ((this.operator == null) ? (other.operator != null) : !this.operator.equals(other.operator)) {
if (!Objects.equals(operator, other.operator)) {
return false;
}
return true;