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

Use EqualsVerifier to test equals and hashCode, modified implementati… #244

Merged
merged 6 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 25 additions & 19 deletions src/main/java/org/jfree/chart/JFreeChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Klaus Rheinwald;
* Nicolas Brodu;
* Peter Kolb (patch 2603321);
* Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
*
* NOTE: The above list of contributors lists only the people that have
* contributed to this source file (JFreeChart.java) - for a list of ALL
Expand Down Expand Up @@ -102,7 +103,6 @@
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.Size2D;
import org.jfree.chart.ui.VerticalAlignment;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.SerialUtils;
import org.jfree.data.Range;
Expand Down Expand Up @@ -1510,19 +1510,13 @@ public boolean equals(Object obj) {
return false;
}
JFreeChart that = (JFreeChart) obj;
if (!this.renderingHints.equals(that.renderingHints)) {
return false;
}
if (this.borderVisible != that.borderVisible) {
return false;
}
if (!Objects.equals(this.borderStroke, that.borderStroke)) {
return false;
}
if (!PaintUtils.equal(this.borderPaint, that.borderPaint)) {
if (this.elementHinting != that.elementHinting) {
return false;
}
if (!this.padding.equals(that.padding)) {
if (!Objects.equals(this.padding, that.padding)) {
return false;
}
if (!Objects.equals(this.title, that.title)) {
Expand All @@ -1534,26 +1528,38 @@ public boolean equals(Object obj) {
if (!Objects.equals(this.plot, that.plot)) {
return false;
}
if (!PaintUtils.equal(
this.backgroundPaint, that.backgroundPaint
)) {
return false;
}
if (!Objects.equals(this.backgroundImage, that.backgroundImage)) {
return false;
}
if (this.backgroundImageAlignment != that.backgroundImageAlignment) {
return false;
}
if (this.backgroundImageAlpha != that.backgroundImageAlpha) {
if (Float.floatToIntBits(this.backgroundImageAlpha) !=
Float.floatToIntBits(that.backgroundImageAlpha)) {
return false;
}
if (this.notify != that.notify) {
return false;
}
if (!Objects.equals(this.id, that.id)) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + (this.borderVisible ? 1 : 0);
hash = 59 * hash + (this.elementHinting ? 1 : 0);
hash = 59 * hash + Objects.hashCode(this.padding);
hash = 59 * hash + Objects.hashCode(this.title);
hash = 59 * hash + Objects.hashCode(this.subtitles);
hash = 59 * hash + Objects.hashCode(this.plot);
hash = 59 * hash + Objects.hashCode(this.backgroundImageAlignment);
hash = 59 * hash + Float.floatToIntBits(this.backgroundImageAlpha);
hash = 59 * hash + (this.notify ? 1 : 0);
hash = 59 * hash + Objects.hashCode(this.id);
return hash;
}

/**
* Provides serialization support.
*
Expand Down Expand Up @@ -1623,7 +1629,7 @@ public Object clone() throws CloneNotSupportedException {
chart.title.addChangeListener(chart);
}

chart.subtitles = new ArrayList();
chart.subtitles = new ArrayList<>();
for (int i = 0; i < getSubtitleCount(); i++) {
Title subtitle = (Title) getSubtitle(i).clone();
chart.subtitles.add(subtitle);
Expand Down
68 changes: 36 additions & 32 deletions src/main/java/org/jfree/chart/LegendItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* David Li;
* Wolfgang Irler;
* Luke Quinane;
* Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
*
*/

Expand All @@ -54,10 +55,8 @@
import java.text.AttributedString;
import java.text.CharacterIterator;
import java.util.Objects;
import org.jfree.chart.text.AttributedStringUtils;
import org.jfree.chart.ui.GradientPaintTransformer;
import org.jfree.chart.ui.StandardGradientPaintTransformer;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.PublicCloneable;
import org.jfree.chart.util.SerialUtils;
Expand Down Expand Up @@ -935,68 +934,73 @@ public boolean equals(Object obj) {
return false;
}
LegendItem that = (LegendItem) obj;
if (this.datasetIndex != that.datasetIndex) {

if (!Objects.equals(this.dataset, that.dataset)) { //dataset
return false;
}
if (this.series != that.series) {
if (!Objects.equals(this.seriesKey, that.seriesKey)) { //
return false;
}
if (!this.label.equals(that.label)) {
if (this.datasetIndex != that.datasetIndex) { // datasetIndex
return false;
}
if (!AttributedStringUtils.equal(this.attributedLabel,
that.attributedLabel)) {
if (this.series != that.series) { // series
return false;
}
if (!Objects.equals(this.description, that.description)) {
if (!Objects.equals(this.label, that.label)) { // label
return false;
}
if (this.shapeVisible != that.shapeVisible) {
if (!Objects.equals(this.labelFont, that.labelFont)) { // labelFont
return false;
}
if (!ShapeUtils.equal(this.shape, that.shape)) {
if (!Objects.equals(this.description, that.description)) { // description
return false;
}
if (this.shapeFilled != that.shapeFilled) {
if (!Objects.equals(this.toolTipText, that.toolTipText)) { // toolTipText
return false;
}
if (!PaintUtils.equal(this.fillPaint, that.fillPaint)) {
if (!Objects.equals(this.urlText, that.urlText)) { // urlText
return false;
}
if (!Objects.equals(this.fillPaintTransformer,
that.fillPaintTransformer)) {
if (this.shapeVisible != that.shapeVisible) { // shapeVisible
return false;
}
if (this.shapeOutlineVisible != that.shapeOutlineVisible) {
if (this.shapeFilled != that.shapeFilled) { // shapeFilled
return false;
}
if (!this.outlineStroke.equals(that.outlineStroke)) {
if (!Objects.equals(this.fillPaintTransformer, // fillPaintTransformer
that.fillPaintTransformer)) {
return false;
}
if (!PaintUtils.equal(this.outlinePaint, that.outlinePaint)) {
if (this.shapeOutlineVisible != that.shapeOutlineVisible) {
return false;
}
if (!this.lineVisible == that.lineVisible) {
return false;
}
if (!ShapeUtils.equal(this.line, that.line)) {
return false;
}
if (!this.lineStroke.equals(that.lineStroke)) {
return false;
}
if (!PaintUtils.equal(this.linePaint, that.linePaint)) {
return false;
}
if (!Objects.equals(this.labelFont, that.labelFont)) {
return false;
}
if (!PaintUtils.equal(this.labelPaint, that.labelPaint)) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.dataset);
hash = 83 * hash + Objects.hashCode(this.seriesKey);
hash = 83 * hash + this.datasetIndex;
hash = 83 * hash + this.series;
hash = 83 * hash + Objects.hashCode(this.label);
hash = 83 * hash + Objects.hashCode(this.labelFont);
hash = 83 * hash + Objects.hashCode(this.description);
hash = 83 * hash + Objects.hashCode(this.toolTipText);
hash = 83 * hash + Objects.hashCode(this.urlText);
hash = 83 * hash + (this.shapeVisible ? 1 : 0);
hash = 83 * hash + (this.shapeFilled ? 1 : 0);
hash = 83 * hash + Objects.hashCode(this.fillPaintTransformer);
hash = 83 * hash + (this.shapeOutlineVisible ? 1 : 0);
hash = 83 * hash + (this.lineVisible ? 1 : 0);
return hash;
}

/**
* Returns an independent copy of this object (except that the clone will
* still reference the same dataset as the original {@code LegendItem}).
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/jfree/chart/LegendItemCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* (C) Copyright 2002-2021, by David Gilbert.
*
* Original Author: David Gilbert;
* Contributor(s): -;
* Contributor(s): Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
*
*/

Expand All @@ -39,6 +39,7 @@
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import org.jfree.chart.util.ObjectUtils;

/**
Expand Down Expand Up @@ -123,12 +124,19 @@ public boolean equals(Object obj) {
return false;
}
LegendItemCollection that = (LegendItemCollection) obj;
if (!this.items.equals(that.items)) {
if (!Objects.equals(this.items, that.items)) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 13 * hash + Objects.hashCode(this.items);
return hash;
}

/**
* Returns a clone of the collection.
*
Expand Down
Loading